Setting values in an option function and the 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

Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

Setting values in an option function and the option

Post by Deaglex »

HELLO ALL,

I am new to PHP
I'm trying to set up an option that sets the value and the option data for my web site..
I am have have successfully taken the data from mySQL but I can't seem to set the proper function to

echo '<option value="$data1[]">$data2[]</option>'


Each set of data comes from seperate tables and are both arrays..
Thanks in advance.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Re: Setting values in an option function and the option

Post by bg »

Deaglex wrote:HELLO ALL,

I am new to PHP
I'm trying to set up an option that sets the value and the option data for my web site..
I am have have successfully taken the data from mySQL but I can't seem to set the proper function to

echo '<option value="$data1[]">$data2[]</option>'


Each set of data comes from seperate tables and are both arrays..
Thanks in advance.
might want to brush up on your html first. an option is defined like this

Code: Select all

<input type="radio" name="input_name" value="X" />
Also, looks like you might want to look up on how to use variables. You need to specify an index when working with an array if you want to grab a value from it. My guess is that you want your line of code to look like this:

Code: Select all

echo '<label for="this_radio">'.$data&#1111;2].'<input type="radio"  value="'.$data&#1111;1].'" id="this_radio" /></label>';
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

I need it to pull from two differnt tables

Post by Deaglex »

I'm using a this so far and I don't know how to get seperate values posted in the HTML

query = "SELECT CONCAT(driver_first_name, ' ' ,driver_last_name) AS name, driver_id FROM drivers ORDER BY driver_id ASC";
$result = @mysql_query ($query); //Run this query.
$nub= mysql_num_rows ($result);


if ($nub > 0) //If if ran OK display the records
{
echo "<select>";

while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "<option value=\"$row[0]\">$row[0]</option>";
}

I want the option value="" to be the driver_id
I don't want to have to redefine the driver's name as driver_id later in the script. Cuz that would be reduntant and a waste of time.

I should have clarified better,
THANKS :)
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Re: I need it to pull from two differnt tables

Post by bg »

Deaglex wrote:I'm using a this so far and I don't know how to get seperate values posted in the HTML

query = "SELECT CONCAT(driver_first_name, ' ' ,driver_last_name) AS name, driver_id FROM drivers ORDER BY driver_id ASC";
$result = @mysql_query ($query); //Run this query.
$nub= mysql_num_rows ($result);


if ($nub > 0) //If if ran OK display the records
{
echo "<select>";

while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "<option value="$row[0]">$row[0]</option>";
}

I want the option value="" to be the driver_id
I don't want to have to redefine the driver's name as driver_id later in the script. Cuz that would be reduntant and a waste of time.

I should have clarified better,
THANKS :)
Ahh, I should have realized what you were doing with the option tag. Anyway the driver id is stored in the $row array at index 1, $row[1]. The name is at index 0, $row[0].
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

WOOT!

Post by Deaglex »

THANKS!!
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Re: WOOT!

Post by bg »

Deaglex wrote:THANKS!!
not a problem. If you want to make your code more readable and managable, you should use the mysql_fetch_assoc instead of mysql_fetch_array. You wont need the second parameter (MYSQL_NUM), and then your array indexes will be associative, meaning instead of numerical they will coincide with the database field name. Like this:

Code: Select all

query = "SELECT CONCAT(driver_first_name, ' ' ,driver_last_name) AS name, driver_id FROM drivers ORDER BY driver_id ASC";
$result = @mysql_query ($query); //Run this query.
$nub= mysql_num_rows ($result);


if ($nub > 0) //If if ran OK display the records
&#123;
echo "<select>";

while ($row = mysql_fetch_assoc($result))
&#123;
echo '<option value="'.$row&#1111;'driver_id'].'">'.$row&#1111;'name'].'</option>';
&#125;

feyd | :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

both of you, please post code using [syntax=php]tags while the[/syntax][syntax=php]tags are down.[/syntax]
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

THAT ROCKS TY.....ONE other thing

Post by Deaglex »

Code: Select all

<select name="di&#1111;]">.........

while ($row = mysql_fetch_assoc($result, MYSQL_NUM))
		&#123;
		echo "<option name="$row&#1111;1]" >$row&#1111;0]</option>";
		&#125;
	&#125;
How do I get the di[] to have the selected array value. when it outputs now it only says Array.

I know it's any easy fix I just can't seem to put the right search in. Thanks again.
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

THAT ROCKS TY.....ONE other thing

Post by Deaglex »

Code: Select all

<select name="di&#1111;]">.........

while ($row = mysql_fetch_assoc($result,))
		&#123;
		echo "<option name="$row&#1111;driver_id]" >$row&#1111;name]</option>";
		&#125;
	&#125;
How do I get the di[] to have the selected array value. when it outputs now it only says Array.

I know it's any easy fix I just can't seem to put the right search in. Thanks again.

UPDATED CODE...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try using $row['name']...

edit: didn't read carefully enough

are you just trying to get the output if di[] on the form action page?

if so you're going to need to convert it from an array to a string

try

Code: Select all

$bob = implode(",",$_POST&#1111;'di']);
echo $bob;
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

Post by Deaglex »

WOOT!!

That worked Kick@$$!

Thanks!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

out of curiosity, why do you have that select in an array?

I assume you have more than on the form page in that array?
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

Post by Deaglex »

The driver_id corolates w/ the points table. The drivers name is pulled from the driver_info table.

I using it so I can select the driver's name (in a form) to output the driver_id in to an HTML table that relates to the driver's information.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

but why are you using an array for it?

why not just <select name="di">
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

Post by Deaglex »

That didn't work for some reason. I tried it.. It doesn't have value on the action page...

Code: Select all

select name="di" >
<? 
#####################################
#	Data Base Access For Driver		#
#	Names							#
#####################################
require_once ('connection.file'); //connect to DB
$dbc;
//set message to null

$message= NULL; 

// Checking driver data

$query = "SELECT CONCAT(driver_first_name, ' ' ,driver_last_name) AS name, driver_id FROM drivers ORDER BY driver_id ASC";
$result = @mysql_query ($query); //Run this query.
$nub= mysql_num_rows ($result); //Check if list is populated

if ($nub > 0) //If if ran OK display the records
	&#123;
	while ($row = mysql_fetch_array($result, MYSQL_NUM))
		&#123;
		echo "<option name="$row&#1111;1]" value"">$row&#1111;0]</option>";
I'm lost what I want is to select the drivers name and have it output the driver_id when submitted... when i implode the a ray it kicks out the driver's name how can I get it to output the driver_id after selecting the drivers name?
I know it's easy I just can't find where to get the info...

Thanks
Post Reply