Forum Moderators: coopster

Message Too Old, No Replies

How to add new <option value> with MySQL + PHP

         

kennyg

4:34 am on Jun 10, 2007 (gmt 0)

10+ Year Member




<?
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");

?>

<td width="6025" rowspan="3" align="left"><p class="style1">Target:
<select name="select">
<? echo "<option value='$id'>$name</option>";?> <--------(want to add option here)
</select>

<td width="6025" rowspan="3" align="left"><p class="style1"Delete Target:
<select name="select">
<? echo "<option value='$id'>$name</option>";?> <--------(want to add option here)
</select>

##end of code

Lets say with every line of results I draw out from MySQL and processed into variables with PHP.

How can I actively add a '<option value ='$id'> $name </option>' in the 2 places I want to everytime the loop hooks up?

I tried the above code which I written but it even loops echoes the HTML text. How can I skip the 2nd part of the HTML text?

Habtom

5:03 am on Jun 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are showing it ok, except some tags are a bit messed up.

Errors:
You left td, p unclosed.
Small fixes is there in between. The code is as follows:

<td width="6025" rowspan="3" align="left">
<p class="style1">
Target:
<select name="select">
<?php echo "<option value='$id'>$name</option>";?>
</select>
</p>
</td>

<td width="6025" rowspan="3" align="left">
<p class="style1">
Delete Target:
<select name="select">
<?php echo "<option value='$id'>$name</option>";?>
</select>
</p>
</td>

Habtom

kennyg

5:31 am on Jun 10, 2007 (gmt 0)

10+ Year Member



<?
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");

?>

<td width="6025" rowspan="3" align="left"><p class="style1">Target:
<select name="select">
<? echo "<option value='$id'>$name</option>";?> <--------(want to add option here)
</select>

<td width="6025" rowspan="3" align="left"><p class="style1"Delete Target:
<select name="select">
<? echo "<option value='$id'>$name</option>";?> <--------(want to add option here)
</select>
<?
$i++;
}
?>
##end of code

Sorry this should be the actual code. During the $i++ which helps to move the loop, the php works well.. yes. It loops till all the <option> $names </option> are out. But it also loops and echos the <select name="select"> many time.

How can I bypass the HTML echoing? Sorry for the misunderstanding

Habtom

6:40 am on Jun 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think most people here would agree looping just the option
<?php echo "<option value='$id'> $name </option>";?>

<td width="6025" rowspan="3" align="left">
<p class="style1">
Target:
<select name="select">
<?
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
?>
<? echo "<option value='$id'>$name</option>";?>
<?php }?>
</select>

You do the same thing for the other loop. Or assign this:
$new_variable = "<option value='$id'>$name</option>";
to a variable and just echo in the other select box.

I hope this helps.

Habtom

kennyg

7:39 am on Jun 10, 2007 (gmt 0)

10+ Year Member



.. totally didnt think of that. Thanks a lot!

Habtom

8:05 am on Jun 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



np, and welcome to Webmasterworld.

[jatar_k, is welcoming reserved for moderators? :) ]

eelixduppy

1:19 pm on Jun 10, 2007 (gmt 0)



>> is welcoming reserved for moderators?

Of course not; everyone deserves to be welcomed here. It does not matter who is pulling the wagon ;)

and yes, welcome kennyg! :)

Habtom

1:33 pm on Jun 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:)