Page 1 of 2

Setting values in an option function and the option

Posted: Thu Feb 24, 2005 10:25 am
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.

Re: Setting values in an option function and the option

Posted: Thu Feb 24, 2005 11:25 am
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>';

I need it to pull from two differnt tables

Posted: Thu Feb 24, 2005 12:43 pm
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 :)

Re: I need it to pull from two differnt tables

Posted: Thu Feb 24, 2005 1:31 pm
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].

WOOT!

Posted: Thu Feb 24, 2005 1:37 pm
by Deaglex
THANKS!!

Re: WOOT!

Posted: Thu Feb 24, 2005 4:01 pm
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:

Posted: Thu Feb 24, 2005 4:10 pm
by feyd
both of you, please post code using [syntax=php]tags while the[/syntax][syntax=php]tags are down.[/syntax]

THAT ROCKS TY.....ONE other thing

Posted: Thu Feb 24, 2005 8:47 pm
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.

THAT ROCKS TY.....ONE other thing

Posted: Thu Feb 24, 2005 8:48 pm
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...

Posted: Thu Feb 24, 2005 8:53 pm
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;

Posted: Thu Feb 24, 2005 9:13 pm
by Deaglex
WOOT!!

That worked Kick@$$!

Thanks!

Posted: Thu Feb 24, 2005 9:23 pm
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?

Posted: Thu Feb 24, 2005 9:31 pm
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.

Posted: Thu Feb 24, 2005 9:33 pm
by Burrito
but why are you using an array for it?

why not just <select name="di">

Posted: Thu Feb 24, 2005 9:49 pm
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