Forum Moderators: coopster
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);
?>
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.
$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!
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.