Forum Moderators: coopster

Message Too Old, No Replies

formatting results returned from MySQL

         

bassata

2:33 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Can someone help me with this? I am running a query on a database and "echoing" my results in an HTML table. I have 4 fields per row; location, job_name, job_description, and effective_date. What I want to do, is display location, job_name, and effective_date; but I want the job_description to be a link from job_name. So if the user clicks on job_name, then another page will open with the matching job_description. Here is the code I have for displaying my results:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Displaying in an HTML table</title> </head>
<body>
<table border="1">
<tr>
<th>Location</th>
<th>Job Name</th>
<th>Job Description</th>
<th>Effective Date</th>
</tr>
<?php
//Include our login information
include('db_login.php');
// Connect
$connection = mysql_connect("localhost", "user", "pass");
if (!$connection){
die("Could not connect to the database:
<br />". mysql_error());
}
// Select the database
$db_select = mysql_select_db("mydatabase"); if (!$db_select){
die ("Could not select the database:
<br />". mysql_error());
}
// Assign the query
$query = "SELECT * FROM hr";
// Execute the query
$result = mysql_query($query);
if (!$result){
die ("Could not query the database:
<br />". mysql_error());
}

// Fetch and display the results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$location = $row["location"];
$job_name = $row["job_name"];
$job_description = $row['job_description'];
$effective_date = $row['effective_date'];
echo "<tr>";
echo "<td>$location</td>";
echo "<td>$job_name</td>";
echo "<td>$job_description</td>";
echo "<td>$effective_date</td>";
echo "</tr>";
}
// Close the connection
mysql_close($connection);
?>
</table>
</body>
</html>

d40sithui

4:08 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



So if the user clicks on job_name, then another page will open with the matching job_description.

If you want to open the description in another page, then you will likely have to run separate query on that page. Also, you will need to introduce another field that identifies each job to be unique (job_id). You will then use this id as the $_GET parameter as to retrieve the associated job_description on that new page.
You'd probably end up with something like this

$job_id = $row['job_id'];
echo "<td><a href=\"job_description.php?job_id=$job_id\">$job_name</a></td>";

where job_description is your new page with the job_id as the passing paramter. On this page, you'll run a query, something like

"SELECT job_description FROM hr WHERE job_id=".$_GET['job_id'];

bassata

4:36 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Awesome! Thank you......just one more question...and I apologize for being a nube....I know that it's now passing the data along to job_description.php, because I'm getting a unique job_description.php?id= , but it's not printing the "job_description"

Thanks again for all your help!

d40sithui

7:49 pm on Sep 18, 2008 (gmt 0)

10+ Year Member




I know that it's now passing the data along to job_description.php, because I'm getting a unique job_description.php?id= , but it's not printing the "job_description"

i think you may be misunderstanding me. it's not exactly passing the job_description along the URL, but the job_id(identifier) of the job that is selected which will be used to retrieve the job's description. if your table is set up to have these fields then you should find what you're looking for very easily. just query using the job_id (identifier).
job_id ¦ location ¦ job_name ¦ job_description ¦ effective_date

in job_description.php (or whatever file you want to call it) you will run a query that selects the "job description" using the job_id that is provided from the link on the previous page.

job_description.php


//header
//make database connection
//gets the job_id from $_GET
//queries job_id against db and get the job_description that is associated.
//displays the job_description/results from query

bassata

8:23 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Yea....it took a sec but I finally got it......thanks man....much appreciated.