Jump to content

Calling results from first while loop in to second?


Kristoff1875
Go to solution Solved by DavidAM,

Recommended Posts

I have a form that has 2 select menus, I was hoping I could pre-populate the menus using the fields that exist in the table of the database, is this possible? I've created a dropdown, using the following:

	/* Get data. */
	$sql = "SELECT DISTINCT Area FROM Users ORDER BY Area ASC";
	$result = mysql_query($sql1); 

while($row = mysql_fetch_array($result))
		{
 '<option value="'.$row['Area'].'"> '.$row['Area'].' </option>';
			  }
With <select> tags wrapped around it works just how I wanted it to, apart from it not being involved with the second dropdown:

$sql1 = "SELECT * FROM Users WHERE UserID = $UserID";
$result1 = mysql_query($sql1); 

while($row1 = mysql_fetch_array($result1))
{

<select name="Area" id="Area" class="dropdown" style="width:260px;">
        <option> Any </option>
        <option <?php if ($row1['Area']=='London') { echo 'selected="SELECTED"'; } ?> value="London"> London </option>
        <option <?php if ($row1['Area']=='Birmingham') { echo 'selected="SELECTED"'; } ?> value="Birmingham"> Birmingham </option>
        </select> 
}
Is there a way of integrating the first result in to the second? Or even better, integrating them in to one query? I wasn't sure if that was possible as i'm searching for one row in the second query.

 

Thanks in advance.

Edited by Kristoff1875
Link to comment
Share on other sites

Sorry, yes I'd like to end up with the select box in the second query:
 

<select name="Area" id="Area" class="dropdown" style="width:260px;">
        <option> Any </option>
        <option <?php if ($row1['Area']=='London') { echo 'selected="SELECTED"'; } ?> value="London"> London </option>
        <option <?php if ($row1['Area']=='Birmingham') { echo 'selected="SELECTED"'; } ?> value="Birmingham"> Birmingham </option>
</select> 

With the results from the first, so:

<option <?php if ($row1['Area']=='***RESULT FROM QUERY 1***') { echo 'selected="SELECTED"'; } ?> value="***RESULT FROM QUERY 1***"> ***RESULT FROM QUERY 1*** </option>

To for the second if possible.

Link to comment
Share on other sites

  • Solution

Since you are only going to find ONE row in the second snippet, there is no need for a loop there. So, you can do the Area logic after retrieving that record:

 

$sql1 = "SELECT * FROM Users WHERE UserID = $UserID";
$result1 = mysql_query($sql1); 
$row1 = mysql_fetch_array($result1);

// Add area LOGIC HERE
I usually have a function for this purpose. Something like:

function getAreaOptions($psSelected = '') {
  $out = array();
  $sql = "SELECT DISTINCT Area FROM Users ORDER BY Area ASC";
  $result = mysql_query($sql);
  while($row = mysql_fetch_array($result)) {
    $out[] = sprintf('<OPTION %s value="%s">%s</OPTION>', 
        ($row['Area'] == $psSelected ? 'SELECTED="selected"' : ''),
        $row['Area'], $row['Area']);
  }

  return implode(PHP_EOL, $out);
}
Then call it from your user code:

$sql1 = "SELECT * FROM Users WHERE UserID = $UserID";
$result1 = mysql_query($sql1); 
$row1 = mysql_fetch_array($result1);

echo '<select name="Area" id="Area" class="dropdown" style="width:260px;">
        <option> Any </option>';
echo getAreaOptions($row1['Area']);
echo '</select>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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