Jump to content

[SOLVED] Drop Down Menu Population


xfire

Recommended Posts

Hi,

 

I am trying to populate a drop down menu.  The information is coming from a MySQL database and PHP is used to populate it.  Now what I can do is populate it just fine, but that isn't going to help me for a contact edit page.  When the edit page loads I want the drop down menu to pull up automatically what is already in the row for that contact.

 

Here is the code I currently have:

 

$select_status = "SELECT c.char_id, c.status_id, s.status_name
		FROM CHARACTERS c, STATUS s
		WHERE c.char_id = '$c_id'
		AND c.status_id = s.status_id
		ORDER BY s.status_id";

$results_status = @mysql_query($select_status, $connection) or die(mysql_error());

while($row = mysql_fetch_array($results_status)) {
$s_id = $row['status_id'];
$s_name = $row['status_name'];
$display_status .= "<option value=$status_id>$status_name</option>";
} //End while

<td align="right"><strong>Status:</strong></td>
<td align="left"><select name="status_id" id="status_id">
                      <option>==Select==</option>
                        <? echo "$display_status"; ?>
                      </select></td>

 

Now nothing loads into the drop down menu.

 

Here is the code I know that works:

 

$select_status = 'SELECT status_id, status_name FROM STATUS ORDER BY status_id';

$results_status = @mysql_query($select_status, $connection) or die(mysql_error());

while($row = mysql_fetch_array($results_status)) {
        $status_id = $row['status_id'];
$status_name = $row['status_name'];
$display_status .= "<option value=$status_id>$status_name</option>";

<td align="left"><select name="status_id" id="status_id">
                      <option>==Select==</option>
                        <? echo "$display_status"; ?>
                      </select></td>
} //End while

 

Any help would be great.

 

Thanks.

Link to comment
Share on other sites

I actually got it to work.  Here is the code for those that are interested:

 

	<?
		include("../dbconn.php");

		$c_id = $_GET['char_id'];

		$select_character = "SELECT c.char_id, c.rank_id, c.status_id, c.platoon_id, c.comp_id, c.char_fname, c.char_lname, c.char_race, c.char_sex, c.char_position, c.char_position_short, c.char_email, c.char_url, r.rank_name, s.status_name, co.comp_name, p.platoon_name
							FROM CHARACTERS c, RANKS r, COMPANIES co, PLATOONS p, STATUS s
							WHERE char_id = '$c_id'
							AND c.status_id = s.status_id
							AND c.rank_id = r.rank_id
							AND c.comp_id = co.comp_id
							AND c.platoon_id = p.platoon_id
							ORDER BY r.rank_id DESC";						  
		$result = mysql_query($select_character, $connection) or die(mysql_error());

		while($row = mysql_fetch_array($result)) {
			$ra_id = $row['rank_id'];
			$st_id = $row['status_id'];
			$com_id = $row['comp_id'];
			$pl_id = $row['platoon_id'];
			$c_fname = $row['char_fname'];
			$c_lname = $row['char_lname'];
			$c_race = $row['char_race'];
			$c_sex = $row['char_sex'];
			$c_position = $row['char_position'];
			$c_position_short = $row['char_position_short'];
			$c_email = $row['char_email'];
			$c_url = $row['char_url'];
			$c_image = $row['char_image'];
			$ra_name = $row['rank_name'];
			$st_name = $row['status_name'];
			$com_name = $row['comp_name'];
			$pl_name = $row['platoon_name'];
		} //End while

		$select_rank = "SELECT r.rank_id, r.rank_name
						FROM RANKS r
						ORDER BY r.rank_id DESC";

		$select_status = "SELECT s.status_id, s.status_name
						FROM STATUS s
						ORDER BY s.status_id";

		$select_company = "SELECT co.comp_id, co.comp_name
						FROM COMPANIES co
						ORDER BY co.comp_id";

		$select_platoon = "SELECT p.platoon_id, p.platoon_name
						FROM PLATOONS p
						ORDER BY p.platoon_id";

		$results_rank = mysql_query($select_rank, $connection) or die(mysql_error());
		$results_status = mysql_query($select_status, $connection) or die(mysql_error());
		$results_company = mysql_query($select_company, $connection) or die(mysql_error());
		$results_platoon = mysql_query($select_platoon, $connection) or die(mysql_error());

		while($row = mysql_fetch_array($results_rank)) {
			$r_id = $row['rank_id'];
			$r_name = $row['rank_name'];
			if($r_id == $ra_id) {
				$display_rank .= "<option selected=$ra_id>$ra_name</option>";
			} //End If
			else {
				$display_rank .= "<option value=$r_id>$r_name</option>";
			}//End Else
		} //End while

		while($row = mysql_fetch_array($results_status)) {
			$s_id = $row['status_id'];
			$s_name = $row['status_name'];
			if($s_id == $st_id) {
				$display_status .= "<option selected=$st_id value=$st_id>$st_name</option>";
			} //End If
			else {
				$display_status .= "<option value=$s_id>$s_name</option>";
			}//End Else				
		} //End while

		while($row = mysql_fetch_array($results_company)) {
			$co_id = $row['comp_id'];
			$co_name = $row['comp_name'];
			if($co_id == $com_id) {
				$display_company .= "<option selected=$com_id value=$com_id>$com_name</option>";
			} //End If
			else {
				$display_company .= "<option value=$co_id>$co_name</option>";
			}//End Else	
		} //End while

		while($row = mysql_fetch_array($results_platoon)) {
			$p_id = $row['platoon_id'];
			$p_name = $row['platoon_name'];
			if($p_id == $pl_id) {
				$display_platoon .= "<option selected=$pl_id value=$pl_id>$pl_name</option>";
			} //End If
			else {
				$display_platoon .= "<option value=$p_id>$p_name</option>";
			}//End Else	

		} //End while
	?>

 

HTML:

                  <tr>
                    <td align="right"><strong>Company:</strong></td>
                    <td align="left"><select name="comp_id" id="comp_id">
                        <option>==Select==</option>
                        <? echo "$display_company"; ?>
                      </select>*</td>
                    <td align="right"><strong>Platoon:</strong></td>
                    <td align="left"><select name="platoon_id" id="platoon_id">
                        <option>==Select==</option>
                        <? echo "$display_platoon"; ?>
                      </select>*</td>
                  </tr>

                  <tr>
                    <td align="right"><strong>Rank:</strong></td>
                    <td align="left"><select name="rank_id" id="rank_id">
					<option>==Select==</option>
                        <? echo "$display_rank"; ?>
                      </select>*</td>
                    <td align="right"><strong>Status:</strong></td>
                    <td align="left"><select name="status_id" id="status_id">
                        <option>==Select==</option>
                        <? echo "$display_status"; ?>
                      </select>*</td>
                  </tr>

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.