Jump to content

Recommended Posts

Hi I have posted this before but I thought I would add a proper example.

 

The following is an example of a form used to display an array which works fine:

 

<?php

$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7) 
             ); 
$shops = sizeof($shop);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="update.php">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>select</strong></td>
<td align="center"><strong>flower</strong></td>
<td align="center"><strong>price</strong></td>
<td align="center"><strong>number</strong></td>
</tr>
<?php
for($i = 0; $i<$shops; $i++){
?>
<tr>
<td align="center"><input name="name[]" type="text" id="name"
value="<?php echo $shop[$i][0]?>" ></td>
<td align="center"><input name="lastname[]" type="text" id="age"
value="<?php echo $shop[$i][1]?>"></td>
<td align="center"><input name="email[]" type="text" id="wife"
value="<?php echo $shop[$i][2]?>"></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</form>
</table>

 

However If I try to add a select to this table the 3 rows displayed above no longer show instead I get just 1 row:

<?php

$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7) 
             ); 
$shops = sizeof($shop);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="update.php">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>select</strong></td>
<td align="center"><strong>flower</strong></td>
<td align="center"><strong>price</strong></td>
<td align="center"><strong>number</strong></td>
</tr>
<?php
for($i = 0; $i<$shops; $i++){
?>
<tr>
<td><select name = "PCode[]"> <?php

      for($i = 0; $i<$shops; $i++)
      {
        echo '<option value = '.$shop[$i][0];
        if ($shop[$i][0] == $shop[$i][0]) echo ' selected';
        echo '>'.$shop[$i][0].'</option>\n';
      }

?></select></td>
<td align="center"><input name="name[]" type="text" id="name"
value="<?php echo $shop[$i][0]?>" ></td>
<td align="center"><input name="lastname[]" type="text" id="age"
value="<?php echo $shop[$i][1]?>"></td>
<td align="center"><input name="email[]" type="text" id="wife"
value="<?php echo $shop[$i][2]?>"></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</form>
</table>


 

can anyone advise on how I can get all 3 rows to display with the select in each one??

 

Link to comment
https://forums.phpfreaks.com/topic/52537-solved-a-puzzle/
Share on other sites

use different variable name in 2nd for loop

      for($j = 0; $j<$shops; $j++)
      {
        echo '<option value = '.$shop[$j][0];
        if ($shop[$j][0] == $shop[$i][0]) echo ' selected';
        echo '>'.$shop[$j][0].'</option>\n';
      }

Link to comment
https://forums.phpfreaks.com/topic/52537-solved-a-puzzle/#findComment-259279
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.