I have to do a query to get the total of product filtred by payment type
My table:
product_id, paymentType price
87 E 1
87 E 3
87 C 5
87 V 30
100 C 1
359 E 12
359 C 32
My query:
$query = select count(*) as qte, paymentType as PT, product_id as PID, sum(price) as PR from mytable
group by product_id, paymentType
order by product_id
the result of the query is :
product_id paymentType qte price
87 E 2 4
87 C 1 5
87 V 1 30
100 C 1 1
359 E 1 12
359 C 1 32
I need to create table on php as :
$output .= '<table border="1" >';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th><div style="width:150px" >product</div></th>';
$output .= '<th>paymentType E</th>';
$output .= '<th>Qte E</th>';
$output .= '<th>total price E</th>';
$output .= '<th>paymentType C </th>';
$output .= '<th>Qte C</th>';
$output .= '<th>total price C</th>';
$output .= '<th>paymentType v </th>';
$output .= '<th>Qte V</th>';
$output .= '<th>total price V</th>';
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
if ($row){
$old_product_id = -1;
do {
// ID du produit
$product_id = $row->PID;
/* switch($row->PT){
case 'E': {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
case 'C: {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
case 'V': {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
}
*/
if ($old_product_id == $product_id){
$output .= '<tr>';
$output .= ' <td style="width:200px;">'.$row->PID.'</td>';
// payment type E
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
// payment type C
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
// payment type V
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
$output .= '</tr>';
}else{
}
$old_product_id = $product_id;
}while ($row = sqlsrv_fetch_object($result));
$output .= '</tr>';
$output .= '</tbody>';
$output .= '</table>';
$this->db->free();
echo $output;
}else {
echo 'no data found ';
}
Any solution to get this worK ? thanks if advance :)
my script doesn't work , I can't show the row for the same product with different payment type in the same row
I created a script that would do the headers for you aswell:
Get data:
<?php
require('connect.php');
$tsql = "SELECT * FROM BLAH";
$stmt = sqlsrv_query($conn, $tsql);
## Get Results ##
$data = array(); ## Variable to hold data.
while (($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) !== false) {
$data[] = $row;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Display data:
<thead>
<tr>
<?php
foreach(array_keys($data[0]) as $key) {
echo "<th>" . $key . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($data as $key) {
echo "<tr>";
foreach($key as $vals) {
echo "<td>" . $vals . "</td>";
}
echo "</tr>";
}
?>
</tbody>
This is a function which helps you to create a table not thinking about design. Just pass the SQL.
Call As like this -
$fieldname=array("name","Roll");
$query = "Select name,roll from Student";
create_table($query,$fieldname,$id='mytable');
function create_table($query,$fieldname,$id='mytable'){
print' <table id="'.$id.'" class="table table-striped">';
print'<tr>';
for($i=0;$i<sizeof($fieldname);$i++){
print'<th>'.$fieldname[$i].'</th>';
}
print'</tr>';
$result = mysql_query($query);
while($row= mysql_fetch_array($result)){
print ' <tr>';
for($i=0;$i<sizeof($row)/2;$i++){
print'<td>'.$row[$i].'</td>';
}
print' </tr>';
}
print'</table>';}
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 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;
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;
I have created an object (one of my first working ones) in PHP. I have a need to return the following code several times on a page......
<?php while($section = mysql_fetch_array($section3)){ ?>
<tr width='150'>
<td><?php echo $section['EmProSectName'];?></td>
<td><?php
echo $sect2->FindStatusRow($_SESSION['cEmployee'], $section['idEmProSect']);
?></td>
<td><?php echo $section['EmProSectBTN']; ?></td>
</tr>
<?php
$checkSect = $sect2->checkSubSection($section['idEmProSect']);
if($checkSect==1){
$subSect = $sect2->getSubSection($section['idEmProSect']);
while($sect = mysql_fetch_array($subSect)){
?>
<tr>
<td class="indent30"><?php echo $sect['EmProSectName'];?></td>
<td><?php
echo $sect2->FindStatusRow($_SESSION['cEmployee'], $sect['idEmProSect']);
?></td>
<td><?php echo $sect['EmProSectBTN']; ?></td>
</tr>
<?php }?>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
}
} ?>
I was going to write a method to return this above statement. Something along the lines of
public function showEmployProcessresult($sectName){
// here would be the table statement
}
however Im not sure how to write my block in the middle and then return it properly. can someone please show me how?
Here you go, I removed the $sectName variable as it is not used and combined the empty td together.
public function showEmployProcessresult(){
$block = '';
while($section = mysql_fetch_array($section3)){
$block .= '<tr width='150'>';
$block .= '<td>';
$block .= $section['EmProSectName']
$block .= '</td>';
$block .= '<td>';
$block .= $sect2->FindStatusRow($_SESSION['cEmployee'], $section['idEmProSect']);
$block .= '</td>';
$block .= '<td>';
$block .= $section['EmProSectBTN'];
$block .= '</td>';
$block .= '</tr>';
$checkSect = $sect2->checkSubSection($section['idEmProSect']);
if($checkSect==1){
$subSect = $sect2->getSubSection($section['idEmProSect']);
while($sect = mysql_fetch_array($subSect)){
$block .= '<tr>';
$block .= '<td class="indent30">'.$sect['EmProSectName'].'</td>';
$block .= '<td>';
$block .= sect2->FindStatusRow($_SESSION['cEmployee'], $sect['idEmProSect']);
$block .= '</td>';
$block .= '<td>'.$sect['EmProSectBTN'].'</td>';
$block .= '</tr>';
}
$block .= '<tr>';
$block .= '<td colspan="3"> </td>';
$block .= '</tr>';
}
}
return $block;
}
I'm very new to working with php / mysql coding, and I've got a slight problem with displaying results that are linked to an sql database.
At the moment people search using the below code - which works fine - it's a drop down select box, but you can only select one option. I want it so the items are check boxes and you can select more than one item.
I've included below the code that's used for when people input the data to the database - which are check boxes - and I have tried replacing the 'drop down select code' with this but it doesn't work.
Does anybody know what code I have to use to replace the 'drop down select code' so that checkboxes are viewable and you can filter more than one item - I have also included the 'results page code', which displays the results and I'm thinking that 'ClientStage' needs adding to the 'check box code' somewhere.
Sorry about my lack of knowledge with this and would be grateful for some help?
DROP DOWN SELECT CODE
<select name="ClientStage" id="ClientStage">
<option value=""></option>
<?php
include 'Easyspace.php';
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
?>
</select></span>
CHECK BOX CODE
<table width="100%" border="0" cellspacing="1">
<?php
$side=1;
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
if ($side == 1){
$side = 2;
echo '<tr>';
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
} else {
$side = 1;
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
}
?>
</table>
RESULTS PAGE CODE
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
$ClientStage = $_POST['ClientStage'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$ClientStage', Client_typeID)";
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>
Here's your new results code. I hope it's what you were looking for. Pay attention to your code: try to puts vars in lowercase ($Country has to be $country, try to use PDO for MySQL, etc...).
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
/* change here */
$ClientStage = $_POST['Clients'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
/* change here */
$sql .= 'Client_typeID IN (';
foreach($ClientStage as $i => $id) {
if ($i !== 0)
$sql .= ',';
$sql .= "'" . $id . '"';
}
$sql .= ')';
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>