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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.