私はこのようなものを持っています:
public function options()
{
$out = '';
$docs = $this->getAll();;
foreach($docs as $key => $doc) {
$out .= ',{"label" : "' . $doc['name'] . '", "value" : "' . $doc['id'] .'"}';
}
return $out;
}
DB からのオプションのリストが表示されますが、上部に null 値も表示されます。
次のように書くと:
public function options()
{
//$out = '';
$docs = $this->getAll();;
foreach($docs as $key => $doc) {
$out = '';
$out .= '{"label" : "' . $doc['name'] . '", "value" : "' . $doc['id'] .'"}';
}
return $out;
}
null 値は返されませんが、1 つの値しか返されません。
$out .= ',{"label" : "' . $doc['name'] . '", "value" : "' . $doc['id'] .'"}';
この行に を追加しない,
と、エラー メッセージが表示されます。これ$out = '';
は、一番上にあるためです。それでは、最初に空の値なしで DB からすべての値を取得する方法を教えてください。
;;
このコードで (ダブル セミコロン)を使用する理由について、別の質問もあります。
$docs = $this->getAll();;