dongyue8640 2011-10-19 06:24
浏览 36
已采纳

在PHP数组中存储和显示MySQL结果

Suppose I have the following MySQL table result:

ID    price    
-------------
 1     10      
 2     20      
 3     30      


Basically what I want to accomplish is to store these values to a PHP array, and have these values displayed/echoed as a HTML table on a per row basis.

I could do something like:

if($result) {

    $i = 0;

    while ($row = mysql_fetch_array($result)) {
        $id[$i] = $row['id'];
        $price[$i] = $row['price'];      
    }
}

And just have those elements echo together with the HTML table.

However, I also need to have a function that allows the user to delete a row. With that mind I believe I need to have some sort of a 'key' for every row as the identifier for deletion -- a feature which multidimensional array supports.

  • 写回答

2条回答 默认 最新

  • duandan9680 2011-10-19 06:39
    关注

    There's nothing preventing you from using a multi dimensional array and using one of the unique values as an index:

    // Store result
    $data = array();
    if($result) {
        while ($row = mysql_fetch_array($result)) {
            $data[$row['raiser_id']] = $row;
        }
    }
    
    
    // Building table
    echo "<table>";
    foreach ($data as $row)
    {
        echo "<tr>";
        echo "<td>" . $row['raiser_id'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['fcr'] . "</td>";
        echo "<td>" . $row['date_of_application'] . "</td>";
        echo "<td>" . $row['no_of_heads'] . "</td>";
        echo "<td>" . $row['place_of_farm'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    
    // Removing an entry by raiser_id
    $raiser_id = 10;
    if (!empty($data[$raiser_id]))
    {
        unset($data[$raiser_id]);
        echo "Removed entry";
    }
    else
    {
        echo "No entry to remove";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出