且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在页面中多次获取相同数据的***方法是什么?

更新时间:2023-02-09 16:00:14

您***的解决方案是将检索到的数据存储在一个数组中,然后您可以随时使用它,而不必对数据库进行更多查询,例如所以:

Your best solution would be to store your retrieved data in an array, which you can then use whenever you want without having to make more queries to your database, like so:

// First we make the query and store the rows in an array
$result = mysql_query($query);
$data_array = array();
while ($data = mysql_fetch_assoc($result)) {
    $data_array[] = $data;
}

// Then we can loop through the values in that array without making further queries
echo '<ul class="menu">';
foreach ($data_array as $data) {
    echo '<li><a href="page.php?id=', $data['id'], '">', $data['title'], '</a>';
}
echo '</ul>';

// Another loop through our array
echo '<ul class="sitemap">';
foreach ($data_array as $f_data) {
    echo '<li><a href="page.php?id=', $f_data['id'], '">', $f_data['title'], '</a>';
}
echo '</ul>';