0

配列に情報を格納するにはどうすればよいですか?

データベースから情報を取得し、HTML コードを配列に格納しようとしています。

これは可能ですか?もしそうなら、どのように?

function echothatshit() {
    $sqll=mysql_query("select id,data from produkter where weight='1'");
    $alla = array();    
    while($row=mysql_fetch_array($sqll))
    {
        $idd=$row['id'];
        $data=$row['data'];
        $alla[] = '<option value="'.$idd.'">'.$data.'</option>';
    }
}

$test2 = echothatshit();                       
echo $test2;
4

4 に答える 4

4

最後に関数が必要ですreturn $alla;。また、配列を直接エコーすることはできません。ループする必要があります。

于 2012-07-05T12:42:17.887 に答える
2
function echothatshit() {
    $sqll=mysql_query("select id,data from produkter where weight='1'");
    $alla = array();
    $i=0;    
    while($row=mysql_fetch_array($sqll))
    {
        $idd=$row['id'];
        $data=$row['data'];
        $alla[$i] = '<option value="'.$idd.'">'.$data.'</option>';
        $i++;
    }
    $yourVariable="";
    for ($j=0;$j<$i;$j++)
    {
        $yourVariable.=$alla[$j];
    }
    return $yourVariable;
}

$test2 = echothatshit();                       
echo $test2;
于 2012-07-05T12:45:20.407 に答える
1

文字列として持たない理由$alla、つまり(いくつかのフォーマット/名前変更/単純化を伴う):

function getProductsHTML()
{
  $sql = mysql_query("select id,data from produkter where weight='1'");
  $html = '';

  while($row = mysql_fetch_array($sql))
    $html .= '<option value="'.$row['id'].'">'.$row['data'].'</option>'."\n";

  return $html;
}

echo getProductsHTML();
于 2012-07-05T12:45:13.317 に答える
0
function echothatshit() {
$sqll=mysql_query("select id,data from produkter where weight='1'");
$alla = array();    
while($row=mysql_fetch_array($sqll))
    {
$idd=$row['id'];
$data=$row['data'];
$alla[] = '<option value="'.$idd.'">'.$data.'</option>';
    }
return $alla;
}
$test2 = echothatshit();                       
echo $test2;

$test2 が配列 $alla を取得するようにしたい場合は、それを返します。そして、配列を正しく印刷するには、使用します

echo "<pre>";
print_r($test2);
echo "</pre>";
于 2012-07-05T12:46:16.230 に答える