how to display data in two column with php with header - php

I have database
---------------------------------------------
no | name | code | grade
---------------------------------------------
1 | john | A1 | C
2 | john | A2 | D
3 | john | A3 | B
4 | tom | A1 | A
5 | john | A4 | A
6 | alice | A1 | C
7 | alice | A2 | D
8 | john | A5 | D
9 | john | A6 | C
---------------------------------------------
when I want show data with name john, I want result with 2 column:
---------------------------------------------------------------
no | name | code | grade | no | name | code | grade |
---------------------------------------------------------------
1 | john | A1 | C | 5 | john | A5 | D |
2 | john | A2 | D | 6 | john | A6 | C |
3 | john | A3 | B | | | | |
4 | john | A4 | A | | | | |
---------------------------------------------------------------
this my code that I already try
$result = mysql_query("select * from grade ");
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>";
$no = 1;
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
echo "<td>$no</td>";
echo "<td>$row[name]</td>";
echo "<td>$row[code]</td>";
echo "<td>$row[grade]</td>";
if ($count++ % 2 == 0)
{
echo "</tr><tr>";
$no++;
}
}
echo "</tr></table>";
But the result not what I want, this is the result when I run my code
no | name | code | grade | | | | |
---------------------------------------------------------------
1 | john | A1 | C | 1 | john | A4 | A |
2 | john | A2 | D | 2 | john | A5 | D |
3 | john | A3 | B | 3 | john | A6 | C |
---------------------------------------------------------------
Can somebody help me...thank you

This is representation problem, so it can be solved it two ways.
1. You can make this columns using modern HTML:
<section style="-webkit-column-count:2; -webkit-column-gap:15;">
<div class='columns'>
<table>
<tr>
<th>no</th>
<th>name</th>
<th>code</th>
<th>grade</th>
</tr>
<?php
$result = mysql_query("select * from grade ");
$no = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
echo "<td>{$row['no']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['code']}</td>";
echo "<td>{$row['grade']}</td>";
echo '</tr>';
$no++;
}
?>
</table>
</div>
</section>
2. If you want to stick to plain tables, you have to do some calculations:
First, you need to know full amount of grades you gonna show. You can get it with SQL query(more efficient way), I'll do it by preloading all data to array. HTML footer and header are omitted for brevity.
<?php
$data = array();
$result = mysql_query('SELECT * FROM grade;');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$data[] = $row;
}
$rows = (int) ceil(count($data)/2);
for ($i=0; $i < $rows; $i++) {
$no = $i + 1;
echo '<tr>';
echo "<td>{$no}</td>";
echo "<td>{$data[$i]['name']}</td>";
echo "<td>{$data[$i]['code']}</td>";
echo "<td>{$data[$i]['grade']}</td>";
if (array_key_exists($i + $rows, $data)) {
$no = $i + $rows + 1;
echo "<td>{$no}</td>";
echo "<td>{$data[$i + $rows]['name']}</td>";
echo "<td>{$data[$i + $rows]['code']}</td>";
echo "<td>{$data[$i + $rows]['grade']}</td>";
} else {
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
echo "</tr>\n";
}

I have not tested this nor checked for errors, but I think it is on the lines of what you are trying to accomplish.
$result = mysql_query("select * from grade ");
$orig_count = mysql_num_rows($result);
if ( $orig_count % 2 != 0 )
$count = $orig_count + 1;
$cols = $count / 2;
//draw header
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>"
for ( $i = 0; $i < $cols; i++ )
{
if ( $orig_count == 0 )
break; //zero results
//first column
echo "<td>$i</td>";
echo "<td>".mysql_result( $result, $i, "name" )."</td>";
echo "<td>".mysql_result( $result, $i, "code" )."</td>";
echo "<td>".mysql_result( $result, $i, "grade" )."</td>";
//second column
if ( $i > $orig_count )
{
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
else
{
echo "<td>".$i+($count/2)."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "name" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "code" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "grade" )."</td>";
}
}
//finish off
echo "</tr></table>";

Related

How to listout the item list for same order (in food order) and show in table using php and mysql? [duplicate]

I have written code in PHP (listed below,) but face a small problem...
<html>
<head>
<title> trans </title>
<body>
<style>
table, th, td {
border: 1px solid black;
width: 700px;
margin: auto;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE'";
$result = mysqli_query($con , $q1 ) ;
if($result){
echo "<table>";
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["term_no"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
When I run the code I see the following output:
---------------------------------------
| courses | terms | grades |
---------------------------------------
| CHEM 101 | 1 | A |
---------------------------------------
| ENGL 101 | 1 | C+ |
---------------------------------------
| PE 101 | 1 | B |
---------------------------------------
| PHYS 101 | 1 | F |
---------------------------------------
| ENGL 102 | 2 | B+ |
---------------------------------------
| PE 101 | 2 | B |
---------------------------------------
| PHYS 102 | 2 | D+ |
---------------------------------------
| MATH 201 | 3 | A |
---------------------------------------
| COE 200 | 3 | B |
---------------------------------------
But I do not want it like that. I want the term cells to become one, like this:
---------------------------------------
| courses | term | grade |
---------------------------------------
| CHEM 101 1 A |
| ENGL 101 C+ |
| PE 101 B |
| PHYS 101 F |
---------------------------------------
| ENGL 102 2 B+ |
| PE 101 B |
| PHYS 102 D+ |
---------------------------------------
| MATH 201 3 A |
| COE 200 B |
---------------------------------------
or in any other way that does not let term repeat, by which I mean: group the courses that have the same term together.
try this code,
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"]
);
}
echo '<table width="200" border="1">';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $data) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}else {
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "</table>";

Remove repeating data in a column

I am currently developing an ordering system where a customer can order many items. I also have an admin where he/she can see all the orders on that day. The Admin can view the name of the customer, the total payable, the products and the quantity of the product the customer have ordered.
I am currently seeing this results using my query.
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
Test | 165 | Tumbler | 1
Miguel | 525 | Keychain | 3
Miguel | 525 | Magic Mug | 3
Dandel | 1010 | keychain | 3
Dandel | 1010 | T-shirt | 2
Dandel | 1010 | Keychain | 3
Dandel | 1010 | Mug | 5
This is my query.
$result = mysql_query("
SELECT reservation.firstname, reservation.lastname, reservation.payable, reservation.city, orders.product, orders.qty, reservation.date
FROM orders
INNER JOIN reservation
ON orders.confirmation = reservation.confirmation
WHERE reservation.date = CURDATE() && reservation.city = '24th Floor'
");
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$row['firstname'].'</td>';
echo '<td>'.$row['payable'].'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
I want to get results like this. How can I do it?
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
| | Tumbler | 1
Miguel | 525 | Keychain | 3
| | Magic Mug | 3
Dandel | 1010 | keychain | 3
| | T-shirt | 2
| | Keychain | 3
| | Mug | 5
You can maintain some state while iterating which keeps track of whether the current row is a new name/payable:
$last_name_seen = NULL;
while ($row = mysql_fetch_array($result)) {
$firstname = "";
$payable = "";
if ($last_name_seen === NULL || $row['firstname'] != $last_name_seen) {
$last_name_seen = $row['firstname'];
$firstname = $row['firstname'];
$payable = $row['payable'];
}
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$firstname.'</td>';
echo '<td>'.$payable.'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
Note that your MySQL query should have some ORDER BY clause, to generate the ordering you want, e.g.
ORDER BY Name, Product;
As the comments above also suggest, if you are using a deprecated PHP API, you should consider upgrading to something more modern. But, the logic used in the above loop would not change much with changing the MySQL API.
You have to set flag for name & payable column & use array_column(),array_unique() to get unique column values :
<?php
$result = array(array("Name"=>"Test","Payable"=>'165',"Product"=>"keychain","Quantity"=>3),array("Name"=>"Test","Payable"=>'165',"Product"=>"Tumbler","Quantity"=>1));
$name_arr = array_unique(array_column($result, 'Name'));
$payable_arr = array_unique(array_column($result, 'Payable'));
$name_flag = 0;
$pay_flag = 0;
echo "<table border='1'>";
for($i=0;$i<count($name_arr);$i++)
{
for($j=0;$j<count($payable_arr);$j++)
{
foreach($result as $list)
{
if($list['Name'] == $name_arr[$i] && $list['Payable'] == $payable_arr[$j])
{
if($name_flag==0 && $pay_flag==0)
{
echo "<tr>";
echo "<td>".$name_arr[$i]."</td>";
echo "<td>".$payable_arr[$j]."</td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "<tr>";
}
$name_flag++;
$pay_flag++;
}
}
}
$name_flag=0;
$pay_flag=0;
}
echo "</table>";
?>

Displays the data array in the default table with looping

I have 1 array data may also be more and want to present it on the table, I tried to make the default table by 5 column, but when I try the same result 5 column all its data. should have 3 column 2 column is empty, what is lacking in my script?
This myscript
<?php
$no = 1;
for($x=1; $x<=5; $x++) {
foreach ($mydata as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
$no++;
}
}
?>
the results of the script
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | 001 | Paul | x |
3 | 001 | Paul | x |
4 | 001 | Paul | x |
5 | 001 | Paul | x |
I expected
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | | | |
3 | | | |
4 | | | |
5 | | | |
Try this:
<?php
$no = 1;
for ($x=1; $x <= 5; $x++) {
if (isset($mydata[$x-1])) {
$row = $mydata[$x-1];
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
} else {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
$no++;
}
?>
You need to check if the data is set based off $x var and then render empty row or populated.
You don't need nested loops. Just a single loop from 1 to 5, and each iteration shows the corresponding element of the array, or empty cells if there's no such element.
for ($x = 1; $x <= 5; $x++) {
echo "<tr>";
echo "<td>" . $x . "<td>";
if (isset($mydata[$x-1])) {
$row = $mydata[$x-1];
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
} else { // show empty fields
echo "<td></td><td></td><td></td>";
}
echo "</tr>";
}
There's also no need for separate variables $x and $no.

PHP: Troubles on indexing a nested for-loop

I am trying to print a 3-dimensional array into a table. But the indexes are kinda fked up. When I use the following (psuedo)code:
...
<<print headers and stuff>>
for ( $i = 0; $i < count( $array ); i++) {
$itemArray = $array[i];
for ( $j = 0; $j < count( $itemArray; j++) {
$innerItem = $itemArray[j];
echo <<tr start + both indexes in td>>
foreach ($innerItem as $spec) {
echo <<td with item>>
}
echo <<tr stop>>
}
}
In this example I am using i as index for the outer array and j as an index for the inner array (pretty obvious).
The result I am getting from this is as follows:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 1 | 0 | | |
| 2 | 0 | | |
| ... | ... | | |
Whilst I would expect:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 0 | 1 | | |
| 1 | 0 | | |
| 1 | 1 | | |
| 1 | 2 | | |
| 2 | 0 | | |
| ... | ... | | |
The (original) full code is:
echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
$battle = $this->combatLog[$battleIndex];
for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
$attack = $battle[$attackIndex];
echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
foreach ($attack as $stat) {
echo "<td>" . $stat . "</td>";
}
echo "</tr>";
}
}
echo "</tbody></table>";
What is going wrong?
Tested your code and runs as expected. You should do a echo '<pre>'.print_r($this->combatLog).'</pre>'; and debug the array contents.
Also I would recommend you the following:
1) You can use foreach instead of for, example: foreach ($this->combatLog as $battleIndex => $battle)
2) If you're not sure that a array contains values you should first do a: if (is_array($this->combatLog) && count($this->combatLog) > 0)
3) For simplicity and code maintenance I would first loop the multi-dimensional array and turn it into a one dimension called $attacks containing a array per each attack indexed by keys that you can recognize, ej:
$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);
Then you could define some columns for example:
$columns=array(
'Mon',
'Att',
'DungLVL',
'CharLVL',
'Health',
'Weapon',
'Potions',
);
Then print the table header like this:
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
And print rows like this:
foreach ($attacks as $attack) {
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$attack[$column].'</td>';
}
echo '</tr>';
}

Table UI View : Mysql - Php

My Table View :
id | packetId | fweight |
1 | 002 | 150 |
2 | 002 | 230 |
3 | 002 | 200 |
But i want My Table UI should be look a like below
:
id | packetId | fweight | sum(fweight)
1 | 002 | 150 |
2 | | 230 |
3 | | 200 | 580
I want to avoid that repeat (002) value , also fweight sum should be shown in another coloumn on last insert id.
ID - Primary id ,
How i can get this in Mysql , php .
QUERY What i Know till now:
"SELECT id, packetId , fweight from product where packetId="002" ";
Assuming you have all the header file/conn etc.
Try this:
$result = mysqli_query($con,"SELECT id, packetId , fweight from product where packetId='002'");
$tmp = "";
$ctr = 0;
$total = 0;
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
if($ctr == 0 || $tmp != $row['id'])
{
echo $row['packetId'];
}
echo "</td><td>";
echo $row['fweight'];
$total = $total + $row['fweight'];
echo "</td><td>";
if($tmp != $row['id'])
{
echo $total;
$total = 0;
$tmp = $row['id'];
}
echo "</td></tr>";
$ctr++;
}

Categories