Jump to content

MySQL DB to PHP Array to HTML Form to POST to SESSION and then...


Recommended Posts

I posted before, but I managed to figure out that problem... kind of. Now I have a related, yet entirely new and exciting problem. Here's the deal.

I query the DB like so:
$query = "SELECT jobName, jobPay, jobDescription FROM jobs WHERE numAbil <= {$abilities} ORDER BY jobName ASC";

I slap it into an array like so:
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\" value=\"{$data[0]}\"></td>";
print "\n</tr>";
}

What I'm trying to do here is get the jobName from the query to be the value for the radio button.

Then it's POSTed to the next page, where I assign it to a SESSION like so:
$_SESSION['job'] = $_POST['job'];

Then I try and get it to display like so:
<p align="center"><font size="2"><b>JOB</b>: <?php print $_SESSION['job']; ?>

The weird thing is, that all I get displayed is the first letter of the jobDescription, which I thought would be $data[2], but why only the first letter I can't begin to guess.

Any thoughts?

If you need more code or better description, please ask. Thanks.
[!--quoteo(post=367099:date=Apr 20 2006, 10:03 PM:name=freshrod)--][div class=\'quotetop\']QUOTE(freshrod @ Apr 20 2006, 10:03 PM) [snapback]367099[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I posted before, but I managed to figure out that problem... kind of. Now I have a related, yet entirely new and exciting problem. Here's the deal.

I query the DB like so:
$query = "SELECT jobName, jobPay, jobDescription FROM jobs WHERE numAbil <= {$abilities} ORDER BY jobName ASC";

I slap it into an array like so:
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\" value=\"{$data[0]}\"></td>";
print "\n</tr>";
}

What I'm trying to do here is get the jobName from the query to be the value for the radio button.


If you need more code or better description, please ask. Thanks.
[/quote]

My best guess is that you aren't accounting for the nature of mysql_fetch_row which returns a mixed array of indexed keys and associative keys, combined with the fact that you foreach this into a string, and then attempt to treat it like an array -- which is fine, but what you get is an array of characters, hence your one character output. But more than that, there's really no benefit to the obfuscation or the foreach, since you know exactly what columns you want to use, and where. I'd recommend you use instead mysql_fetch_assoc(). Your code would then be something like:

[code]
while ($row = mysql_fetch_assoc($result)) {
     print "\n<tr>";
    
    print "\n\t<td><center> {$row['jobName']} </center></td>";
    print "\n\t<td><input type=\"radio\" name=\"job\" value=\"{$row['jobName']}\"></td>";
    print "\n</tr>";
}        
[/code]
Thanks for the response gizmola.

I guess I didn't explain well enough what the page is supposed to be doing. I want the query to populate a form that lists out the available jobs based on the number of abilities. The form will display the jobName, jobPay, jobDescription, and then have a radio button with which they select which job they want. That much is working...at least it looks as if it's working.

Thanks for pointing out the difference between mysql_fetch_row() and mysql_fetch_assoc(). But even after changing that, it's still only displaying the first letter of the jobDescription.

Perhaps if you saw all the code it would help

[code]
<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>
[/code]

Any help is appreciated in advance.
AH-HA!!! I got it to work (of course getting it to work doesn't always mean I did it RIGHT, hahaha).

All I did was switch out where I had:

value="{$data[jobName]}"
for
value="{$row[jobName]}"

and it seems to be working.

Thanks for the help.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.