homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
While loops and mysql data seek
Sub_Seven




msg:4293511
 2:32 am on Apr 7, 2011 (gmt 0)

Hello everyone,

I am trying to get the results from a MySQL DB and display them in a few HTML select tags.

The first while loop runs fine, the second runs fine if I use mysql_data_seek($result, 0); but I cannot do the same trick for a third while loop and I need to have 10 of these, what is the trick for this scenario?

This is the code so far if useful at all:
<p>
<select name="brand_01">
<option selected="selected" value="<?php echo $row['brand_01']; ?>"><?php echo $row['brand_01']; ?></option>
<?php while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['cp_brand_name'].'">'.$row['cp_brand_name'].'</option>';
} ?>
</select>
</p>
<?php mysql_data_seek($result, 0); ?>
<p>
<select name="brand_02">
<option selected="selected" value="<?php echo $row['brand_02']; ?>"><?php echo $row['brand_02']; ?></option>
<?php while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['cp_brand_name'].'">'.$row['cp_brand_name'].'</option>';
} ?>
</select>
</p>


I'm pretty sure this can be done, I just don't have a clue,

Thanks

 

coopster




msg:4316858
 1:59 pm on May 24, 2011 (gmt 0)

Why use data seek? If you need to "rewind" the pointer ... well, I guess I never approach it that way. I build the select elements within the same while loop construct. Iterate once over the result set, building the select containers at the same time into a variable. Echo them out afterward.

agent_x




msg:4316887
 2:38 pm on May 24, 2011 (gmt 0)

Another way to do it is to add the rows from the result into an array, then loop through that array whenever you want to create the HTML element, e.g.

$my_array = array();
while($row = mysql_fetch_array($result)
{
$my_array[] = $row;
}

and then

for($i = 0; $i < sizeof($my_array); $i++)
{
echo '<option value="' . $my_array[$i]["cp_brand_name"] . '">' . $my_array[$i]["cp_brand_name"] . '</option>';
}

You can do that loop as many times as you like then without running any more MySQL queries.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved