Click to See Complete Forum and Search --> : Echo-ing data from table
focus310
08-01-2007, 01:09 PM
Hello:
I have a table which looks like this:
ID | client_id | event_date | event_type
1 | 4 | 08/01/2007 | SV
2 | 4 | 09/01/2007 | AD
All I need to do is display this information in an HTML format. Thus far, all I get is the last entry for that client.
How can I print both entries into a report?
Thanks for the help.
Detect
08-01-2007, 02:13 PM
Change the SQL statement. Make sure it doesn't have a LIMIT 1 in it. Make sure the function that gets the response has a loop in it to print out each row in the table.
TJ111
08-01-2007, 02:28 PM
First of all if you want any real help you'll have to post your source code (delete private information and database login stuff). It sounds like yuo are trying to fetch the results from a DB without any type of looping function, which by default only lists the last entry to match the query.
Assuming you have a table in your database with only the following fields "ID, client_ID, event_date, event_type":
$qry = "SELECT * FROM table_name_here";
/*WHERE client_id='".$_SESSION['client_id']."' if you want personal only*/
$result = mysql_query($qry, $db) or die(mysql_error());
echo "<table>
<tr>
<th>ID</th>
<th>Client Id</th>
<th>Event Type</th>
<th>Event Date</th></tr>";
while(list($ID,$client_id,$event_date,$event_type)=(mysql_fetch_array($result)))
{
echo " <tr>
<td>$ID</td>
<td>$event_date</td>
<td>$event_type</td></tr>";
}
echo "</table>";
focus310
08-01-2007, 05:42 PM
Hi,
This is my code.
This bit of code selects the client from a drop down box.
<?php
include('header_admin.html');
require_once('mysql_connect.php');
?>
<fieldset><legend>Please select a client</legend>
<form action="testing.php" method="post">
<p><label for="client_id">Client Name:</label>
<select name="client_id">
<option value="">- Please Select -</option>
<?php
$query="SELECT * FROM client";
$result = @mysql_query ($query) or die (mysql_error());
while($row=@mysql_fetch_array($result, MYSQL_ASSOC))
{
$client_id = $row['client_id'];
$client_name = $row['client_name'];
print '<option value="' . $client_id . "\" >" . $client_name . "</option>\n";
}
?>
</select>
</p>
<p><label for="blank"> </label><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>
This bit of code should produce the list for the chosen client.
<?php
require_once ('mysql_connect.php');
$query="SELECT client_name
FROM client
WHERE client_id = $_POST[client_id]";
$result = @mysql_query ($query) or die (mysql_error());
if ($result) {
while($row=@mysql_fetch_array($result, MYSQL_ASSOC))
{
$client_name = $row['client_name'];
}
}
$query9="SELECT * FROM progress_note WHERE client_id = $_POST[client_id]";
$result9 = @mysql_query ($query9) or die (mysql_error());
if ($result9) {
while($row9=@mysql_fetch_array($result9, MYSQL_ASSOC))
{
$progress_note_id = $row9['progress_note_id'];
$client_id = $row9['client_id'];
$event_date = $row9['event_date'];
$event_type = $row9['event_type'];
$event_entry = $row9['event_entry'];
$loc = $row9['loc'];
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link href="output_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
</div>
<div id="content">
<table class="apptable">
<tr>
<td class="j"><b>Client's Name:</b></td><td class="j"><?php echo $client_name; ?></td>
<td class="j"><b>LOC:</b></td><td class="j"><?php echo $loc; ?></td></tr>
</table>
<tr><td> </td></tr>
<table class="apptable">
<tr><th>Date</th><th scope="col">Type of Event</th><th scope="col">Entry</th></tr>
<tr><td class="d"><?php echo $event_date; ?></td><td class="d"><?php echo $event_type; ?></td><td class="d"><?php echo $event_entry; ?></td></tr>
</table>
I do have a loop going. I don't know why the result is only selecting one record for the client instead of all.
Thanks for the help.
Detect
08-01-2007, 09:47 PM
Hey...
You're right...you do have a loop, BUT since you don't print out anything inside the loop, every time it loops, it overwrites the variables with the latest row of information. So make sure you are printing out info inside your loop by turning it into a function and running the function below where the HTML table is, or just move your PHP code down to where the table is.
focus310
08-02-2007, 03:14 AM
Hi,
How do I include all the HTML code which has my reference to CSS coding?
How do I create a function to be able to print like this?
TJ111
08-02-2007, 07:34 AM
You can echo or print all your HTML information, just make sure you have your classes right. Remember PHP is executed on the server, and CSS and HTML are interpreted by the browser. The browser will only see the resulting HTML from your PHP function, so it has no idea it even existed. Going on your code, something like this (correcting for the out of place table tags).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link href="output_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
</div>
<div id="content">
<table class="apptable">
<?php
require_once ('mysql_connect.php');
$query="SELECT client_name
FROM client
WHERE client_id = $_POST[client_id]";
$result = @mysql_query ($query) or die (mysql_error());
if ($result) {
while($row=@mysql_fetch_array($result, MYSQL_ASSOC))
{
$client_name = $row['client_name'];
}
}
$query9="SELECT * FROM progress_note WHERE client_id = $_POST[client_id]";
$result9 = @mysql_query ($query9) or die (mysql_error());
$rowCheck = mysql_num_rows($result9);
//Checks to see if there's any entries
if ($rowCheck >= 1) {
//Might as well declare variables here, save time
while(list($progress_note_id,$client_id,$event_date,$event_type,$event_entry,$loc)=@mysql_fetch_arra y($result9,MYSQL_ASSOC))
{
?>
<tr>
<td class="j"><b>Client's Name:</b></td><td class="j"><?php echo $client_name; ?></td>
<td class="j"><b>LOC:</b></td><td class="j"><?php echo $loc;?> </td></tr>
<?php
}
echo "</table><table class='apptable'>";
while(list($progress_note_id,$client_id,$event_date,$event_type,$event_entry,$loc)=@mysql_fetch_arra y($result9,MYSQL_ASSOC))
{
?>
<tr><th>Date</th><th scope="col">Type of Event</th><th scope="col">Entry</th></tr>
<tr><td class="d"><?php echo $event_date; ?></td><td class="d"><?php echo $event_type; ?></td><td class="d"><?php echo $event_entry; ?></td></tr>
<?php
}
?>
</table>
It's sloppy, but shows the general idea.
focus310
08-02-2007, 08:02 AM
Hi,
I had to do some minor adjustments. It works great.
Thanks for the help.