Jump to content

Displaying query results twice


padams

Recommended Posts

I need to display the results of a query twice on one page and have run into a problem. I have used mysql_data_seek at the end of the first do while loop, setting the internal pointer back to 0, but when I echo out the results a second time I get a blank record at the top.

 

You can see the results on a test page at http://elsombrero.stacnz.com/testing.php, the code is:

 

$courses_sql = "SELECT * FROM courses";

$courses_query = mysql_query($courses_sql) or die(mysql_error());

$rsCourses = mysql_fetch_assoc($courses_query);

 

<ul><?php

  do {

  ?>

  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">

  <?php echo $rsCourses['cName']; ?>

  </a></li>

  <?php

  } while ($rsCourses = mysql_fetch_assoc($courses_query));

  mysql_data_seek($courses_query, 0);

  ?>

<?php

  do {

  ?>

  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">

  <?php echo $rsCourses['cName']; ?>

  </a></li>

  <?php

  } while ($rsCourses = mysql_fetch_assoc($courses_query));

  mysql_data_seek($courses_query, 0);

  ?></ul>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/202939-displaying-query-results-twice/
Share on other sites

It's getting late and my brain isn't all there at this hour, but I'll give this a shot...

 

Do you have the same issue if you convert the do-while to a while?

 

$courses_sql = "SELECT * FROM courses";
$courses_query = mysql_query($courses_sql) or die(mysql_error());

<ul><?php
while ($rsCourses = mysql_fetch_assoc($courses_query)){
  ?>
  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">
  <?php echo $rsCourses['cName']; ?>
  </a></li>
  <?php
  }
  mysql_data_seek($courses_query, 0);
  ?>
<?php
  while ($rsCourses = mysql_fetch_assoc($courses_query)){
  ?>
  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">
  <?php echo $rsCourses['cName']; ?>
  </a></li>
  <?php
  }
  mysql_data_seek($courses_query, 0);
  ?></ul>
</html>

 

-Kalivos

Let's change this so you only have to loop through the dataset once:

<?php
$tmp = array();
$tmp1 = array();
$tmp2 = array();
$courses_sql = "SELECT * FROM courses";
$courses_query = mysql_query($courses_sql) or die(mysql_error());
$tmp[] = '<ul>';
while ($rsCourses = mysql_fetch_assoc($courses_query)) {
  $tmp1[] = '<li><a href="menu.php?courseID=' . $rsCourses['courseID'] . '">' . $rsCourses['cName'] . '</a></li>';
  $tmp2[] = '<li><a href="menu.php?courseID=' . $rsCourses['courseID'] . '">' . $rsCourses['cName'] . '</a></li>';
  }
$tmp[] = implode("\n",$tmp1);
$tmp[] = implode("\n",$tmp2);
$tmp[] = '</ul>';
$tmp[] = '</html>';
echo implode("\n",$tmp) . "\n";
?>

 

Ken

Brilliant, solved it. Thanks, I really should have thought of that myself...

 

It's getting late and my brain isn't all there at this hour, but I'll give this a shot...

 

Do you have the same issue if you convert the do-while to a while?

 

$courses_sql = "SELECT * FROM courses";
$courses_query = mysql_query($courses_sql) or die(mysql_error());

<ul><?php
while ($rsCourses = mysql_fetch_assoc($courses_query)){
  ?>
  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">
  <?php echo $rsCourses['cName']; ?>
  </a></li>
  <?php
  }
  mysql_data_seek($courses_query, 0);
  ?>
<?php
  while ($rsCourses = mysql_fetch_assoc($courses_query)){
  ?>
  <li><a href="menu.php?courseID=<?php echo $rsCourses['courseID']; ?>">
  <?php echo $rsCourses['cName']; ?>
  </a></li>
  <?php
  }
  mysql_data_seek($courses_query, 0);
  ?></ul>
</html>

 

-Kalivos

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.