Populating table from database - php

I am trying to populate a table with the query results I receive from a function. I am able to populate the table however my table header keeps repeating in each row. Is the any way I can stop this from happening?

You need to put table header outside of your while loop:
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
echo '</table>';

because your table's head part is inside of loop
you should do like this
for example we get book names and writer's name :
<table>
<thead>
<th>book></th>
<th>writer</th>
</thead>
<tbody>
<?php
while($row = mysql_fetch_assoc($get5star)){
echo'
<tr><td> '.$row['book']. '</td><td>'.$row['writer']. '</td> </tr>
';
}
?>
</tbody>
</table>

however my table header keeps repeating in each row
That's because it's inside the loop. The general structure of a loop is:
(stuff that happens once)
while (some condition) {
(stuff that happens many times)
}
(stuff that happens once)
If the UI table header should happen only once (which it should), then it needs to go in one of those (stuff that happens once) locations. In this case, the first one:
// happens once
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
// happens many times
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
// happens once
echo '</table>';

You have the table head code inside the loop:
while($row = mysql_fetch_assoc($get5star)){
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
You should put that outside the while loop, like this:
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
echo '</table>';
Worth noticing that you shouldnt use mysql , to access the Database. Use PDO ( i prefer them ) or mysqli .

Related

How to make a separate table for separate student in the view file

I have students in database and their fee table. i am getting all fee's of students submitted within two dates. Currently i am showing each result in a row. some student have multiple records as they have submitted multiple fee's inbetween those days. But i want to show each same student's data in seperate table.
This is Current Design Pic
I want these tables in design Pic
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Voucher</th>
<th>Name</th>
<th>Amount</th>
<th>Net Amount</th>
<th>Month</th>
<th>Issue Date</th>
<th>Due Date</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$paidcount = 0;
$unpaidcount = 0;
$unpaidamount = 0;
$totalsum = 0;
$payablesum = 0;
$count = 1;
foreach ($this->vouchers as $key => $voucher) { ?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $voucher['id']; ?></td>
<td><?php echo ucwords($voucher['name']); ?></td>
<td>
<?php
$totalsum+=$voucher['total'];
echo $voucher['total'];
?>
</td>
<td>
<?php
$payablesum+=$voucher['payable'];
echo $voucher['payable'];
?>
</td>
<td>
<?php echo date("F", mktime(0, 0, 0, $voucher['id_month'], 10)); ?>
</td>
<td><?php echo $voucher['issue_date']; ?></td>
<td><?php echo $voucher['due_date']; ?></td>
<td>
<?php if($voucher['paid']==1) {
$paidcount+=$voucher['paid'];
echo "Paid";
} else {
$unpaidamount = $voucher['payable'];
$unpaidcount++;
echo "Pending";
} ?>
</td>
<td class="text-center">
<span class="glyphicon glyphicon-info-sign"></span>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="2"><strong>Total Vouchers: </strong><?php echo --$count; ?>
</td>
<td colspan="1"><strong>Received: </strong><?php echo $paidcount; ?>
</td>
<td colspan="1"><strong>Unpaid: </strong><?php echo $unpaidcount; ?>
</td>
<td colspan="2"><strong>Total Amount: </strong><?php echo $totalsum; ?>
</td>
<td colspan="2"><strong>Paid Amount: </strong><?php echo $payablesum; ?>
</td>
<td colspan="2"><strong>Pending Amount: </strong><?php echo $unpaidamount; ?>
</td>
</tr>
</tbody>
</table>
try it (i don't test it yet, so tell me edit code if there is any typical mistake)
we just use one php block
instead collecting all of state values in single variable, we have our arrays which collects state of each name inside the separate index of their array
for each voucher, we have a table row element and push it to an array in named_rows[$voucher['name']]
at last, we have another loop to print our tables, pleas read comments inside the code
<!-- you shoul use only one php block <?php ?> -->
<?php
$paidcount = Array(); // use arrays instead to separate each value for only unique name
$unpaidcount = Array();
$unpaidamount = Array();
$totalsum = Array();
$payablesum = Array();
$count = 1;
$name_count = 0;
// this array holds all of the rows of each table by "name"
$named_rows = Array();
// keep an instance of table for future use
$table_tag_start = '
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Voucher</th>
<th>Name</th>
<th>Amount</th>
<th>Net Amount</th>
<th>Month</th>
<th>Issue Date</th>
<th>Due Date</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>';
$table_tag_end = '
</tbody>
</table>';
// now search and separate each name then create new table for each one
foreach ($this->vouchers as $key => $voucher)
{
if (!array_key_exists($voucher['name'], $named_rows)) {
$named_rows[$voucher['name']] = new Array();
}
$totalsum[$voucher['name']] += $voucher['total'];
$payablesum[$voucher['name']] += $voucher['payable'];
if($voucher['paid']==1) {
$paidcount[$voucher['name']] += $voucher['paid'];
}
else {
$unpaidamount[$voucher['name']] = $voucher['payable'];
$unpaidcount[$voucher['name']]++;
}
$row = '<tr>
<td>' . ($count++) . '</td>
<td>' . $voucher['id'] . '</td>
<td>' . ucwords($voucher['name']) . '</td>
<td>' . $voucher['total'] . '</td>
<td>' . $voucher['payable'] . '</td>
<td>' . date("F", mktime(0, 0, 0, $voucher['id_month'], 10)) . '</td>
<td>' . $voucher['issue_date'] . '</td>
<td>' . $voucher['due_date'] . '</td>
<td>' . (($voucher['paid']==1)?'Paid':'Pending') . '</td>
<td class="text-center"><a href="' . SITEURL . 'vouchersinfo/?action=voucherDetails&id_voucher=' . $voucher['id'] .'" title="View Voucher Details">
<span class="glyphicon glyphicon-info-sign"></span></a></td>
</tr>';
array_push($named_rows[$voucher['name']], $row);
}
// for each name, stored in "$named_rows" create a new table and for each row inside "$named_rows[current_name]" create rows
// when rows are ended, then generate the last row (sum displays) as a new special row, and close the table
foreach ($named_rows as $key1 => $value1)
{
$output = $table_tag_start;
foreach ($named_rows[$key1] as $value2)
{
$output .= $value2;
}
$output .= '<tr>
<td colspan="2"><strong>Total Vouchers: </strong>'.(--$count).'</td>
<td colspan="1"><strong>Received: </strong>'.$paidcount[$key1].'</td>
<td colspan="1"><strong>Unpaid: </strong>'.$unpaidcount[$key1].'</td>
<td colspan="2"><strong>Total Amount: </strong>'.$totalsum[$key1].'</td>
<td colspan="2"><strong>Paid Amount: </strong>'.$payablesum[$key1].'</td>
<td colspan="2"><strong>Pending Amount: </strong>'.$unpaidamount[$key1].'</td>
</tr>';
$output .= $table_tag_end;
echo $output;
}
?>

Fixed column width with changing no. of columns

I have a horizontal table that have a changing number of columns.
How do I set a fixed column width? I have tried to set width in the main table setup and in the tr and td tags. No luck so far.
Any suggestions?
Here is my code
...mysql query
while ($row = mysql_fetch_assoc($result))
{
$file_no .= '<td>'.$row['File_no'].'</td>';
$user .= '<td>'.$row['User'].'</td>';
$movement .='<td>'.$row['Downloaded']. " &nbsp " .$row['Uploaded'].'</td>';
$i++;
}
echo '
<table border="1">
<tbody>
<tr align="center">
<td align="left"><b>File no.</b></td>'.$file_no .'
</tr>
<tr align="center">
<td align="left"><b>User</b></td>'.$user .'
</tr>
<tr align="center">
<td align="left"><b>Movement</b></td>'.$movement .'
</tr>
</tbody>
</table>
';

MYSQL Output - HTML Table

I have a query to output data within my MYSQL database and would like it to display in a table. With "Name" and "UserID" as the headings... currently the code I have developed, does output the results but instead creates multiple instances of the table and doesn't create new rows.
Please could someone help me achieve my goal?
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
</table>";
The Table should be defined outside of the loop of the data, you just loop through the data and add a row on every loop :
echo "<table width='400' border='1'> ";
echo "<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
// Example of while loop :
while ($row = mysql_fetch_array($results)) {
echo" <tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
If the code you have shown is within some kind of while loop that loops through the database results, it should be clearly evident why you get a bunch of one row tables. Your entire table output would happen with each loop execution. What you should be doing is outputting your table opening tag and header row before you start the data retrieval loop. Output only the row with each loop execution, and then output the table closing tag after you exit the loop.
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while ($row = $result->fetch_assoc()) { // or whatever your retrieval code is
echo "<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
You need to echo the table open tag and headers then loop through the database results, then echo the table closing tag.
You should put <table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
before loop (
<HERE>
while() {
...
}
AND </table> you should put after cycle...
it should look like this:
echo "
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while(....) {
echo "
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
";
}
echo "</table>";
http://www.siteground.com/tutorials/php-mysql/display_table_data.htm
Use this inside the loop instead
echo"
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
and put the rest of the table wrapping around the loop.
Don't complicate the mission ; you don't need to do loop ; mysql provides option whichi is --html , just use it :
mysql --html -uroot -e "USE mydb;select * from mytable"
if you want to display only : FullName and UserID :
mysql --html -uroot -e "USE mydb;select FullName,UserID from mytable"

PHP Dynamically returning multidimensional arrays

I have been doing a lot of work in learning how to return multidimensional arrays dynamically- but what I can't seem to figure out is how to nest them.
I have two tables, each has the identical format: ID, name.
Table one: SSC
- sscid
- sscname
Table two: SRV
- srvid
- srvname
What I am trying to do is print all of the items in table two under EACH item in the table one list.
The table one items are the headers, the table two items are returned as a checkbox (with the srvid as the value) and label(srvname).
I can get it to all print together, but it is a. one giant list of results and it's in a
| checkbox | table 1: name | table 2: name | format.
Not pretty at all (although it is progress for me to get this far).
After I run my query and get the result, my code looks like this:
Now, I've had a few additional thoughts about the design of the concept re:the database tables go, but everything I read indicates that they really need to be on their own tables, and they should be able to be referenced by the key from one table and the key from the other (eventually ended up in a joint table with user ID references) Because they are numerically indexed, I don't know why this would be an issue for me; however I simply can't seem to get this to work properly.
I should mention that when I alter the code to try to make the ssc_name span 2 cols and make it more like a header, it returns a header row for each checkbox/srv row, instead of for all of the checkbox/srv rows.
if($result) {
echo '<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
<tr><th colspan="2"><h3>Options</h3></th></tr>
<tr><td></td><td align="left"><b>Services</b></td></tr>';
$numfields = mysql_num_fields($result);
$data = array();
$flist = array();
for($i=0;$i<$numfields;$i++)$flist[] = mysql_field_name($result,$i);
$data[0] = $flist;
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
echo '<tr><td colspan="2" align="center"><b>' . $row['ssc_name'] .'</b><td></tr>
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>
<td align="left">' . $row['ssvname'] . '</td>
</tr>';
}
echo '</table>';
}
Can anyone help me figure this out, please?
You are missing a > on this line
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>
Should be
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" /></td>
Something you might be able to pick up on with better formatting...
<?php
if ($result) {
echo <<<EOD
<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
<tr>
<th colspan="2"><h3>Options</h3></th>
</tr>
<tr>
<td></td>
<td align="left"><b>Services</b></td>
</tr>
EOD;
while ($row = mysql_fetch_assoc($result)) {
echo <<<EOD
<tr>
<td colspan="2" align="center"><b>{$row['ssc_name']}</b><td>
</tr>
<tr>
<td align="center"><input type="checkbox" value="{$row['ssv_id']}" /></td>
<td align="left">{$row['ssvname']}</td>
</tr>
EOD;
}
echo '</table>';
}
?>
And I'm not really sure what business any of those arrays have being in there.

Loop through html table (for each tr)

I need to be able to loop through an HTML table and output data. In each <tr> there is 8 td's. The first td is a dropdown menu of engineers. The next 7 tds are days of the week with drop downs for time slots.
I'm basically building a scheduler output for a specific internal app. (that's irrelevant here).
So, here's an example table:
<table width="200" border="1">
<tr>
<th scope="col">Engineer</th>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
<tr>
<td>John Doe</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Jane Doe</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Each td has a form element and each tr is a "section" of data. In the end I will have 20+ tr's and need to be able to run through each tr and grab the relavant data from these, however, I need to be able to iterate through each tr so that I can manage the code better.
Is there a way to do this with PHP?
echo '<table width="100%" border="0"><tr>';
echo '<td width="20px"></td>';
echo '<td align="left"><strong>Title</strong></td>';
echo '<td align="center" width="125px"><strong>Posted</strong></td>';
$sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM `announcement` ORDER BY `id` DESC LIMIT '.$search['start'].', '.$search['max'];
$rows = $mysql_conn->fetch_array($sql);
foreach($rows as $key=>$record) {
echo (($key+1)%2) ? '<tr bgcolor="#AEDEFF" >' : '<tr>';
echo '<td align="left"><input class="checkbox" type="checkbox" name="delete[]" id="delete[]" value="'.$record["id"].'" /></td>';
echo '<td align="left">'. $record["title"] .'</td>';
echo '<td align="center">'.$record["datetime"].'</td></tr>';
}
echo '</table>';
Example of what I use when I want to output a list of rows with columns of data. Not sure if this is what you want but it works. :)

Categories