Pre-Loading Select/Option

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Pre-Loading Select/Option

Post by diseman »

I'm developing a business type website to learn PHP. Thank you in advance for any help you can provide...

I have a function that creates a drop-down menu and fills it with the names of companies found in the PARTNERS table. I also have a javascript onChange event that will post that selected company to a 'users' table in the 'partner_assigned' column. Works great, but how do I pre-load that saved data if it exists or let them select a company if nothing exists. Also, if company exists and is pre-loaded, the user might want to change the company to another, so that option has to be available too. Oye!

Here's my working (albeit not doing everything I want) function:

Code: Select all


function partners_dynamic_menu()  {

	include ("../_includes/_connect.php");

	$query = "SELECT company_name FROM partners ORDER BY company_name ASC";

	$result = mysqli_query($con, $query);

		echo "<form id=\"partner_assigned\" method=\"post\">  ";

		echo "<select id=\"partner_assigned\" name=\"partner_assigned\" class=\"dropmenu200\" onChange=\"selectChange(this.value)\" \">" ;
		echo "<option selected=\"selected\">Select Partner</option>";

			while($row = mysqli_fetch_array($result))	{

				echo "<option value=\"".$row['company_name']."\">".$row['company_name']."</option>" ;
			}

		echo "</select>";
		echo "</form>";
	}

Thank you!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pre-Loading Select/Option

Post by Celauran »

You've got all your concerns tangled together. That function is simply doing too much. Have a method that queries the database and returns a list of companies. That's its only job. Let your view/template iterate over those results and build up the select list for you. Separately, fetch the user data. As you're building your select, you can compare each item to that particular user's data.

Code: Select all

$partners = getPartnersList();
$user = getUserInfo($user_id);

...

<select name="whatever">
    <?php foreach ($partners as $partner): ?>
        <option value="<?= $partner->id; ?>" <?= $partner->id == $user->associated_partner ? 'selected="selected"' : ''; ?>>
            <?= $partner->name; ?>
        </option>
    <?php endforeach; ?>
</select>
Pseudo code, clearly
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Pre-Loading Select/Option

Post by diseman »

Thank you for your help. Late reply 'cause I do spend a lot of time reading and testing before asking for help.

Ok, so I have spent a lot of time trying to get close to what you were saying. Your OOP is confusing me in my procedural programming though.

Based on what you said and where I think you were going with it, I feel like I'm close with what I have, but maybe not. Where I'm at right now doesn't show me the PARTNERS->company_name, but instead the drop-down shows me the word "array" (twice) instead. I'm assuming twice because my db has two company_name in in the partners tbl, but not sure.

I feel like the problem is that I'm not properly getting the array data out of the query, which I know has 2 results.

Here's where I'm at after doing a lot of google'ing:

Code: Select all


$query_user = "SELECT partner_assigned FROM users WHERE username = '".$requestor."' ";

	$result_user = mysqli_query($con, $query_user) or die (mysqli_error($con));

	$query_partner = "SELECT company_name FROM partners ORDER BY company_name ASC";

	$result_partner = mysqli_query($con, $query_partner) or die (mysqli_error($con));

//$result_partner = array(); // not sure if I have to do this... I saw it on other examples and was testing it...

		echo "<form id=\"partner_assigned\" method=\"post\">";

		echo "<select id=\"partner_assigned\" name=\"partner_assigned\" class=\"dropmenu200\" onChange=\"selectChange(this.value)\" \">";

					foreach ($result_partner as $key => $val)	{

					print "$key = $val\n";

							if ($result_user == $val)  { // trying to match saved company_name in user tbl to avail. opt. in partners table

									echo "<option value='".$key."' selected='selected'>".$val."</option>";

								} else {

									echo "<option value=\"".$key."\">".$val."</option>";

								}

					}

		echo "</select>";
		echo "</form>";

be gentle! ;)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pre-Loading Select/Option

Post by Celauran »

You're really close. The problem is that the result set is a collection of arrays, so you'd need to reference $val['company_name'] rather than just $val
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Pre-Loading Select/Option

Post by diseman »

Thank you again.

I'm getting closer. Now, the only issue that remains is the value that currently exists in USER tbl is not returning when == (if ($val['company_name'] == $result_user)).

Code: Select all


	$query_user = "SELECT partner_assigned FROM users WHERE username = '".$requestor."' ";

	$result_user = mysqli_query($con, $query_user) or die (mysqli_error($con));

	$query_partner = "SELECT company_name FROM partners ORDER BY company_name ASC";

	$result_partner = mysqli_query($con, $query_partner) or die (mysqli_error($con));

//$result_partner = array();

		echo "<form id=\"partner_assigned\" method=\"post\">";

		echo "<select id=\"partner_assigned\" name=\"partner_assigned\" class=\"dropmenu200\" onChange=\"selectChange(this.value)\" \">";

					foreach ($result_partner as $key => $val)	{

							if ($val['company_name'] == $result_user)  {

									echo "<option value='".$key."' selected='selected'>".$val['company_name']."</option>";

								} else {

									echo "<option value=\"".$key."\">".$val['company_name']."</option>";

								}

					}

		echo "</select>";
		echo "</form>";

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pre-Loading Select/Option

Post by Celauran »

$result_user is a result set. You'll need to fetch the actual row out of it. http://php.net/manual/en/class.mysqli-result.php
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Pre-Loading Select/Option

Post by diseman »

Thanks for your help Celauran. I finally got it to work. This piece of my learning site turned out to be an excellent learning event on a few different levels!

So, rather than a function, which I started off with, I'm now just making this work as an include file.

Here's the code for anyone who might find it helpful:

Code: Select all


<?php

include("_connect.php");

	$query_user = "SELECT partner_assigned FROM users WHERE username = '".$requestor."' ";

	$result_user = mysqli_query($con, $query_user) or die (mysqli_error($con));

		while($row = mysqli_fetch_object($result_user))	{

			$default_select = $row->partner_assigned;

		}

	$query_partner = "SELECT company_name FROM partners ORDER BY company_name ASC";

	$result_partner = mysqli_query($con, $query_partner) or die (mysqli_error($con));

		echo "<form id=\"partner_assigned\" method=\"post\">";

		echo "<select id=\"partner_assigned\" name=\"partner_assigned\" class=\"dropmenu200\" onChange=\"selectChange(this.value)\" \">";

					foreach ($result_partner as $key => $val)	{

							if ($val['company_name'] == $default_select)  {

									echo "<option value='".$val['company_name']."' selected='selected'>".$val['company_name']."</option>";

								} else {

									echo "<option value=\"".$val['company_name']."\">".$val['company_name']."</option>";

								}

					}

		echo "</select>";
		echo "</form>";

?>

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pre-Loading Select/Option

Post by Celauran »

You could still wrap those two queries in functions and/or a class (not a requirement, just a suggestion). Also, make sure you're sanitizing input you're passing into queries. Better yet, look at using prepared statements.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Pre-Loading Select/Option

Post by diseman »

on other php page am using:

$partner_assigned = mysqli_real_escape_string($con, $_POST['partner_assigned']);

prepared statements is something I do need to look into. Been seeing a lot about it in my searchings. Just not to that level yet. :)
Post Reply