Displaying field wise records from mysql by php? - php

Hi i have table in mysql like this:
to show this by php like this:
now first i get and print all gradeid on top by this
$qry = "select * from grademaster";
$result= mysql_query($qry,$link);
$nro=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$gradeid = $row['gradeid'];
echo "<th> $gradeid </th>";
$grdid[] = $gradeid; ////////takes all grades in array
}
and then getting all details from the below table
and but i can't able to show records like on top how can i show this plz help

Just copy and paste it instead of your code, then run script in browser.
You'll see the table. Prettify it via CSS and you are done.
I've tested it on my local server and it seems to work.
$qry = "select * from grademaster";
$result = mysql_query($qry, $link);
$nro = mysql_num_rows($result);
$table = array();
$rowNum = 0;
while ($row = mysql_fetch_array($result)) {
$table[$rowNum]['ordid'] = $row['ordid'];
$table[$rowNum]['orddate'] = date('m/d/Y', strtotime($row['orddate']));
$table[$rowNum]['1001'] = intval($row['gradeid']) == 1001 ? $row['ordqty'] : '';
$table[$rowNum]['1002'] = intval($row['gradeid']) == 1002 ? $row['ordqty'] : '';
$table[$rowNum]['1003'] = intval($row['gradeid']) == 1003 ? $row['ordqty'] : '';
$table[$rowNum]['1004'] = intval($row['gradeid']) == 1004 ? $row['ordqty'] : '';
$table[$rowNum]['1005'] = intval($row['gradeid']) == 1005 ? $row['ordqty'] : '';
$rowNum++;
}
?>
<table>
<tr>
<th>ordid</th>
<th>orddate</th>
<th>1001</th>
<th>1002</th>
<th>1003</th>
<th>1004</th>
<th>1005</th>
</tr>
<?php foreach ($table as $row): ?>
<tr>
<td><?php echo $row['ordid']; ?></td>
<td><?php echo $row['orddate']; ?></td>
<td><?php echo $row['1001']; ?></td>
<td><?php echo $row['1002']; ?></td>
<td><?php echo $row['1003']; ?></td>
<td><?php echo $row['1004']; ?></td>
<td><?php echo $row['1005']; ?></td>
</tr>
<?php endforeach; ?>
</table>

Related

echo first row of data into page title

I have a table of contacts and a search form the returns the contact searched for. All work fine as it should. But is it possible to show an instance of the first row only in a separate div or span at top of page? I have seen this done in MS Access, is it possible in php? Here is my search query but I have no idea how to echo or show the first row only into a title or heading div.
$sql = "SELECT * FROM people";
if( isset($_GET['search']) ){
$name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$name2 = mysqli_real_escape_string($con, htmlspecialchars($_GET['search2']));
$sql = "SELECT * FROM people WHERE first_name ='$name' AND last_name= '$name2'";
}
$result = $con->query($sql);
?>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['people_id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
</tr>
}
I just would do it like this (updated answer):
$tableData = [];
$divData = "";
while ($row = $result->fetch_assoc()) {
// empty tableData --> first row
if (empty($tableData)) {
// do your special for the first row here (div)
$divData = "hello world! ".$row['first_name']." ".$row['last_name'];
}
$tableData[] ="<tr>
<td>".$row['people_id']."</td>
<td>".$row['first_name']."</td>
<td>".$row['last_name']."</td>
<td>".$row['address']."</td>
<td>".$row['contact']."</td>
</tr>";
}
echo "<div>".$divData."</div>";
echo "<table>";
echo implode("\n", $tableData);
echo "</table>";

duplicate on my PDO query

Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins

Query works in phpMyAdmin but not PHP

In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!
You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?
You are getting this error because no rows return from this query.

How to showing report that group by archieve with PHP & MySQL?

I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>

Set proper conditional statement in WHILE LOOP

After submitting a mysqli_query to select open/unfulfilled orders from a cart database the results is output to the screen with a standard WHILE LOOP.
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
orders-open.php is simply the internal part of a TABLE:
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
So here is my question, I want to apply a simple HTML "<HR>" tag between the records at the point where the value in $row['Buyer'] changes, so by example:
John Doe 9/11/13 123456 Item 1 $5.99 5
John Doe 9/11/13 123654 Item 2 $8.99 3
John Doe 9/9/13 321456 Item 3 $4.99 2
(HR - Horizontal Rule Tag here)
Mike Doe 9/7/13 123555 Item 1 $9.99 2
Mike Doe 9/7/13 123777 Item 2 $2.99 6
What would be the best way to write the conditional statement inside the WHILE LOOP to compare the $row[Buyer'] result to the previous $row['Buyer'] result?
$first_run = TRUE;
$previous_buyer = NULL;
while($row = mysqli_fetch_array($oresult)) {
if($first_run) {
$first_run = FALSE;
$previous_buyer = $row['Buyer'];
}
include('orders-open.php');
}
And then in your include file:
<?php if($previous_buyer != $row['Buyer']) {
echo '<HR width="100%">';
$previous_buyer = $row['Buyer'];
} ?>
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
$prev_buyer = '';
while($row = mysqli_fetch_array($oresult))
{
if($prev_buyer !== $row['Buyer'])
{
//Do Something
}
include('orders-open.php');
$prev_buyer = $row['Buyer']
}
Just as Elon said above - change this:
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
to
$old_buyer = null;
while($row = mysqli_fetch_array($oresult)) {
if ($row['Buyer'] != $old_buyer) {
echo '<hr>';
}
$old_buyer = $row['Buyer'];
include('orders-open.php');
}
You could do it like this:
$buyer = null ; //Cache buyer in a variable, because $row is reset in the loop.
while($row = mysqli_fetch_array($oresult)) {
include('orders-open.php');
if ($buyer !== $row["Buyer"]){
echo ($buyer !== null) ? "<hr/>" : "" ;
$buyer = $row["Buyer"] ;
}
}

Categories