Forum Moderators: coopster

Message Too Old, No Replies

Getting the value of a form input from a DB query?

         

freshrod

8:10 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



I'm new to PHP and for my first project (not copied out of a book) I'm attempting to tackle my own online game.

I've hit a snag where I want users to choose a job for their character. The jobs available to a character are determined by the characters breed, and the list is populated by a DB query. I have a radio button beside each available job, HOWEVER I don't know how to assign the specific job (the jobName for that $row) to the value for the radio button.

Any ideas?

Here's what I have so far:

<form id="jobs" name="jobs" action="char_menu.php" method="post">
<tr>
<td align="center" valign="middle" width="33%">
<?php

switch ($_SESSION['breed']) {
case 'Human':
$abilities = 3;
break;
case 'Altered':
$abilities = 2;
break;
case 'Mutant':
$abilities = 1;
break;
default:
echo "ERROR!";
}

function displayJobs ($result) {
print "<h3>Avalible Jobs for " . $_SESSION['breed'] . "</h3>\n";
print "\n<table>\n<tr>\n" .
"\n\t<th>Job Title</th>" .
"\n\t<th>Job Pay</th>" .
"\n\t<th>Job Description</th>" .
"\n\t<th>Select One</th>" .
"\n</tr>";

while ($row = @ mysql_fetch_row($result)) {
print "\n<tr>";

foreach ($row as $data)
print "\n\t<td><center> {$data} </center></td>";
print "\n\t<td><input type=\"radio\" name=\"job\"";
print "\n</tr>";
}
print "\n</table>\n";
}

$query = "SELECT jobName, jobPay, jobDescription FROM jobs WHERE numAbil <= {$abilities} ORDER BY jobName ASC";

displayJobs($result);
?>

gettopreacherman

8:24 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



When you query a $row or $data it is in the form of an array, to get specific values out you have to assign them like:

$job=$data[0];
$name=$data[1];

Depending on the order of the items you pulled in the query.

freshrod

2:02 pm on Apr 20, 2006 (gmt 0)

10+ Year Member



Thanks for the reply gettopreacherman.

But I'm still a litte confused. I guess I don't understand arrays as well as I thought I did...

So you are saying ( I think) that I need to assign a value to the jobName field of the DB query? Then I can use that for the value of the radio button?

Sorry... I'm a visual person. I usually have to see something to understand it.

gettopreacherman

3:02 pm on Apr 20, 2006 (gmt 0)

10+ Year Member



$sql = "Select name,address,city from [table]";
$result = msql_query($sql,[datbase link]);

$i=1;
$num = $mysql_numrows($result);

while ($i <= $num) {

$data = mysql_fetchrow($result);
$name = $data[0];
$address = $data[1];
$city = $data[2];

echo $name;
echo $address;
echo $city;
$i++;
}

This loops through each record. Each row is stored as an "array" when you query it:

$data = mysql_fetchrow($result);

you then have to pull out the data and put them in their own variables:

$name = $data[0];
$address = $data[1];
$city = $data[2];

Those variables are in the order in which you called them in the sql query:

Select name,address,city

Hope that helps!

freshrod

7:13 pm on Apr 20, 2006 (gmt 0)

10+ Year Member



AH-HA! I think I get it now.

Thanks for all your help!

freshrod

2:19 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



Ok... I got the script working... kind of.

The form displays, the buttons are there, you can select and submit, but then problems.

When I try and display the results I only get the first character of the jobDescription field. I thought maybe it was due to the mysql_fetch_rows() so I switched to mysql_fetch_assoc(), but that didn't help either.

Here is what I have now.

<form id="jobs" name="jobs" action="char_title.php" method="post">
<tr>
<td align="center" valign="middle" width="33%">
<?php

switch ($_SESSION['breed']) {
case 'Human':
$abilities = 3;
break;
case 'Altered':
$abilities = 2;
break;
case 'Mutant':
$abilities = 1;
break;
default:
echo "ERROR!";
}

function displayJobs ($result) {
print "<h3>Avalible Jobs for " . $_SESSION['breed'] . "</h3>\n";
print "\n<table>\n<tr>\n" .
"\n\t<th>Job Title</th>" .
"\n\t<th>Job Pay</th>" .
"\n\t<th>Job Description</th>" .
"\n\t<th>Select One</th>" .
"\n</tr>";

while ($row = @ mysql_fetch_assoc($result)) {
print "\n<tr>";

foreach ($row as $data)
print "\n\t<td><center> {$data} </center></td>";
print "\n\t<td><input type=\"radio\" name=\"job\" value=\"{$data[jobName]}\"></td>";
print "\n</tr>";
}
print "\n</table>\n";
}

$query = "SELECT jobName, jobPay, jobDescription FROM jobs WHERE numAbil <= {$abilities} ORDER BY jobName ASC";

if (!($connection = mysql_connect("localhost", "*****", "*****")))
die ("Cannot Connect to DB!");

if (!(mysql_select_db("*****", $connection)))
showerror();

if (!($result = mysql_query ($query, $connection)))
showerror();

displayJobs($result);

?>

</td>
</tr>
</tbody></table>
</center>
</div>

<p><center>
<input name="job_butt" value="LET IT BE SO!" type="submit">
</center></p>
</form>

Any help is appreciated in advance.