What is the best way to get the same data multiple times on a page?

I want to show the same data on the page where the data was extracted from MySQL several times.

First I want to get data from MySQL using mysql_fetch_assoc() in a while loop, and then show it as a menu. The second time I want to show the same data as the site map in the footer.

I am currently mysql_fetch_assoc() twice:

 // This one is the Menu echo '<ul class="menu">'; while( $data = mysql_fetch_assoc($query) ) { echo '<li><a href="page.php?id='.$data['id'].'">'.$data['title'].'</a>'; } echo '</ul>'; // other page contents here // at the footer - the small sitemap echo '<ul class="sitemap">'; while( $f_data = mysql_fetch_assoc($query) ) { echo '<li><a href="page.php?id='.$f_data['id'].'">'.$f_data['title'].'</a>'; } echo '</ul>'; 

I think the above code can use more resources than required by querying the database twice. Since I have data on the page, to get the same data, memory is again lost, not good.

So my questions are:

  • mysql_fetch_assoc() it send separate queries and retrieve data from the database every time I use mysql_fetch_assoc() ? Or is it just fetching data from the database once, and then it just goes through the existing array?

  • What is the best solution in my case?

Simple - Am I doing it in the best way? Is there a better way to do this without wasting memory / resources / time? Since this is the same data that I show twice.

+4
source share
7 answers

It would be best to store the data in an array that you can use whenever you want, without having to make more queries to your database, for example:

 // 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>'; 
+6
source

Just make a link to it :)

 $menu = '<ul class="menu">'; while( $data = mysql_fetch_assoc($query) ) { $menu .= '<li><a href="page.php?id='.$data['id'].'">'.$data['title'].'</a>'; } $menu .= '</ul>'; // header echo $menu; // footer echo $menu; 
+3
source

Save the received information in an array and skip this array for both the menu and the footer.

+3
source

1.No this is not. mysql_query queries the database, and mysql_fetch_assoc used to filter the returned result set.

2. There is a simpler solution:

 $output = ""; while( $data = mysql_fetch_assoc($query) ) $output .= "<li><a href='page.php?id={$data['id']}'>{$data['title']}</a></li>"; echo "<ul class='menu'>{$output}</ul>"; // This one is the Menu // other page contents here echo "<ul class='sitemap'>{$output}</ul>"; // at the footer - the small sitemap 
+3
source

Why are you making multiple db calls for the same data? This is a waste of resources. Just save the db data in an array and then output it wherever you want and you will like it.

Try the following:

  while( $data = mysql_fetch_assoc($query) ) $menu[$data['id']] = $data['title']; // This one is the Menu echo '<ul class="menu">'; foreach ($menu AS $id => $title) echo "<li><a href='page.php?id=$id'>$title</a></li>"; echo '</ul>'; // other page contents here // at the footer - the small sitemap echo '<ul class="sitemap">'; foreach ($menu AS $id => $title) echo "<li><a href='page.php?id=$id'>$title</a></li>"; echo '</ul>'; 
+2
source

Use mysql_data_seek($result, 0);

It sets the result pointer to the beginning.

+1
source

Use mysqli_data_seek ($ result, 0).

Seems much more efficient and works like a charm

0
source

All Articles