abch624 Posted June 25, 2008 Share Posted June 25, 2008 Hi Guys I want to create a drop down list using the <td width="135"><select name="branch"><option value="<?php echo $row_branch['name']; ?>" selected=""><?php echo $row_branch['name']; ?></option></select></td> I want to create more option values from the database; I get all the branches from the database by $sql_branch_all="SELECT * FROM $tbl_branches"; $result_branch_all=mysql_query($sql_branch_all); $row_branch_all=mysql_fetch_array($result_branch_all); Please help... Link to comment https://forums.phpfreaks.com/topic/111835-solved-php-loop-through-sql-dataset/ Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 <td width="135"><select name="branch"> <?php $sql_branch_all="SELECT * FROM $tbl_branches"; $result_branch_all=mysql_query($sql_branch_all); while($row_branch_all = mysql_fetch_array($result_branch_all)){ echo '<option value="'.htmlspecialchars($row_branch['name']).'">'.htmlspecialchars($row_branch['name']).'</option>'; } ?> </select></td> Link to comment https://forums.phpfreaks.com/topic/111835-solved-php-loop-through-sql-dataset/#findComment-574071 Share on other sites More sharing options...
abch624 Posted June 25, 2008 Author Share Posted June 25, 2008 Hi There I did something Like this: <select name="branch"> <?php while($row_branch_all = mysql_fetch_array($result_branch_all)) {echo '<option value="'.htmlspecialchars($row_branch_all['name']).'">'.htmlspecialchars($row_branch_all['name']).' 'if (($row_branch['name'] == ($row_branch_all['name']) { echo 'selected=""';} '</option>';} ?> </select> If I remove the if statement it all works fine. But I need the if statement to make sure that the branch that the user is subscribed to is selected. When I try running the code as per above I get the error bellow: Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in C:\wamp\www\1STA\editprofile.php on line 406 And line 406 is the same as per above: i.e. {echo '<option value="'.htmlspecialchars($row_branch_all['name']).'">'.htmlspecialchars($row_branch_all['name']).' 'if (($row_branch['name'] == ($row_branch_all['name']) { echo 'selected=""';} '</option>';} Please help... Thanks Link to comment https://forums.phpfreaks.com/topic/111835-solved-php-loop-through-sql-dataset/#findComment-574110 Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 <td width="135"><select name="branch"> <?php $sql_branch_all="SELECT * FROM $tbl_branches"; $result_branch_all=mysql_query($sql_branch_all); while($row_branch_all = mysql_fetch_array($result_branch_all)){ $selected = $row_branch['name'] == $row_branch['name'] ? ' SELECTED' : ''; echo '<option value="'.htmlspecialchars($row_branch['name']).'"'.$selected.'>'.htmlspecialchars($row_branch['name']).'</option>'; } ?> </select></td> Link to comment https://forums.phpfreaks.com/topic/111835-solved-php-loop-through-sql-dataset/#findComment-574118 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.