Solved

PHP: mysql_data_seek($rs, 0) returns additional blank line when used with 2nd Repeat Region

Posted on 2009-04-06
2
585 Views
1 Endorsement
Last Modified: 2013-12-12
I have a simple recordset that simply lists Record ID's and Record Names.
I want to repeat the same rows from the query in 2 or more separate places on the page.

When using "mysql_data_seek($rs, 0)" to restart the recordset query i am getting a additional blank line added to the repeated content.

------------------------
Existing Code:
<p>
<?php
      do {
            echo "1. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>
<p>
<?php
      mysql_data_seek($rs, 0);      
      do {
            echo "2. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>

Returns the following:
1. Field 01
1. Field 02
1. Field 03

2.
2. Field 01
2. Field 02
2. Field 03
------------------------

This is a scaled down example of what i'm actually doing which is multiple nested repeat regions, the problem above is causing my script to lose the primary key of the second repeat region.

I could of course fix this by using different Recordset Queries per repeat region but won't this be placing unnecessary stress on the database?

How can i fix this blank row being added?
Thanks
1
Comment
Question by:ticallian
2 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24075925
Hello,

Use this :

while ($row_rs = mysql_fetch_assoc($rs))
{
echo "1. ".$row_rs['name']."<br/>";
}
?>
</p>
<p>
<?php
mysql_data_seek($rs, 0);      
     
while ($row_rs = mysql_fetch_assoc($rs))
{
echo "2. ".$row_rs['name']."<br/>";
}


Instead of :

      do {
            echo "1. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>
<p>
<?php
      mysql_data_seek($rs, 0);      
      do {
            echo "2. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>

Do part will execute atleast once  even if the while condition is not true.


Hope this will Help you.

Regards.
1
 

Author Closing Comment

by:ticallian
ID: 31566955
Thanks, with a small adjustment the code worked, without the adjust i lost the first row from the first repeat region.

Revised code:
<?php
      do {
            echo "1. ".$row_rs['Field']."";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>



<?php
      mysql_data_seek($rs, 0);        
      while ($row_rs = mysql_fetch_assoc($rs)) {
            echo "2. ".$row_rs['Field']."";
      }
?>
1

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question