0

Can anyone please assist how to retrieve data for dropdown menu from database. I have dropdown menu in mysettings.php of login script v2. The array i am using retrieves the all contents from the database for selected dropdown menu but it does not retrieve the specific saved data selection (dropdown menu) that user registered (register.php) in mysettings.php

Do I use the same mysql_query (rs_mysettings) for the dropdown menu or do I write a new code to query the database for the dropdown menu? If so can anyone please provide solution (query) for the dropdown menu. I am new to php and do not know how to do this.

Basically I want to pull saved data from database in mysettings.php that user saved from selection in dropdown menu at the time they registered their details.

My code for the dropdown menu in register.php

<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
<tr>
<td width="147">Acc Category<span class="required">*</span><br />
<select name="acc_category" id="acc_category" class="required">
<option value="" selected></option>
<option value="Bed & Breakfast">Bed & Breakfast</option>
<option value="Guest House">Guest House</option>
<option value="Selfcatering">Selfcatering</option>
<option value="Hotel">Hotel</option>
<option value="Lodge">Lodge</option>
<option value="Backpacker">Backpacker</option>
</select></td>
</tr>
</table>

------------------------------------------------------------------------------
I am using an array as provided by pbu to populate the selectboxes in form (mysettings.php) like so:

<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
<tr>
<td width="147">Acc Category<span class="required">*</span><br />
<select name="acc_category" id="acc_category" class="required">
<?php
//Array of accommodation categories for select box
$Categories = array("Bed & Breakfast","Guest House", "Selfcatering", "Hotel","Lodge","Backpacker" );
$dbacc_category = "Guest House"; //Stored in database
foreach ($Categories as $acc_category)
{
if($acc_category == $dbacc_category) {
echo "<option value=\"$acc_category\"SELECTED>$acc_category</option>";
} 
else
{
echo "<option value=\"$acc_category\">$acc_category</option>";
}
}
?>
</select></td>
</tr>
</table>

Any help is apprecieted.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by SikoSoft
0

Sorry, but I am a little unclear on what exactly your problem here is. What is the key issue you are facing? Is this code here not working? Do you want to convert this to storing the options in SQL? I didn't quite get what you meant.

A couple things that I can say off the top of my head though are as follows:

for one, I always give things shortened (usually integer) IDs for storage in the database. So for example, if I had options like "Bed & Breakfast", "Guest House", "Selfcatering", I would instead use an associative array and store the index in the database (this means less data is stored in the database, and makes checks much quicker, and allows you to change the text/wording of that value without effecting people who already selected it). So for example I would encourage you to do something like this instead:

$ops = array("1"=>"Bed & Breakfast", "2"=>"Guest House", "3"=>"Selfcatering", "4"=>"Hotel", "5"=>"Lodge", "6"=>"Backpacker);

To populate the list of options I would do this:

$selectmenu .= "<select name='category'>";
foreach ($ops as $o_i => $o_v)
	{
	$isSelected = "";
	if ($o_i == $uservalue)
		{
		$isSelected = " selected='selected'";
		}
	$selectmenu .= "<option value='{$o_i}'{$isSelected}>{$o_v}</option>";
	}
$selectmenu .= "</select>";

I would also be re-using the same code for both in situations when the user has selected a value already and otherwise, since one of the goals you should focus on when writing code is to re-use your code. You want to write as little as code as possible for one (since it usually implies elegance, and it saves you time, and allows you to easily fix many areas at once).

Anyhow, I wouldn't be surprised if what I've said so far is way off, so if you can clear up your precise problem I can try and help a bit more.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.