Skip to main content
Inspiring
October 29, 2006
Question

If-condition and Repeat Region

  • October 29, 2006
  • 6 replies
  • 364 views
In the sidebar I want to list the articles whose number is "3" or more
or which are without any number, whereas in the main content the
articles with numbers "1" or "2" should be listed.
So I have in the maincontent:

<?php do { ?>
<?php if ($row_rsFirstpage['main_number'] < 3 ) { ?>
<p><?php echo $row_rsFirstpage['main_text']; ?></p>
<? } ?>
<?php } while ($row_rsFirstpage = mysql_fetch_assoc($rsFirstpage)); ?>

and in the sidebar:

<?php do { ?>
<?php if ($row_rsFirstpage['main_number']=="" ||
$row_rsFirstpage['main_number'] > 2 ) { ?>
<li><?php echo $row_rsFirstpage['main_text']; ?></li>
<? } ?>
<?php } while ($row_rsFirstpage = mysql_fetch_assoc($rsFirstpage)); ?>

Why doesn't that code work. Well, I could use two recordsets but would
like to get a result with only one.

Martin
This topic has been closed for replies.

6 replies

Inspiring
October 30, 2006
David Powers wrote:
> but if you're on a busy shared server, it's possible that there
> could be a delay in getting the second database result.

Thank you!
Inspiring
October 30, 2006
Martin Lang wrote:
> I could make two recordsets (No1: SELECT main_text etc. WHERE
> main_number < 3 goes into content, No2: SELECT main_text etc. WHERE
> main_number > 2 goes into sidebar). Is there any advantage to use that
> instead of the php-commands?

I see what you mean. I suspect that the difference in performance would
be minimal. One advantage of doing it with PHP is that you need only one
trip to the database. MySQL is extremely fast, particularly with simple
queries, but if you're on a busy shared server, it's possible that there
could be a delay in getting the second database result.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Inspiring
October 29, 2006
David Powers wrote:
> Sorry, I don't understand what you mean by select in this case.

I could make two recordsets (No1: SELECT main_text etc. WHERE
main_number < 3 goes into content, No2: SELECT main_text etc. WHERE
main_number > 2 goes into sidebar). Is there any advantage to use that
instead of the php-commands?

Martin


Inspiring
October 29, 2006
Martin Lang wrote:
> Basically, is it better to use one select and to apply php-commands as I
> did (with your help only) or is it recommended to build two selects to
> achieve the same result?

Sorry, I don't understand what you mean by select in this case.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Inspiring
October 29, 2006
David Powers wrote:
> Because you have looped through the database result resource. You need
> to reset it to the beginning before looping through it again.
> <?php mysql_data_seek($rsFirstpage, 0); ?>

Great - never heard of that. I had to change the condition slightly into
<?php if ($row_rsFirstpage['main_number']!="" &&
$row_rsFirstpage['main_number'] < 3 ) { ?> - but it works now.

Basically, is it better to use one select and to apply php-commands as I
did (with your help only) or is it recommended to build two selects to
achieve the same result?

Martin


Inspiring
October 29, 2006
Martin Lang wrote:
> Why doesn't that code work.

Because you have looped through the database result resource. You need
to reset it to the beginning before looping through it again.

Put this before your sidebar code:

<?php mysql_data_seek($rsFirstpage, 0); ?>

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/