4xjbh Posted June 27, 2013 Share Posted June 27, 2013 I am not getting the correct option loop selected results for this and I am not sure why. It was working fine when I had it in a while loop but I am separating my html from php and I changed it over to a foreach loop as it is an array....... Where have I gone wrong, Thanks in advance, James <select id='status'> <option>Please Select</option> <?php foreach ($options as $option): ?> <option value=<?php echo $option['id']> <?php echo $option['list_value'] ?> </option> <?php endforeach ?> </select><br/> // Status select options $sql_status = "SELECT value_lists.id, value_lists.list_value FROM value_lists WHERE value_lists.list_name = 'project_status' ORDER BY value_lists.list_value ASC"; $status_results = mysqli_query($link,$sql_status); $options = mysqli_fetch_array($status_results); php 5.3.13 | mysql 5.5.24 | apache 2.2.22 Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/ Share on other sites More sharing options...
dalecosp Posted June 27, 2013 Share Posted June 27, 2013 "Syntax error, unexpected > in line $somewhere" ...Try this? foreach ($options as $option): ?> <option value=<?php echo $option['id'].">".$option['list_value'] ?></option> <?php endforeach ?> Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/#findComment-1438189 Share on other sites More sharing options...
trq Posted June 27, 2013 Share Posted June 27, 2013 mysqli_fetch_array() return one row at a time in an array consisting of the data from each column. You will need to create an array of all rows first, then loop through that. eg; $options = array(); while ($row = mysqli_fetch_array($status_results)) { $options[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/#findComment-1438191 Share on other sites More sharing options...
4xjbh Posted June 27, 2013 Author Share Posted June 27, 2013 That was a mistake I made at post time. Even with only the one php tag it does not work. I get 4 rows returned in the combo with 2 2 A A instead of Active, complete. On hold, Lost. <select id='status'> <option>Please Select</option> <?php foreach ($options as $option): ?> <option> <?php echo $option['list_value'] ?> </option> <?php endforeach ?> </select><br/> Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/#findComment-1438212 Share on other sites More sharing options...
4xjbh Posted June 28, 2013 Author Share Posted June 28, 2013 I have this working now and I have to set 'selected' when displayed. I gave it a good go but I have not got it working. What is wrong with my sel function? Thanks in advance. <select id='status'> <option>Please Select</option> <?php while($row_status = mysqli_fetch_assoc($results_status)): ?> <?php //if ($row_status['list_value']==$status){$rs = "selected";} else {$rs = "";} ?> <option <?php sel($row_status['list_value'],$category)?> value=<?php echo $row_status['list_value']?> > <?php echo $row_status['list_value'] ?> </option> <?php endwhile; ?> </select> // Status select options $sql_status = "SELECT value_lists.id, value_lists.list_value FROM value_lists WHERE value_lists.list_name = 'project_status' ORDER BY value_lists.list_value ASC"; $results_status = mysqli_query($link,$sql_status); function sel($row_status,$status){ if (is_array($row_status)){ foreach ($row_status as $optval){ if ($status == $optval){ return "selected"; } } } else if ($status == $row_status){ return "selected"; } return '';} Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/#findComment-1438379 Share on other sites More sharing options...
DavidAM Posted June 28, 2013 Share Posted June 28, 2013 <option <?php sel($row_status['list_value'],$category)?> value=<?php echo $row_status['list_value']?> > <?php echo $row_status['list_value'] ?> </option> sel($row_status['list_value'],$category) returns a value, so you need to echo it. value=<?php echo $row_status['list_value']?> You need quotes around the value (for HTML). So: <option <?php echo sel($row_status['list_value'],$category)?> value="<?php echo $row_status['list_value'];?>" > <?php echo $row_status['list_value'] ?> </option>Usually, I use the ID as the value so the database lookup on the submission script uses the primary key. Quote Link to comment https://forums.phpfreaks.com/topic/279632-foreach-loop-not-displaying-values-correctly/#findComment-1438392 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.