the below code shows a table with users to be accepted or declined. the code as it is has a dropdown on the end allowing to either accept it, deny it or leave as is. there is a button on the bottom to submit the form and after that there should be a php script that decides which user is accepted, denied or still pending.
what would be the best approach to make this work since i find it hard to link the dropdown box and the id.
<table align='center' width='90%'>
<tr>
<center><td><strong>ID:</strong></td></center>
<center><td><strong>Name:</strong></td></center>
<center><td><strong>Level:</strong></td></center>
<center><td><strong>Job:</strong></td></center>
<center><td><strong>Guild:</strong></td></center>
<center><td><strong>Date:</strong></td></center>
<center><td><strong>Action:</strong></td></center>
</tr>
<?php
$id = 0;
$result=mysql_query("SELECT * FROM accounts WHERE active ='0' ORDER BY date LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$id = $id + 1;
?>
<form method='POST' action=''>
<tr>
<center><td><?php echo $row['id']; ?></td></center>
<center><td><?php echo $row['user']; ?></td></center>
<center><td><?php echo $row['level']; ?></td></center>
<center><td><?php echo $row['job']; ?></td></center>
<center><td><?php echo $row['guild']; ?></td></center>
<center><td><?php echo $row['date']; ?></td></center>
<td>
<select name='action_<?php echo $row['id']; ?>'>
<option value='none'>None</option>
<option value='accept'>Accept</option>
<option value='decline'>Decline</option>
</select>
</td>
</tr>
<?php } ?>
<tr>
<br /><td align='right'><input type='submit' value='Submit' name='submit' /></td>
</tr>
</form>
</table>
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "accept" ) {
$acc = mysql_query("UPDATE `accounts` SET `active`='1' WHERE `id` = '".$row['id']."'");
echo "<meta http-equiv=refresh content=\"5; url=?wesofly=main&page=recruitment\">";
}elseif(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "decline" ) {
$dec = mysql_query("DELETE * from `accounts` WHERE '".$row['id']."'");
echo "<meta http-equiv=refresh content=\"0; url=?wesofly=main&page=recruitment\">";
}
}
?>
Your code is confusing. Your WHILE loop starts before the form tag, but the WHILE loop closes before the form tag closes. This means that you are going to have multiple form elements.
So, first of all, change that in your code. Next the way you want this to work is using arrays.
Your drop down box should look like this
<select name='action[<?php echo $row['id']; ?>]'>
<option value='none'>None</option>
<option value='accept'>Accept</option>
<option value='decline'>Decline</option>
</select>
Then on the PHP side, you should be able to do something like
foreach ($_REQUEST['action'] as $key => $value) {
$id = $key;
$status = $value;
// put your code here to update the specific database item
}
I think you'll be better off with HTML form arrays, for instance:
<select name="action[<?php echo $row['id']; ?>]">
<option value="none">None</option>
<option value="accept">Accept</option>
<option value="decline">Decline</option>
</select>
And inside your php code, you would simply loop through the submitted action field in your $_POST array to find which id had which action. Each entry would be presented as an array, the key would be the user's id.
Related
Iam trying a solution in PHP/MYSQL where in there is a some country list with a checkbox. Certain countries have something called entities which is a drop down which shows Micro, Small, Large as a dropdown. This entity dropdown appears for certain countries only. I am able to pass the checkbox checked values to next page. But i need to pass the value of the entity selected in the drop down also. Iam not able to acheieve that. My code ie posted below. Here i need to pass the value of the drop down (select - entity_selected) also to next page:
<?php
echo '<table border="1">';
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1["country"];
$entity1 = $row1["entity"];
echo '<tr>';
?>
<td><input name="check_list[]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?></td>
<td>
<?php
if($entity1 == 'Yes'){ ?>
<select class="form-control selectpicker" name="entity_selected">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php }
?>
</td>
<?php echo '</tr>'; } ?>
</table>
The Next page code is below to get the check box selected countries.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
How can i get the drop down values here? iam getting the checkbox (countries values correctly). Can anyone pls help me on this pls?
<form method="POST" action="nextpage.php">
<table border="1">
<?php
$i = 0;
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1['country'];
$entity1 = $row1['entity'];
echo "<tr>";
?>
<td>
<input name="check_list<?=$i ?>" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
</td>
<td>
<?php
if($entity1=="Yes") {
?>
<select class="form-control selectpicker" name="entity_selected<?=$i ?>">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php
};
?>
</td>
<?php
$i++;
echo "</tr>";
};
?>
</table>
</form>
nextpage.php:
<?php
$entities = $checklist = [];
foreach($_POST as $k => $v) {
if(preg_match("/^checklist(\d+)$/", $k, $matches))
$checklist[intval($matches[0])] = $v;
unset($matches);
if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
$entities[intval($matches[0])] = $v;
};
?>
So, I need to make sure this sets the right parameters to the DB when pressing the buttons. I just want to get the calls and comparisons right so it does what it should when I hit the buttons. There should be one delete button for each row from database, and update page when I press it.
It should be possible to update the text/numbers in the forms presented by MySQL by changing the forms and press Save-button, then refresh the page.
$counter = 1;
if(isset($_POST['save']) and something is changed from the forms compared to DB) {
Update MySQL
Refresh page
<script>
window.location.replace("thispage.php");
</script>
}
if(isset($_POST['del'])) {
DELETE MySQL
Refresh page
<script>
window.location.replace("thispage.php");
</script>
}
echo "<tr><td>ID</td><td>Namn</td><td>Platser</td><td>Fullbokad</td><td>Ta bort</td></tr>";
$sqlListSections = "SELECT * FROM avdelningar WHERE user = {$_SESSION['id']}";
$queryListSections = mysqli_query($mysqli, $sqlListSections);
$del = [];
while($rowListSections = mysqli_fetch_array($queryListSections))
{
if($counter%2)
{
echo "\n<tr bgcolor=#F1F1F2>\n\n";
}else
{
echo "\n<tr bgcolor=#FFFFFF>\n\n";
}
$counter++;
echo "
<td>".$rowListSections['id']."</td>
<td>
<input type=text value=".$rowListSections['namn']."></td>
<td>
<input type=text value=".$rowListSections['platser']."></td>
<td>";
if($rowListSections['prio'] == 1)
{
echo "<select name=platser>
<option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
<option value='0'>0</option>".$rowListSections['prio'];
}elseif($rowListSections['prio'] == 0)
{
echo "<select name=platser>
<option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
<option value='1'>1</option>".$rowListSections['prio'];
}
echo "</td>
<td>
<form method=post action=thispage.php>
<input type=submit value=Delete name=del>";
</td>
</form>
</tr>";
}
echo "<form method=post action=thispage.php>
<input type=submit value=Save name=save>";
`
in your checkbox change naming as array.
<input type=checkbox name="del[]" value={$rowListSections['id']}>
like
echo $rowListSections["id"].' '.$rowListSections["namn"].' '.$rowListSections["platser"].' '.
$rowListSections["prio"].'';
and in your if(isset($_POST)) you can get a del array so you can loop this array like below.
foreach($del as $val){
$id = $val;
$sql_query_for_update = "update table_name set field = 1 where id= '$id' ";
}
UPDATE TO Below:
I changed the code a bit and am having a bit of success but I am still off on one thing. I changed the line
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
to
<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
Now it shows the correct select name and actually saves a changed value in the database however, instead of saving the name id number, it saves the actual name. a print_r of $row gives me:
Array ( [0] => 4 [id] => 4 [1] => 6 [brokername] => 6 [2] => Kims Boat [boatname] => Kims Boat )
The correct brokerlist.id for that selected user is 6 but instead of saving 6 to the boatlist.name it save the brokerlist.name field instead of the brokerlist.id.
What can I change to have it save the brokerlist.id as opposed to the physical name? :)
Thank you in advance!
(here is the new edit page code just in case:
<?php
include_once('db.php');
if(isset($_GET['edit']))
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM boatlist WHERE id='$id'");
$row= mysql_fetch_array($res);
$brokerlistquery = mysql_query("SELECT * FROM brokerlist");
print_r($row);
}
if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))
{
$newbrokername = $_POST['newbrokername'];
$newboatname = $_POST['newboatname'];
$id= $_POST['id'];
$sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";
$res= mysql_query($sql) or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";
}
?>
<table class="centerwithroom">
<form action="testeditboat.php" method="POST">
<tr>
<td>New Broker Name </td><td>
<select name='newbrokername'>
<?php
while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
?>
<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>
<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>
</tr>
<td></td><td><input type="submit" value=" Update "/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>
</form>
</table>
(ORIG POST Starts Here)
This is my first post and I will try to be as thorough as possible. This is just a rudimentary form and I have not added any security in it at all as I am just trying to make it work, then get to the rest :) Being new to programming my code may look bad to some of you experts and I would love any examples you could give.
On to the project. I have two mysql tables, one called brokerlist with two fields id and name. the second table is called boatlist with three fields id, brokername, and boatname. The brokername field contains the value of the brokerlist.id field and a text entered boatname.
I have created php add forms to add data to both tables that are working properly. I have created a php edit form to edit the brokerlist table that works properly. My problem lies in the form I call testeditboat.php. What I am trying to do is allow the user to change the field brokername in the testeditboat.php form with a dropdown box that populates from the mysql table brokerlist but also uses the select selected of the current entry.
Where my form currently stands is it submits the request and will change any value in the boatname field, but will not update the brokername field.
Thank you all in advance very much for your help, I hope I have been descriptive enough.
The code for my testeditboat.php form is as follows:
<?php
include_once('db.php');
if(isset($_GET['edit']))
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM boatlist WHERE id='$id'");
$row= mysql_fetch_array($res);
$brokerlistquery = mysql_query("SELECT * FROM brokerlist");
}
if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))
{
$newbrokername = $_POST['newbrokername'];
$newboatname = $_POST['newboatname'];
$id= $_POST['id'];
$sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";
$res= mysql_query($sql) or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";
}
?>
<table class="centerwithroom">
<form action="testeditboat.php" method="POST">
<tr>
<td>New Broker Name </td><td>
<select name='newbrokername'>
<?php
while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
?>
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>
<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>
</tr>
<td></td><td><input type="submit" value=" Update "/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>
</form>
</table>
Change the following line:
$row= mysql_fetch_assoc($res); //this fetches the array in associative mode
Change the following line:
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[name]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
You where comparing $row[brokername] == $brokerlistrow[id] which seems to be incorrect. You have to compare $row[brokername] == $brokerlistrow[name]
Note: Stop using mysql_* extensions use mysqli_ instead*
by changing the row to
<option value="<?php echo $brokerlistrow[id]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
it now saved the correct value and shows the correct focus in the edit dropdown box.
Thank you all for your guidance! :)
I have a table that prints out all available cameras. It uses a form to change these settings. The problem is that the form only updates the last camera in the entry. In other words if I change the form and hit "Apply" for the last camera in the list it will work. If I change the form for any other camera in this list it changes the one to have the same settings as the last camera in the list. There are no issues with any values as far as I can tell.
Sorry for the long dump here, but without being able to narrow down the problem I thought I should include the bulk of it:
// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();
$query_camera_name = "SELECT camera_id, camera_name, camera_status, camera_quality, camera_hash, camera_type FROM #__cameras WHERE user_id=".$user->id." AND camera_status!='DELETED'";
$db->setQuery($query_camera_name);
//get number of cameras so we can build the table accordingly
$db->query();
$num_rows = $db->getNumRows();
// We can use array names with loadAssocList.
$result_cameras = $db->loadAssocList();
if (isset($_POST['apply_changes'])) {
//process changes to camera options
$camera_id = $_POST['camera_id'];
$camera_status = check_input($_POST['camera_status']);
$camera_name = check_input($_POST['camera_name'], "You entered an empty camera name. Enter another name and apply changes.");
$camera_quality = check_input($_POST['camera_quality']);
$query_insert_camera = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'", `camera_name` ="'.$camera_name.'", `camera_quality` ="'.$camera_quality.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_insert_camera);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']);
}
echo "<html>";
echo "<head>";
<link href="dashboard/webcam_widget.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function oncameraSubmit(camera_id)
{
document.active_cameras.camera_id.value = camera_id;
return confirm('Apply changes?');
}
</script>
<?php
echo "</head>";
echo "<body>";
if (!isset($result_cameras))
{
//TODO
}
else
{
if ($num_rows == 0)
{
echo '<b><i><center>You currently have no cameras setup. Add a Camera below.</center></i></b>';
}
else
{
?>
<form name="active_cameras" action="<?php htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="hidden" name="camera_id" value="" />
<table id="webcam-table">
<thead>
<tr>
<th>Camera Type</th>
<th>Name</th>
<th>Quality</th>
<th>Status</th>
<th>Camera Actions</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_rows;$i++)
{
//camera_status
if ($result_cameras[$i]["camera_status"] == "ENABLED")
{
$enabled_option = "value='ENABLED' selected='selected'";
$disabled_option = "value='DISABLED'";
}
else
{
$enabled_option = "value='ENABLED'";
$disabled_option = "value='DISABLED' selected='selected'";
}
//camera_quality
if ($result_cameras[$i]["camera_quality"] == "HIGH")
{
$high_option = "value='HIGH' selected='selected'";
$medium_option = "value='MEDIUM'";
$mobile_option = "value='MOBILE'";
}
else if ($result_cameras[$i]["camera_quality"] == "MEDIUM")
{
$high_option = "value='HIGH'";
$medium_option = "value='MEDIUM' selected='selected'";
$mobile_option = "value='MOBILE'";
}
else if ($result_cameras[$i]["camera_quality"] == "MOBILE")
{
$high_option = "value='HIGH'";
$medium_option = "value='MEDIUM'";
$mobile_option = "value='MOBILE' selected='selected'";
}
else
{
//TODO proper logging
}
//camera_type
if ($result_cameras[$i]["camera_type"] == "WEBCAM")
{
$webcam = "value='WEBCAM' selected='selected'";
$axis = "value='AXIS'";
$other = "value='IPCAM'";
}
else if ($result_cameras[$i]["camera_type"] == "AXIS")
{
$webcam = "value='WEBCAM'";
$axis = "value='AXIS' selected='selected'";
$other = "value='IPCAM'";
}
else if ($result_cameras[$i]["camera_type"] == "IPCAM")
{
$webcam = "value='WEBCAM'";
$axis = "value='AXIS'";
$other = "value='IPCAM' selected='selected'";
}
else
{
//TODO
}
?>
<tr>
<td>
<select name="camera_type">
<option <?php echo $webcam; ?>>Webcam</option>
<option <?php echo $axis; ?>>AXIS</option>
<option <?php echo $other; ?>>Other</option>
</select>
</td>
<td>
<input type="text" size="32" maxlength="64" name="camera_name" value="<?php echo $result_cameras[$i]["camera_name"]; ?>" />
</td>
<td>
<select name="camera_quality">
<option <?php echo $high_option; ?>>High</option>
<option <?php echo $medium_option; ?>>Medium</option>
<option <?php echo $mobile_option; ?>>Mobile</option>
</select>
</td>
<td>
<select name="camera_status">
<option <?php echo $enabled_option; ?>>Enabled</option>
<option <?php echo $disabled_option; ?>>Disabled</option>
</select>
</td>
<td>
<input type="submit" name="apply_changes" value="Apply" onClick="javascript:return oncameraSubmit(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>
</td>
</tr>
<?php
}
echo "</tbody>";
echo "</table>";
echo "</form>";
}
}
It looks like you have multiple HTML elements with the same name. As such, you want to get back an array of values when the form is posted.
As such, Get $_POST from multiple checkboxes looks like it might be helpful.
Alternatively, extend oncameraSubmit so that it stores all the data in a hidden input field (not just the id). Then when you update the database, use these hidden fields.
Your form element names are clashing. When you define a form element e.g. 'camera_status' twice, you will only receive the last value in the POST.
Use form array notation, e.g.: "camera_status[]" or even better "camera_status[$id]". Then your PHP code will recieve arrays as POST data and you will be able to update everything at once.
This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
I need the values of form inputs to be populated by the sql database. My code works great for all text and textarea inputs but I can't figure out how to assign the database value to the drop down lists eg. 'Type of property' below. It revolves around getting the 'option selected' to represent the value held in the database.
Here is my code:
$result = $db->sql_query("SELECT * FROM ".$prefix."_users WHERE userid='$userid'");
$row = $db->sql_fetchrow($result);
echo "<center><font class=\"title\">"._CHANGE_MY_INFORMATION."</font></center><br>\n";
echo "<center>".All." ".fields." ".must." ".be." ".filled."
<form name=\"EditMyInfoForm\" method=\"POST\" action=\"users.php\" enctype=\"multipart/form-data\">
<table align=\"center\" border=\"0\" width=\"720\" id=\"table1\" cellpadding=\"2\" bordercolor=\"#C0C0C0\">
<tr>
<td align=\"right\">".Telephone." :</td>
<td>
<input type=\"text\" name=\"telephone\" size=\"27\" value=\"$row[telephone]\"> Inc. dialing codes
</td>
</tr>
<tr>
<td align=\"right\">".Type." ".of." ".property." ".required." :</td>
<td>Select from list:
<select name=\"req_type\" value=\"$row[req_type]\">
<option>House</option>
<option>Bungalow</option>
<option>Flat/Apartment</option>
<option>Studio</option>
<option>Villa</option>
<option>Any</option>
</select>
</td>
</tr>
....
using your current code:
<?php
$options = array('House', 'Bungalow', 'Flat/Apartment', 'Studio', 'Villa', 'Any');
foreach($options as $option) {
if ($option == $row['req_type']) {
print '<option selected="selected">'.$option.'</option>'."\n";
} else {
print '<option>'.$option.'</option>'."\n";
}
}
?>
assuming $row['req_type'] is one of the values in $options. i strongly suggest quoting your array elements (ie $row['req_type'] instead of $row[req_type]. the latter method generates errors under error_reporting(E_ALL)
you also might want to look at resources such as phpbuilder.com. some articles are pretty outdated, but will provide you with some basics on how to better structure your code. (for example, separating your HTML from your PHP code woulud help readiability in this sitaution a lot).
edit: as per the comments, any information displayed should be escaped properly (see htmlentities() for example).
Nigel,
See the answer with the check mark next to it. It go me start in solving my problem. First I select my data from the table, then I select the data i want display in my dropdown menu. If the data from the primary table matches the data from the dropdown table selected is echoed.
$query9 = "SELECT *
FROM vehicles
WHERE VID = '".$VID."'
";
$result9=mysql_query($query9);
while($row9 = mysql_fetch_array($result9)){
$vdate=$row9['DateBid'];
$vmodelid=$row9['ModelID'];
$vMileage=$row9['Mileage'];
$vHighBid=$row9['HighBid'];
$vPurchased=$row9['Purchased'];
$vDamage=$row9['Damage'];
$vNotes=$row9['Notes'];
$vKBBH=$row9['KBBH'];
$vKBBM=$row9['KBBM'];
$vKBBL=$row9['KBBL'];
$vKBBR=$row9['KBBR'];
$vYID=$row9['YID'];
$vMID=$row9['MID'];
$vModelID=$row9['ModelID'];
$vELID=$row9['ELID'];
$vECID=$row9['ECID'];
$vEFID=$row9['EFID'];
$vColorID=$row9['ColorID'];
$vRID=$row9['RID'];
$vFID=$row9['FID'];
$vDID=$row9['DID'];
$vTID=$row9['TID'];
}
$query1 = "SELECT * FROM year ORDER BY Year ASC";
$result1=mysql_query($query1);
echo "<select name='Year'>";
while($row1 = mysql_fetch_array($result1)){
echo "<option value = '{$row1['YID']}'";
if ($vYID == $row1['YID'])
echo "selected = 'selected'";
echo ">{$row1['Year']}</option>";
}
echo "</select>";
May not be efficient, but it's simple & gets the job done.
$dropdown = str_replace("<option value=\"".$row[column]."\">","<option value=\"".$row[column]."\" selected>",$dropdown);
Thanks Owen. Your code seems perfect but gave lots of php errors. I simplified it to this but just get whitespace as options:
<td>Select from list: <select name=\"req_type\">
$option = array('House', 'Bungalow', 'Flat/Apartment', 'Studio', 'Villa', 'Any');
foreach($option as $option) {
if ($option == $row[req_type]) {
<option selected>$option</option>}
else {
<option>$option</option>}};
</select></td>
<?php
$result = $db->sql_query("SELECT * FROM ".$prefix."_users WHERE userid='$userid'");
$row = $db->sql_fetchrow($result);
// options defined here
$options = array('House', 'Bungalow', 'Flat/Apartment', 'Studio', 'Villa', 'Any');
?>
<center><font class="title"><?php echo _CHANGE_MY_INFORMATION; ?></font></center><br />
<center> All fields must be filled </center>
<form name="EditMyInfoForm" method="POST" action="users.php" enctype="multipart/form-data">
<table align="center" border="0" width="720" id="table1" cellpadding="2" bordercolor="#C0C0C0">
<tr>
<td align="right">Telephone :</td>
<td>
<input type="text" name="telephone" size="27" value="<?php echo htmlentities($row['telephone']); ?>" /> Inc. dialing codes
</td>
</tr>
<tr>
<td align="right">Type of property required :</td>
<td>Select from list:
<select name="req_type">
<?php foreach($options as $option): ?>
<option value="<?php echo $option; ?>" <?php if($row['req_type'] == $option) echo 'selected="selected"'; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
...
oops.. a little too carried away.
Thanks everyone for all your help. Although I couldn't get any one answer to work it gave me ideas and I have accomplished the job! I ended up going about it a long-winded way but hey-ho if it's not broke don't fix it.
if ($row[$req_type]=='House'){
$sel_req_type1='selected';
}
else if ($row[$req_type]=='Bugalow'){
$sel_req_type2='selected';
}
etc. etc. etc.
And in the table:
<option $sel_req_type1>House</option>
<option $sel_req_type2>Bulgalow</option>
etc. etc. etc.
I still have other issues to overcome i.e. htmlentities for the text fields and quoting the array elements (ie $row['req_type'] instead of $row[req_type]. But I'll ask that on another question. Thanks again
Here is another way of going about it.
<?php
$query = "SELECT * FROM ".$prefix."_users WHERE userid='$userid'";
$result = mysql_query($query); ?>
$options = array('House', 'Bungalow', 'Flat/Apartment', 'Studio', 'Villa', 'Any');
<select name="venue">
<?php
while($row = mysql_fetch_array($result)) {
print '<option value="'.$option.'"';
if($row['req_type'] == $option) {
print 'selected';
}
print '>'.$option.'</option>';
}
?>
</select>