I have a generate table like this:
Model# | Description | Height | Width | Length
Item 1 | standard | 5 | 3 | 1
Item 2 | special | | |
Item 3 | plastic | | |
So every item's height, width and length are the same. So I am trying to figure out how to merge the "height", "width" and "length" coz 3 items are all 5 x 3 x 1. Thanks!
This is the code:
function PrintProductTable($productResult, $productDetails, $headArr)
{
$productDetails = populteGeneralToDetail($productResult, $productDetails);
echo "<table class=\"chart\" >";
echo "<tr>";
for($i=0; $i<count($headArr); $i++)
echo "<th>".$headArr[$i]."</th>";
echo "</tr>";
foreach($productDetails as $modelName=>$productDetail)
{
echo "<tr>";
for($i=0; $i<count($headArr); $i++)
{
$col = $headArr[$i];
$value;
if (array_key_exists($col, $productDetail))
$value = $productDetail[$col];
else
$value = "" ;
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
}
Related
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>";
?>
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.
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>";
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>';
}
This is what I want to happen:
------------ ------------ ------------ ------------
| | | | | | | |
| image | | image | | image | | image |
| | | | | | | |
------------ ------------ ------------ ------------
Name Name Name Name
------------ ------------ ------------ ------------
| | | | | | | |
| image | | image | | image | | image |
| | | | | | | |
------------ ------------ ------------ ------------
Name Name Name Name
But this is what's happening:
------------ ------ ------------ ------ ------------ ------------
| | |name| | | |name| | | | |
| image | | | | image | | | | image | | image |
| | | | | | | | | | | |
------------ ------ ------------ ------ ------------ ------------
All of them are just in one row
Here is the code i game using right now
echo"<table border=1>";
while ($row = mysql_fetch_array($content))
{
echo "<td><img src='".$row['image']."' width='100'></td>";
echo"<td>" . $row['name'] . "</td>";
}
echo"</table>";
There might be 8 images at each forth image should be a new Line and under each image should be the name
What am I doing wrong?
Here is what you want to do. It supports any number of rows and outputs 4 at a time. If there are 15 items for example, it outputs 4 in first three rows and 3 in the last one.
echo"<table border=1>";
$images = array();
$names = array();
while ($row = mysql_fetch_array($content))
{
$images[] = $row['image'];
$names[] = $row['name'];
}
while(!empty($images))
{
echo "<tr>";
foreach($images as $count=>$image)
{
echo "<td><img src='".$image."' width='100'></td>";
if($count==3)
{
$images = array_slice($images, 4);
break;
}
}
echo "</tr><tr>";
foreach($names as $count=>$name)
{
echo"<td>" . $name . "</td>";
if($count==3)
{
$names = array_slice($names, 4);
break;
}
}
echo "</tr>";
}
echo"</table>";
Here is a shorter version which does the same:
echo"<table border=1>";
//get images and names in two arrays
$images = array();
$names = array();
while ($row = mysql_fetch_array($content))
{
$images[] = "<img src='".$row['image']."' width='100'>";
$names[] = $row['name'];
}
while(!empty($images))
{
//output images
foreach(array($images, $names) as $items)
{
echo "<tr>";
foreach($items as $key=>$item)
{
echo "<td>$item</td>";
//output only four of them
if($key==3)
{
break;
}
}
echo "</tr>";
}
//remove the first four images from $images because they're already printed
$images = array_slice($images, 4);
$names = array_slice($names, 4);
}
echo"</table>";
It's a little crude but it should work
// store the cells in arrays instead of echoing them
$images=$names=array();
while ($row = mysql_fetch_array($content)){
$images[]="<td><img src='".$row['image']."' width='100'></td>";
$names[]="<td>" . $row['name'] . "</td>";
}
// split them into 4 each. This will give $image_cells[0] as an array with the first 4 images
// and $image_cells[1] containing the latter 4
$image_cells=array_chunk($images, 4);
// same with $names
$name_cells=array_chunk($names, 4);
// echo the table
echo"<table border=1>";
// echo a table row, concat the cells stored as an array to a string using implode
echo '<tr>'. implode('', $image_cells[0]).'</tr>';
// and again but using $name_cells[0] which is the first 4 names
echo '<tr>'. implode('', $name_cells[0]).'</tr>';
// second set of images
echo '<tr>'. implode('', $image_cells[1]).'</tr>';
// second set of names
echo '<tr>'. implode('', $name_cells[1]).'</tr>';
echo"</table>";