loop with db results

Can anyone help me?

i need to print values as follows,

item_1000139
item_1000141 and so on… then

cat_1000139
cat_1000141

this is my code

<select name="txtpg" id="txtpg"> <option value="Home">Home</option> <option value="Main">Main</option> <option value="Sub">Sub</option> <?php $sql_sel="SELECT itemID FROM items WHERE bitapproved=1"; $res_sel=mysql_query($sql_sel); $arr_sel=mysql_fetch_assoc($res_sel); do{ echo "<option value=item_".$arr_sel['itemID'].">item_".$arr_sel['itemID'].""; echo "<option value=cat_".$arr_sel['itemID'].">cat_".$arr_sel['itemID'].""; }while($arr_sel=mysql_fetch_assoc($res_sel)); ?> </select>

output is some thing like this…

item_1000139
cat_1000139
item_1000141
cat_1000141

I think using the ORDER BY statement in your sql statement could help.

The problem’s in the loop:

[php]
do {
echo “<option value=item_”.$arr_sel[‘itemID’].">item_".$arr_sel[‘itemID’]."";
echo “<option value=cat_”.$arr_sel[‘itemID’].">cat_".$arr_sel[‘itemID’]."";
} while ($arr_sel=mysql_fetch_assoc($res_sel));
[/php]

First it echoes item_# value, then it echoes cat_# value. And it does this in a loop. PHP code always does what you tell it to do.

My advice would be to make two separate loops, one for item_# and one for cat_#.

Btw, why use do … while()?

As you suggest two separate loops for item and cat…

my code is like follows,

do{ echo "<option value=item_".$arr_sel['itemID'].">item_".$arr_sel['itemID']."</option>"; }while($arr_sel=mysql_fetch_assoc($res_sel)); do{ echo "<option value=cat_".$arr_sel['itemID'].">cat_".$arr_sel['itemID']."</option>"; }while($arr_sel=mysql_fetch_assoc($res_sel));

and output is,

item_1000139
item_1000141
item_1000144
item_1000145
cat_

but if i modify the code as follows i get what i needed as output…

<?php
		mysql_select_db($database_conmyprice, $conmyprice);
		$sql_sel="SELECT itemID FROM items WHERE bitapproved=1";
		$res_sel=mysql_query($sql_sel);
		$arr_sel=mysql_fetch_assoc($res_sel);
		do{
		echo "<option value=item_".$arr_sel['itemID'].">item_".$arr_sel['itemID']."</option>";
		}while($arr_sel=mysql_fetch_assoc($res_sel));
		mysql_select_db($database_conmyprice, $conmyprice);
		$sql_sel="SELECT itemID FROM items WHERE bitapproved=1";
		$res_sel=mysql_query($sql_sel);
		$arr_sel=mysql_fetch_assoc($res_sel);
		do{
		echo "<option value=cat_".$arr_sel['itemID'].">cat_".$arr_sel['itemID']."</option>";
		}while($arr_sel=mysql_fetch_assoc($res_sel));
		?>

Sorry for the big post…
is there any other way to use rather then do…while()?

while() {}
for() {}
for-each() {}

And if you really wanna live on the edge, you could try recursive functions :wink:

If i use while() the first item Id wont display at all…

thatswhy i went to do…while()

Sponsor our Newsletter | Privacy Policy | Terms of Service