Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-04-06
2
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

610 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