hi ppl of the forum..

i am so glad i joined this forum, members here play a big role in my learning PHP, n im able to work on a PHP project so soon all thanks to the forum and its members..

n here i'm back again, with another piece of my code, not working the way it is supposed to. its issue with my logic i guess.

whats happening here is, i am fetching data from db and displaying it in the form. form has dropdown and textfield. basically this is a edit form, so it should display all the data entered by the user. the data entered in the textfield is appearing properly, but one of the dropdown doesnt display all the options.

<?php
for($i=0; $i<$cnt; $i++)
{
?>
        <tr>
       <td>
	<?php
		echo "<select name='component' id='component' ><option>".$itemName[$i]."</option>";//$itemName[$i] displayes the option selected
 
		while($rowComp = mysql_fetch_array($resComp))
		{
		echo "<option id=".$rowComp['id']." value=\"".$rowComp['name']."\">".$rowComp['name']."</option>";
		}
	?>
	</select>
	</td>


	<td><input type="text" id="quantity" name="quantity" value="<?php echo $quantity[$i] ?> "></td>
	<td><input type="text" id="rate" name="rate" value="<?php echo $rate[$i] ?>"></td>
	<td><input type="text" id="total" name="total" value="<?php echo $total[$i] ?>"></td>
	
	</tr>
	<?php
	}
	?>

for the condition $cnt=2 and 2 options within the dropdown, two rows of this edit form appear, out of which 1st row displays all details correctly, however in the 2nd row only one option is displayed in the drop down.

pls have a look at the code and point out where iam going wrong..

thank you ppl

Recommended Answers

All 4 Replies

Hi,
Could you show what you have in $resComp ?

Just after the while loop i.e., Line 13 in your previous post, add the following statement

mysql_data_seek ($resComp, 0);

It should fix the problem.

What I believe is happening, is when you make a call to get the value from the $resComp, the internal pointer moves forward, so the next time you make a call to the same result resource, you get the next row. In you code above, the query to get the $resComp result happens before the for statement. The first time the pointer goes through (when $i=1) using the while($rowComp = mysql_fetch_array($resComp)) loop is puts the pointer in the last row of the result resource. The ssecond time you call, the same resource ($resComp) using the while loop, the pointer does not get reset and there is no more data to pull so you will get only one line in your dropdown box (from your inital <option> statement before the while loop).

Adding the line mysql_data_seek ($resComp, 0); resets the pointer and hence you should be able to pull the data again.

I hope my explanation is not too confusing... And I hope I'm right, you'll know when you try out the code. So please let me know. :-)

woooohooooooooo thanks guys..
sudeep thanks for the help, it worked n i followed ur suggestion too..

Your welcome... You can upvote me you know ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.