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"] ;
}
}
Related
I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }
I have wrote a select query using function but it is not returning data from MySQL database... can any one help me in this regard. I'm uploading screenshots.
Here is the name of my database, 'classified'
this is conn.php file
$conn = mysql_connect('localhost','root','');
echo mysql_select_db('classified',$conn);
function to fetch record from database, 'adds' table
On index.php i've wrote this code.
include 'functions/crud_functions.php'
$adds = get_all_adds();
foreach($adds as $add) {
<tr>
<td><?php echo $add['id']; ?></td>
<td><?php echo $add['ad_title'];?></td>
<td><?php echo $add['cat_id'];?></td>
<td><?php echo $add['ad_description'];?></td>
<td><?php echo $add['avatar'];?></td>
<td><?php echo $add['price'];?></td>
<td><?php echo $add['contact'];?></td>
<td><?php echo $add['name'];?></td>
<td><?php echo $add['time'];?></td>
<td>Edit | Del </td>
</tr>
but this is not displaying records.
Change :
if ($result) {
return TRUE;
} else {
return FALSE;
}
into this :
if ($result) {
return $data;
} else {
return array();
}
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++;
}
?>
I am trying to generate a student tabulation sheet. How can I display students details with ranking according to Marks & GPA(Descending). currently I can display by either Marks or GPA with the following code:
arsort($myGpa);
foreach($myGpa as $x=>$myValue)
{
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $myName[$x]; ?></td>
<td><?php echo $myCadet[$x]; ?></td>
<td><?php echo $myTotal[$x]; ?></td>
<td><?php echo $myGrade[$x]; ?></td>
<td><?php echo number_format($myValue,2); ?> </td>
<td><?php //$position++;
$myposition = $position++;
if($myposition == '1') { echo '1st'; }
else if($myposition == '2') { echo '2nd'; }
else if($myposition == '3') { echo '3rd'; }
else echo $myposition.'th';
?> </td>
</tr>
and another thing is: if the marks & gpa are same then how to show the position ?
thanks in advance.
actually.. I want to sort the students according to marks & gpa. currently my output is as follows:
Total Marks - Grade - GPA - Position
813 - A - 4.50 - 1
819 - A - 4.44 - 2
779 - A - 4.31 - 3
826 - A - 4.31 - 4
but as you see 2 students got same gpa(4.31) but different marks. I want the marks to be sorted when the gpa is same. that means if 2 students have same gpa then the position will be calculated according to marks.
currently I am using the following code(sort by GPA only)
$myName[] = $student_name['name'];
$myCadet[] = $stud['cadet_no'];
$myTotal[] = $all_total;
$myGrade[] = $final_grade;
$myGpa[] = $final_gpa;
arsort($myGpa);
foreach($myGpa as $x=>$myValue)
{
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $myName[$x]; ?></td>
<td><?php echo $myCadet[$x]; ?></td>
<td><?php echo $myTotal[$x]; ?></td>
<td><?php echo $myGrade[$x]; ?></td>
<td><?php echo number_format($myValue,2); ?> </td>
<td><?php //$position++;
$myposition = $position++;
if($myposition == '1') { echo '1st'; }
else if($myposition == '2') { echo '2nd'; }
else if($myposition == '3') { echo '3rd'; }
else echo $myposition.'th';
?> </td>
</tr>
}
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>