Jump to content


Photo

Recall query several times


  • Please log in to reply
5 replies to this topic

#1 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 05 October 2006 - 05:11 PM

Hi guys.

Maybe this is a silly question but here's goes:

I have a form that calls info from the DB #1 in order to be included in DB#2, here's the query:

        mysql_select_db($database_bb, $bb);
$query_package = "SELECT * FROM packages ORDER BY name ASC";
$package = mysql_query($query_package, $bb) or die(mysql_error());
$row_package = mysql_fetch_assoc($package);
$totalRows_package = mysql_num_rows($package);

Everything's work fine.  The thing is...  I need to call the "packages" table 5 times on popups menues like this:

<select name="top1">//(I want to create 5 of this)
<?php do {?>
<option value="<?php echo $row_package['id_package']?>"><?php echo $row_package['name']?></option><?php
} while ($row_package = mysql_fetch_assoc($package));
?></select>

The problem is when I print the form only the first popup menu show the info from the DB #1 the other doesn't work.  I tried to solve it by creating 5 different querys :-S

I think should be an easier way to do that because I'm calling the same info 5 times.

Thanks for the help, as always it will be appreciated.

#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 05 October 2006 - 05:16 PM

when you
while($row = mysql_fetch_assoc($package)) {
do something
}
it runs through the list of returned rows it has and then it stops so it will only ever run through the rows once unless you either re-run the query or
mysql_data_seek($package,0);

and reset the pointer to the first record again

hope that is what you were after
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 05 October 2006 - 05:24 PM

Thanks for the help Paul....

But what I'm trying to do is call 5 different popups menus with the info from the DB#1

The script code on the first popup works fine.  But the second shows nothing look:

<select name="top1">//(First)
<?php do {?>
  <option value="<?php echo $row_package['id_package']?>"><?php echo $row_package['name']?></option><?php
} while ($row_package = mysql_fetch_assoc($package));
?></select>

<select name="top2">//(Second)
<?php do {?>
  <option value="<?php echo $row_package['id_package']?>"><?php echo $row_package['name']?></option><?php
} while ($row_package = mysql_fetch_assoc($package));
?></select>

Thanks

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 05 October 2006 - 05:32 PM

yes because when you have done the first while loop for top1 then the data pointer has gone past the last row in the returned fields and needs resetting back to zero before running it again try something like this
<select name="top1">//(First)
<?php do {?>
   <option value="<?php echo $row_package['id_package']?>"><?php echo $row_package['name']?></option><?php
} while ($row_package = mysql_fetch_assoc($package));
?></select>

<?php mysql_data_seek($package,0)?>

<select name="top2">//(Second)
<?php do {?>
   <option value="<?php echo $row_package['id_package']?>"><?php echo $row_package['name']?></option><?php
} while ($row_package = mysql_fetch_assoc($package));
?></select>

<?php mysql_data_seek($package,0)?>


and so on
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 October 2006 - 05:37 PM

except that you could you know, put that in a loop.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 05 October 2006 - 05:57 PM

Awesome....

As always you guys got it.

Thanks a lot.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users