I have search and implements from many question in stackoverflow to my code but it always returning only the last row value.
Here my code :
$totalpayment = 0;
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$totalpayment = '$ ' . number_format($totalpayment) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
How to fix this ?
You are overding $totalpayment on every loop by this code:
$totalpayment = '$ ' . number_format($totalpayment) ;
$totalpayment = 0;
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$totalpayment = '$ ' . number_format($totalpayment); //Try transferring this code outside the loop
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
It is not so efficient to do a sum in loop, better change your code as follows and to be done out side the loop as mentioned in question.
$prices = sum(array_coulmn($respon2, 'price'));
$quantity = sum(array_coulmn($respon2, 'quantity'));
$totalPayment = $prices*$quantity;
$totalPayment = '$ ' . number_format($totalPayment);
$totalpayment = 0;
$mytable ='<table>';
$respon2=array(array('title' =>'p1','quantity' =>2,'price' =>10),array('title' =>'p2','quantity' =>2,'price' =>20),array('title' =>'p3','quantity' =>1,'price' =>10));
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$totalpayment = '$ ' . number_format($totalpayment) ;
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
echo $mytable;
Related
I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc
I'm new to php and I'm working with a tutorial about showing images dynamically on the page, it works fine but it shows them vertically and I would like them to be horizontal. I created a page with code to do that but I can't seem to figure out where to insert the code to get the images to show.
Thanks for any help.
Vertical output looks like this
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added ASC LIMIT 6");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="1000px" border="0" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="1000px" align="center"><img style="border:#666 0px solid;" src="images/' . $id . '.jpg" width="50%" height="50%" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br /> $' . $details . '<br />
order</td>
</tr>
</table>';
}
mysql_close();
?>
Grid Output
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = '<table width="1000px" border="1" cellspacing="2" cellpadding="10" align="center">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td>' . $product_name . '</br>' . $details . '</br>' . $price . '</td>';
} else {
$dynamiclist .= '<td>' . $product_name . '</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
I got it figured out, Thanks for the help.
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = "";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 4 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
} else {
$dynamiclist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
I have this foreach loop:
foreach($dataSet1 as $data) {
$result .= '<tr>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
}
EDIT
I need to separate the users with bottom border, based on $data['CUST_ID'];
Example:
$data['CUST_ID'] = 2;
$data['CUST_ID'] = 2;
$data['CUST_ID'] = 2;
bottom-border;
$data['CUST_ID'] = 25;
$data['CUST_ID'] = 25;
bottom-border;
$data['CUST_ID'] = 2131;
bottom-border;...
Just save the previous ID in a variable and check it, and update it, in each loop iteration:
$previousId = '';
foreach($dataSet1 as $data):
if ($previousId !== '' && $previousId !== $data['CUST_ID']) {
// put a border
}
$previousId = $data['CUST_ID'];
$result .= '<tr>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
endforeach;
You need to set a variable outside the loop that will keep this information:
$previous = null;
So inside your loop you can ask:
if ($data['CUST_ID'] == $previous) {
// current customer id same as previous id
} else {
// not the same
}
And in the very end of your loop set the precious variable to the current one:
$previous = $data['CUST_ID'];
/* You have to declare a variable outside of the foreach loop
which will hold your value for previous id as you proceed through the loop.*/
$prev_cust_id = '';
foreach($dataSet1 as $data):
// Then inside the loop, declare a the border styling as empty.
//Make it not empty and as you need it to be if your IDs do not match.
$add_border = '';
if($data['CUST_ID'] != $prev_cust_id && $prev_cust_id != ''){
$add_border = 'style="border-bottom:1px solid black;"';
}
$result .= '<tr>';
// Assign border styling variable to your HTML element.
$result .= '<td '.$add_border.' bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
// Set current ID as previous ID before ending the loop for current ID.
$prev_cust_id = $data['CUST_ID'];
endforeach;
I am working on a project in which a tutor can save its class timing. He can see his timing according to the days. I used the code
$qry = mysqli_query($con, 'select * from users left join time_slot on users.id=time_slot.u_id where users.id=' . $id);
echo '<table class="table_class" border="2">
<tr>
<th class="details">id</th>
<th class="details">Date</th>
<th class="details">start time</th>
<th class="details">End Time</th>
</tr>';
while ($row = mysqli_fetch_array($qry)) {
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
But It show the multiple time if a tutor have multiple class in same day.
I want to show if he has 2 or more class on similar day(Monday) then all time slot show in a single row. Same this for all days of the week. How can I do it?
You can use GROUP_CONCAT function for this. Assuming your ddl is something like that
create table users(id bigint, name varchar(50));
create table time_slot(id bigint, u_id bigint, day datetime, time_from time, time_to time);
the sql would be as follows:
select u.id,u.name, ts.day,
group_concat(ts.time_from, ' - ', ts.time_to ORDER BY ts.time_from, ts.time_to)
from users u left outer join time_slot ts on u.id = ts.u_id
group by u.id, u.name, ts.day
order by u.name, ts.day
See fiddle.
I have did with some temp values.
if you want in same way to impliment then it is usefull for you.
copy the code and check here http://phpfiddle.org/
$obj1['id']='1';
$obj1['name']='a1';
$obj1['day']='asdadh';
$obj1['time_from']='1';
$obj1['time_to']='1';
$obj2['id']='2';
$obj2['name']='a2';
$obj2['day']='asdad';
$obj2['time_from']='1';
$obj2['time_to']='1';
$obj3['id']='3';
$obj3['name']='a2';
$obj3['day']='asdad';
$obj3['time_from']='1';
$obj3['time_to']='1';
$arr = Array();
$arr[]=$obj1;
$arr[]=$obj2;
$arr[]=$obj3;
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
echo "<br><br><br><br><br><br><br>";
$dates=Array();
$count=0;
foreach($arr as $id=>$row){
$val = $row['day'];
$key = array_search($val,$dates);
if(is_numeric($key)){
$arr[$key]['day']=$dates[$key].','.$val;
unset($arr[$id]);
}else{
$dates[$count]=$val;
}
$count++;
}
// new table
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
I am having syntax error with my following code
<?php
If (!empty($_SESSION['LogedinStudentId'])) {
echo '<h3>Your Scholarship Applications:</h3>
<table width="100%" class="table table-bordered">
<tr>
<th scope="col">Sr.No.</th>
<th scope="col">Date of Application</th>
<th scope="col">Course Type</th>
<th scope="col">Course Description</th>
<th scope="col">Subject</th>
<th scope="col">Applied for Semester No.</th>
<th scope="col">Scholarship Status</th>
<th scope="col">View / Print</th>
</tr>
<tr>
<td>' . ++$serialno . '</td>
<td>' . if(empty($row_studentdashboard['DateofApplication'])) {
echo ' ';
} else {
echo date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
. '</td>
<td>' . $row_studentdashboard['CourseType'] .'</td>
<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>
<td>' . $row_studentdashboard['Subject'] .'</td>
<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>
<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>
<td>View / Print</td>
</tr>
</table>';
} else {
echo '<h3>You do not have any application pending</h4>';
}
?>
I am getting syntax error on line no. 17 and 22. The second (nested) if statement is throwing syntax error. I can not judge what is wrong. If I run this second if statement outside of the html it is working fine.
Can anyone point out what's wrong?
What you are doing is an if-statement inside of echo-statement. It is wrong.
Run second if-statement outside of html and create a variable that you later print in your html.
A kind of this:
if(empty($row_studentdashboard['DateofApplication'])) {
$text = ' ';
} else {
$text = date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
}
.....
<td>' . ++$serialno . '</td>
<td>' . $text . '</td>
You 're not supposed to concatenate an if statement to a string. That is what you did on line 17/18
Try :
<?php
if (isset($_SESSION['LogedinStudentId']) && !empty($_SESSION['LogedinStudentId'])) {
$out = '<h3>Your Scholarship Applications:</h3>';
$out .= '<table width="100%" class="table table-bordered">';
$out .= '<tr>';
$out .= '<th scope="col">Sr.No.</th>';
$out .= '<th scope="col">Date of Application</th>';
$out .= '<th scope="col">Course Type</th>';
$out .= '<th scope="col">Course Description</th>';
$out .= '<th scope="col">Subject</th>';
$out .= '<th scope="col">Applied for Semester No.</th>';
$out .= '<th scope="col">Scholarship Status</th>';
$out .= '<th scope="col">View / Print</th>';
$out .= '</tr>';
$out .= '<tr>';
$out .= '<td>' . ++$serialno . '</td>';
$out .= '<td>';
if(!isset($row_studentdashboard['DateofApplication']) || empty($row_studentdashboard['DateofApplication'])) {
$out .= ' ';
} else {
$out .= date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
$out .= '</td>';
$out .= '<td>' . $row_studentdashboard['CourseType'] .'</td>';
$out .= '<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>';
$out .= '<td>' . $row_studentdashboard['Subject'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>';
$out .= '<td>View / Print</td>';
$out .= '</tr>';
$out .= '</table>';
} else {
$out = '<h3>You do not have any application pending</h4>';
}
echo $out;