I create table like this:
id | a1 | a2 | a3 | a | a4 | a5 | a6
1 | 3 | 1 | 7 | 63 | 3 | 5 | 0
2 | 3 | 1 | 7 | 35 | 3 | 5 | 0
3 | 3 | 1 | 7 | 40 | 3 | 5 | 0
4 | 0 | 0 | 0 | 00 | 0 | 0 | 0
5 | 1 | 5 | 5 | 44 | 2 | 2 | 2
6 | 5 | 6 | 9 | 07 | 5 | 5 | 7
7 | 5 | 6 | 9 | 07 | 5 | 5 | 7
8 | 1 | 5 | 0 | 64 | 2 | 5 | 7
9 | 5 | 6 | 7 | 84 | 1 | 3 | 0
10 | 1 | 2 | 4 | 74 | 1 | 3 | 0
11 | 4 | 5 | 0 | 96 | 1 | 2 | 3
I want to show this data on my relevant page. The format for the page should be like this:
<table>
<tr>
<th> a1 </br> a2 </br> a3</th>
<th> a </th>
<th> a4 </br> a5 </br> a6 </th>
.
.
.
.
</tr>
</table>
and after 7 columns from database or we can say after id divisible by 7, it should add </tr><tr> tag
Currently I am using:
<table width="40%" border="1" align="center" bordercolor="#000000" bgcolor="#99CC99">
<?php
$x=mysql_connect("localhost","dbuser","password");
if(!$x)
{
die("not connected");
}
mysql_select_db("mydb");
$q=mysql_query("select * from table");
while($r=mysql_fetch_array($q))
{?>
<td> <?php echo $r["a1"];?></br>
<?php echo $r["a2"];?></br>
<?php echo $r["a3"];?></td>
<td><?php echo $r["a"];?></td>
<td><?php echo $r["a4"];?></br>
<?php echo $r["a5"];?></br>
<?php echo $r["a6"];?></td>
<?php
}
?>
</table>
but it does not break row after 7 columns element
I think you are trying this
<table width="40%" border="1" align="center" bordercolor="#000000" bgcolor="#99CC99"><tr>
<?php
$inr=0;
$x=mysql_connect("localhost","dbuser","password");
if(!$x)
{
die("not connected");
}
mysql_select_db("mydb");
$q=mysql_query("select * from table");
while($r=mysql_fetch_array($q))
{?>
<td> <?php echo $r["a1"];?></br>
<?php echo $r["a2"];?></br>
<?php echo $r["a3"];?></td>
<td><?php echo $r["a"];?></td>
<td><?php echo $r["a4"];?></br>
<?php echo $r["a5"];?></br>
<?php echo $r["a6"];?></td>
<?php
$inr++;
if($inr%7==0)
echo"</tr><tr>";
}
?>
</tr></table>
$x=mysql_connect("localhost","dbuser","password");
if(!$x)
{
die("not connected");
}
mysql_select_db("mydb");
$q=mysql_query("select * from table");
$markup = '<table width="40%" border="1" align="center" while($r=mysql_fetch_array($q)) {
$headersPrinted = false;
bordercolor="#000000" bgcolor="#99CC99"><tr>
';
while ($r = mysql_fetch_array($q)) {
if (!$headersPrinted) {
foreach ($q as $key => $value) {
$markup .= "<th>$key</th>";
}
$markup .= "<tr>$markup</tr>";
$headersPrinted = true;
}
$markup .= '<tr>';
foreach ($q as $key => $value) {
$markup .= "<td>$value</td>";
}
$markup = $markup. '</tr>';
}
$markup .= '</table>';
echo $markup;
You should abort the printing of any HTML code before checking if you got a db connection.
Related
I'm editing a program in PHP and I want to display a table with data from different table.
Now I have 2 tables and I am only printing the first table (registro_eu) with this code:
$sql = "SELECT * FROM registro_eu WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<div class="table-responsive">';
echo '<table class="table table-striped">';
echo '<thead>
<tr>
<th>Date</th>
<th>Desc</th>
</tr>
</thead>
<tbody>';
while($row = $result->fetch_assoc()) {
echo '
<tr>
<td>'. $row["data"].'</td>
<td>'. $row["desc"].'</td>
</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
TABLE REGISTRO_EU:
ID | DATE | DESC
-----------------------------
1 | 2016-06-15 | TEST DESCT
2 | 2016-06-15 | TEST 2
3 | 2016-06-15 | TEST 3
4 | 2016-06-16 | TEST DESCT
5 | 2016-06-16 | TEST 4
6 | 2016-06-17 | TEST 5
TABLE FONDI:
ID | DATE | TOT
-----------------------------
1 | 2016-06-15 | 88
2 | 2016-06-16 | 100
3 | 2016-06-17 | 120
And I want to display to user (with php) something like this:
DATE | DESC |TOT
-------------------------------
2016-06-15 | TEST DESCT |
2016-06-15 | TEST 2 |
2016-06-15 | TEST 3 |88
2016-06-16 | TEST DESCT |
2016-06-16 | TEST 4 |100
2016-06-17 | TEST 4 |120
Use joins:
$sql = "SELECT * FROM registro_eu LEFT JOIN fondi ON registro_eu.DATE = fondi.DATE WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
you will have to disambiguate which table you're talking about in the WHERE clause but I don't know your schema so I'll leave that to you.
https://en.wikipedia.org/wiki/Join_(SQL)
I have a database which contain data and on which table column should this data be in and the other column should be empty
=====================
| data | column |
=====================
| orange | 1 |
| apple | 2 |
| banana | 6 |
=====================
for($i = 1;$i <= 7;$i++) {
$stmt->fetch();
echo ($i == $column ? '<td>' . $data . '</td>' : '<td></td>');
}
Output:
=========================================================
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
=========================================================
| orange | apple | | | | banana | |
=========================================================
So far, so good and all rows with column 1 filled is working fine
BUT!!!
=====================
| data | column |
=====================
| orange | 3 |
| apple | 4 |
| banana | 5 |
=====================
for($i = 1;$i <= 7;$i++) {
$stmt->fetch();
echo ($i == $column ? '<td>' . $data . '</td>' : '<td></td>');
}
Output:
========================================================
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
=========================================================
| | | | | banana | | |
========================================================
and all rows with column one is empty are not working properly !!!
also i want to repeat this multiple times
=============================================================
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
=============================================================
| orange | apple | | | | banana | |
=============================================================
| | | orange | apple | banana | | |
=============================================================
| | orange | | apple | | | banana |
=============================================================
thank you for you helping
edit
the complete code:
<?php
$query = "SELECT column, data FROM fruits";
if ($stmt = $conn->prepare($query)) {
$stmt->execute();
$stmt->bind_result($column, $data);
?>
<table border="1" style="background-color:#FFFFFF;border-collapse:collapse;border:1px solid #000000;color:#000000;width:100%; text-align: center; vertical-align: center" cellpadding="3" cellspacing="3">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<?php
for($i = 1;$i <= 7;$i++) {
$stmt->fetch();
echo ($i == $column ? '<td>' . $data . '</td>' : '<td></td>');
}
$stmt->close();
} else {
echo $conn->error;
}
$conn->close();
?>
</tr>
</table>
Try this,
/* prepare statement */
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2);
/* fetch values */
while ($stmt->fetch()) {
$myarray[$col1]=$col2;
}
for ($i=1; $i < 10; $i++) {
if(array_key_exists($i, $myarray)){
echo '<td>'. $myarray[$i] .'</td>';
}else{
echo '<td></td>';
}
}
/* close statement */
$stmt->close();
}
mysql> SELECT * FROM `db_depo`.`tb_item_inspection_report` a WHERE a.TIPE= 'T' LIMIT 1000;
+---------------------------------+-----------------------+------+--------------------+
| NAMA_ITEM_INSPECTION | NOMOR_ITEM_INSPECTION | TIPE | ID_ITEM_INSPECTION |
+---------------------------------+-----------------------+------+--------------------+
| Protection Box Cover | 1 | T | 1 |
| Manhole LID, Fastening Bolts | 2a | T | 2 |
| Manhole Gasket | 2b | T | 3 |
| PV Valve / Flame Trap / Gauge | 3a | T | 4 |
| Rupture Disc | 3b | T | 5 |
| Loading Port | 4a | T | 6 |
| Top Operated Valve | 5 | T | 7 |
| Dipstick | 6 | T | 8 |
| Air Line Valve (Ball Butterfly) | 7 | T | 9 |
| Calibration Chart | 8 | T | 10 |
| Walkway | 9 | T | 11 |
| Syphone Tube/Butterfly | 4b | T | 12 |
+---------------------------------+-----------------------+------+--------------------+
12 rows in set (0.00 sec)
MODEL
public function get_top_inspection_detail() {
$query = $this->db->query('SELECT a.* FROM `db_depo`.`tb_item_inspection_report` a where a.TIPE = "T" ORDER BY `ID_ITEM_INSPECTION` ASC LIMIT 1000;');
return $query;
}
CONTROLLER
public function menu_container() {
$this->load->library('csvreader');
$data = array('halaman' => 'Data Container',
'last_cargo' => $this->m_surveyor->get_all_last_cargo(), //BUAT LAST CARGO,
'top_item' => $this->m_surveyor_item_inspection->get_top_inspection_detail(),
'bottom_item' => $this->m_surveyor_item_inspection->get_bottom_inspection_detail(),
'detail_condition' => $this->m_surveyor_item_detail_inspection->get_all(),
);
$main_view = $this->load->view('surveyor/v_container', $data, TRUE);
echo $main_view;
}
VIEW
<table class="table ">
<thead>
<tr>
<th style="width: 70%">Item</th>
<th style="width: 20%">Kondisi</th>
<th style="width: 10%">Act</th>
</tr>
</thead>
<tbody>
<?php
$rows = $top_item->num_rows();
for ($j = 0; $j < $rows + 1; $j++) {
?>
<tr>
<td>
<?php
foreach ($top_item->result() as $v) {
echo $v->NAMA_ITEM_INSPECTION;
}
?>
</td>
<td>
<select class="form-control" name="list2_kondisi_1" id="list2_name_1">
<option>Choose...</option>
<?php
foreach ($detail_condition as $v) {
echo '<option value =' . $v->ID_ITEM . ' >' . $v->ALIAS . ' - ' . $v->NAME_ITEM . '</option>';
}
?>
</select>
</td>
<td><input type='checkbox'></td>
</tr>
<?php } ?>
</tbody>
</table>
I want to represent it into html table just like my table in mysql; Based my view code's, I got this :
Protection Box CoverManhole LID, Fastening BoltsManhole GasketPV Valve / Flame Trap / GaugeRupture DiscLoading PortTop Operated ValveDipstickAir Line Valve (Ball Butterfly)Calibration ChartWalkway
Protection Box CoverManhole LID, Fastening BoltsManhole GasketPV Valve / Flame Trap / GaugeRupture DiscLoading PortTop Operated ValveDipstickAir Line Valve (Ball Butterfly)Calibration ChartWalkway
UNTILL end of num_rows, How can I get into like my table in mysql coz foreach is read all the item into a row ?
By putting a foreach loop inside the td element, you are dumping all the values into a single cell. You need to loop at the row level - which you already are. Try:
<?php
$rows = $top_item->result();
foreach ($rows as $row) {
?>
<tr>
<td> <?php echo $row->NAMA_ITEM_INSPECTION; ?> </td>
and so on.
I use dompdf to create my pdf document.
My case is, I have to create a table based my two query on database.
The return of those two querys is array both.
First Array have 28 ELEMENT, and the second Array have 27 element. I want to interprated them like this
---------------------------------------------------------
| FIRST ARRAY | SECOND ARRAY |
---------------------------------------------------------
| 1 | 8 | 15 | 22 | 1 | 8 | 15 | 22 |
| 2 | 9 | 16 | 23 | 2 | 9 | 16 | 23 |
| 3 | 10 | 17 | 24 | 3 | 10 | 17 | 24 |
| 4 | 11 | 18 | 25 | 4 | 11 | 18 | 25 |
| 5 | 12 | 19 | 26 | 5 | 12 | 19 | 26 |
| 6 | 13 | 20 | 27 | 6 | 13 | 20 | 27 |
| 7 | 14 | 21 | 28 | 7 | 14 | 21 | |
---------------------------------------------------------
But now, with my html and the php code :
<tr>
<td colspan="4" align="center">FIRST ARRAY</td>
<td colspan="4" align="center">SECOND ARRAY</td>
</tr>
<?php
$i = 0;
foreach ($damage_codes as $row) : ?>
<?= ($i % 4 == 0) ? "<tr>" : false; ?>
<?= "<td>[".$row->DAMAGE_ID . "]" . $row->NAMA_DAMAGE . "</td>"; ?>
<?php
$i++;
endforeach;
echo "</tr>"
?>
<?php
$i = 0;
foreach ($repair_codes as $row) : ?>
<?= ($i % 4 == 0) ? "<tr>" : false; ?>
<?= "<td>[".$row->REPAIR_ID . "]" . $row->NAMA_REPAIR . "</td>"; ?>
<?php
$i++;
endforeach;
echo "</tr>"
?>
<tr>
And gives me result like this :
---------------------------------------------------------
| FIRST ARRAY | SECOND ARRAY |
---------------------------------------------------------
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 |
---------------------------------------------------------
How to get them like I wish, Any help it so appreciated.
Thanks.
UPDATED, some wrong type in result of my table. So, I updated my question. Sorry about that.
create two tables on td columns
<tr>
<td colspan="4" align="center">FIRST ARRAY</td>
<td colspan="4" align="center">SECOND ARRAY</td>
</tr>
<tr>
<td colspan="4" align="center">
<table>
<tr>
<?php
$i = 0;
foreach ($damage_codes as $row) : ?>
<?= ($i % 4 == 0) ? "</tr><tr>" : ""; ?>
<?= "<td>[".$row->DAMAGE_ID . "]" . $row->NAMA_DAMAGE . "</td>"; ?>
<?php
$i++;
endforeach;
?>
</tr>
</table>
</td>
<td colspan="4" align="center">
<table>
<tr>
<?php
$i = 0;
foreach ($repair_codes as $row) : ?>
<?= ($i % 4 == 0) ? "</tr><tr>" : ""; ?>
<?= "<td>[".$row->REPAIR_ID . "]" . $row->NAMA_REPAIR . "</td>"; ?>
<?php
$i++;
endforeach;
?>
</tr>
</table>
</td>
</tr>
How to Group two Similar fields in php?
I tried with GROUP BY DATE(bill.date) , bill.agent_id but it is not working for me
Table Structure 1: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: http://i.stack.imgur.com/38tKh.jpg (table name is bill )
Current Result
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 0 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 0 |
| Agent 1 | 0 | 0 | 12 | 0 | 0 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
| Agent 2 | 0 | 0 | 0 | 9 | 14 |
+---------+----+----+----+-----+----+
But I want To get Like The Following
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 12 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 14 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
+---------+----+----+----+-----+----+
Php Code pasted below that I am using now .
<table width="100%" border="1" cellspacing="4" cellpadding="1">
<tr>
<td> </td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php echo $i; ?></td>
<?php
}
?>
<td>Total</td>
</tr>
<?php
$query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
"FROM bill, bill_agents ".
"WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ".
"GROUP BY bill.agent_id , DATE(bill.date) ".
// "GROUP BY bill.agent_id , DATE(bill.date) ".
"ORDER BY bill.date ASC";
$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4)>0){
mysql_data_seek($result4, 0);
?>
<?php $total_1 = 0; while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
<?php $date = $row4['date'];
$var = $date;
$date = date("d-m-Y", strtotime($var) );
$date=substr($date, 0, -8);
echo $date;
?>
<tr>
<td><?php echo $row4['name']; ?></td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php if ($date == $i) echo $row4['total']; ?></td>
<?php
}
?>
<td></td>
</tr>
<?php } } ?>
<tr>
<td colspan="31"></td>
<td>Total</td>
<td></td>
</tr>
</table>
My understanding is, you need total amount processed by a agent and in a particular month of year.
Then change your query as follows,
<?php
$query = "SELECT b.date,b.agent_id,(SUM(b.amount) + SUM(b.cheque)) AS total,ba.id,ba.name
FROM bill_agent ba LEFT JOIN bill b ON b.agent_id = ba.id
WHERE YEAR(b.date) = YEAR(CURDATE()) AND MONTH(b.date) = MONTH(CURDATE())
GROUP BY b.date";
?>