Jump to content

[SOLVED] PHP loop through sql dataset


abch624

Recommended Posts

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

<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>

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

<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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.