how to repeat entire table in while loop - php

I have a code which repeats rows while loop, but how can i repeat the entire table in while loop.
The code is
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1 GROUP BY ps_order_detail.product_name");
$ktotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1");
$krow = mysql_fetch_array($ktotal);
$kftotal = $krow['product_total'];
if(mysql_num_rows($kandivli) > 0)
{
echo '<table border="1" align="center" class="total">';
echo "<th> Product Name </td><th> Total Quantity </th>";
while($kandivlirow = mysql_fetch_array($kandivli))
{
$kandivli_product = $kandivlirow['product_name'];
$kandivlitotal = $kandivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$kandivli_product</td><tdalign='center'>$kandivlitotal</td>";
echo '</tr>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$kftotal</b></td>";
echo '</table>';
}
else echo "<div align='center'>No Pending orders</div>";
?>
Right now i just have 5 shops but if i add a new shop every time i have to copy paste and change this code to && ps_order_detail.id_shop = 6 then && ps_order_detail.id_shop = 7 and so on
So please if anyone shows how to repeat table. It would be grateful.
Hope my question is clear. (I want to repeat the entire table)
Regards
Amod
Ok I have Edited the code Now i get 6 tables as expected but the rows show the same data of shop one.
<?php
session_start();
include_once "connect.php";
?>
<?php
$supplier = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total, ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.id_shop");
$alltotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS producttotal FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 && ps_order_detail.id_shop = 11");
$ct = mysql_fetch_array($alltotal);
$ct1 = $ct['producttotal'];
while($supplierrow = mysql_fetch_array($supplier))
{
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total,ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.product_name");
echo '<table border="1" align="center" class="total">';
while($kandivlirow = mysql_fetch_array($kandivli))
{
$sup = $kandivlirow['name'];
$sp = $kandivlirow['product_name'];
$spt = $kadivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$sp</td><td align='center'>$spt</td><td align='center'>$sup</td>";
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$ct1</b></td>";
?>

while($kandivlirow = mysql_fetch_array($kandivli))
{
//Your table code
echo '<table border="1" align="center" class="total">';
//Middle section
echo '</table>';
}

Related

Can I avoid this query in the While loop to speed up page loading?

My code is constructing a web page where I display multiple tables of data based on a machine ID (machine_id). The end user wants to be able to scroll through all tables on a single page versus having a single page for each individual machine.
Attached is a screenshot of what the page looks like (actually displays more than just two tables on the page). Can I modify my code so that I'm not making multiple queries in the While loop as the machine_id changes?
$db = new Database();
$result = mysqli_query($conn, "SELECT machine_id, machine_name, display_order FROM machines WHERE active_board = 'YES' ORDER BY display_order ASC");
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
$mach_id = $row["machine_id"];
$machine_name = $row["machine_name"];
$presspage = $row["machine_id"];
$machine_name = $row["machine_name"];
$daycount = '7';
$rows = $db -> select("SELECT a.machine_id, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM job_assignments a
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id = $presspage
ORDER BY j.job_number ASC");
echo "<tr>";
echo "<td>";
echo "<div id='dhtmlgoodies_dragDropContainer'>";
echo "<div id='dhtmlgoodies_mainContainer' align='center' vertical-align='middle'>";
echo "<h2>" . $machine_name . "</h2>";
for($x=0;$x<$daycount;$x++) {
$thehours = 0;
$theday = date('l M d', strtotime($date . " + {$x} day"));
$theday2 = date('Y-m-d', strtotime($date . " + {$x} day"));
$daycode = date('Md', strtotime($date . " + {$x} day"));
$totalhours = 0;
$pastdue_hours = 0;
$totalvalue = 0.00;
echo "<div style='width:180px; height:1500px; '>";
echo "<p>" . $theday . "</p>";
foreach ($rows as $row) {
$prod_date = $row["production_date"];
$shipped = $row["shipped"];
$hours = $row["hours"];
$value = $row["total_cost"];
$assignment_completed = 0;
$is_completed = $row["is_completed"];
if (($prod_date < date('Y-m-d')) AND ($shipped == 'NO') ) {
$prod_date = date('Y-m-d');
};
...
Screenshot example of web page.
You can join both the queries as
SELECT m.machine_id, m.machine_name, m.display_order, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM
machines m
LEFT JOIN job_assignments a USING (machine_id)
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id = $presspage and m.active_board = 'YES'
ORDER BY m.display_order, j.job_number;
You can first get all the machine ids from the first query into an array, then execute the second query for all machine ids in that array all at once. Below is a demonstrated example:
$db = new Database();
$result = mysqli_query($conn, "SELECT machine_id, machine_name, display_order FROM machines WHERE active_board = 'YES' ORDER BY display_order ASC");
$active_board_machine_ids = array();
while ($row = mysqli_fetch_assoc($result)) {
$active_board_machine_ids[] = $row["machine_id"];
}
$rows = $db -> select("SELECT a.machine_id, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM job_assignments a
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id IN ('".implode("','",$active_board_machine_ids)."')
ORDER BY j.job_number ASC");

SELECT additional table data

I have successfully managed to select and return teams with the associated members. Within the members table there are columns like: score_1, score_2 etc...
I am struggling to get those integer values out of the tablea to sit alongside the data:
<?php
$sql = "SELECT t.team_name as team_name, GROUP_CONCAT(m.firstName, ' ', m.lastName) as team_members
FROM members AS m
JOIN team_members AS tm
ON tm.member_id = m.member_id
JOIN teams as t
on t.team_id = tm.team_id
WHERE t.dashboard_id = $dashboard_id AND t.team_id = $teamSelect
GROUP BY t.team_name";
if(!$result = $conn->query($sql)) {
// die(printf("Errormessage: %s\n", $conn->error));
}
while($row = $result->fetch_assoc()){
echo '<h2>Team Scores: <span class="teamNameTable">' . $row["team_name"] . '</span></h2><br>';
$names = explode(',', $row['team_members']);
echo '<div class="tableHeader">';
echo '<div class="col">Name</div>';
echo '<div class="col">SDO</div>';
echo '<div class="col">DCTO</div>';
echo '<div class="col">ED</div>';
echo '<div class="col">CA</div>';
echo '<div class="col">DHPT</div>';
echo '<div class="col">IRT</div>';
echo '<div class="col">GL</div>';
echo '<div class="col">IL</div>';
echo '</div>';
foreach($names as $name) {
echo '<div class="teamNameMember">' . $name . '</div>';
}
echo '<br>';
}
?>
UPDATE
foreach($names as $name) {
echo '<div class="teamNameMember">' . $name . '</div>';
}
echo '<br>';
}
You forgot to add those columns to your select statement...
$sql = "SELECT t.team_name as team_name, GROUP_CONCAT(m.firstName, ' ', m.lastName) as team_members, m.score_1, m.score_2
FROM members AS m
JOIN team_members AS tm
ON tm.member_id = m.member_id
JOIN teams as t
on t.team_id = tm.team_id
WHERE t.dashboard_id = $dashboard_id AND t.team_id = $teamSelect
GROUP BY t.team_name";

Display selected results only from the database table

I would like to display only a selected result on my php page.
This is my php code:
$bookdetsql = "
SELECT b.bookISBN
, b.bookTitle
, b.bookYear
, b.catID
, b.pubID
, p.pubName
, p.location
, c.catDesc
, b.bookPrice
FROM nbc_book b
LEFT
JOIN nbc_category c
ON b.catID = c.catID
LEFT
JOIN nbc_publisher p
ON b.pubID = p.pubID
";
$bookdetrs = mysqli_query($conn, $bookdetsql) or die(mysqli_error($conn));
$bookdetnum = mysqli_num_rows($bookdetrs);
if($bookdetnum >= 1 ){
echo "<div style='margin: 0 0 10px 0; font-weight: bold;'>$bookdetnum record(s) found!</div>";
while ($row = mysqli_fetch_assoc($bookdetrs)) {
echo "<tr>";
echo "<td><center>" . $row['bookTitle']."</a></center></td>";
echo "<td><center>" . $row['bookYear']."</center></td>";
echo "<td><center>" . $row['catDesc']."</center></td>";
echo "<td><center>" . $row['bookPrice']."</center></td>";
echo "<td><center>" . $row['pubName']."</center></td>";
echo "<td><center>" . $row['location']."</center></td>";
echo "</tr>";
}
} else {
echo "<b>Books not found!</b>";
}
Actually this code above is displaying the whole list of records actually. I only want it to display selected record which i clicked on my first php page.
How did you get the values frmo the first page? GET or POST?
when you have the value(s), you can add an where clause to your SELECT Statement
$bookdetsql = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName, location, catDesc, bookPrice
FROM nbc_book
LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID
LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID
WHERE bookISBN = '" . $_GET["bookISBN"] . "'";
Pleas Replace left join to join because left join is fetch matching and unmatch both record but when you use simple join it is work on inner join and fetch only match record according to your requirement
$bookdetsql = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName, location, catDesc, bookPrice FROM nbc_book JOIN nbc_category ON nbc_book.catID = nbc_category.catID JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID";
$bookdetrs = mysqli_query($conn, $bookdetsql) or die(mysqli_error($conn));
$bookdetnum = mysqli_num_rows($bookdetrs);
if($bookdetnum >= 1 ){
echo "<div style='margin: 0 0 10px 0; font-weight: bold;'>$bookdetnum record(s) found!</div>";
while ($row = mysqli_fetch_assoc($bookdetrs)) {
echo "<tr>";
echo "<td><center>" . $row['bookTitle']."</a></center></td>";
echo "<td><center>" . $row['bookYear']."</center></td>";
echo "<td><center>" . $row['catDesc']."</center></td>";
echo "<td><center>" . $row['bookPrice']."</center></td>";
echo "<td><center>" . $row['pubName']."</center></td>";
echo "<td><center>" . $row['location']."</center></td>";
echo "</tr>";
}
} else {
echo "<b>Books not found!</b>";
}

Formatting a table output from php code

I have written the following code, but somehow the headers are not matching the generated columns in the rendered table,, can someone give me a hint on how to improve it?
<?php
$database =& JFactory::getDBO();
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
// Display quizzes
echo "</br>";
echo "Quizzes History for : " ;
echo "<b>";
echo $name;
echo "</b>";
echo "</br>";
echo "</br>";
$database->setQuery('SELECT distinct qui.title AS name,' .
' ( SELECT GROUP_CONCAT(profiles.title)
FROM #__jquarks_users_profiles AS users_profiles
LEFT JOIN #__jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id
WHERE users_profiles.user_id = sessionWho.user_id ) AS profile, ' .
' ( SELECT sum(score)
FROM #__jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id
AND status <> -1 ) AS score,' .
' ( SELECT count(distinct(question_id))
FROM #__jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id ) AS maxScore,' .
' ( SELECT count(id)
FROM #__jquarks_quizzes_answersessions
WHERE status=-1
AND quizsession_id = quizSession.id ) AS evaluate,' .
' quizSession.finished_on,sessionWho.email' .
' FROM #__jquarks_quizsession AS quizSession' .
' LEFT JOIN #__jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id' .
' LEFT JOIN #__jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id' .
' LEFT JOIN #__jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id' .
' LEFT JOIN #__jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id' .
' LEFT JOIN #__jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id' .
' LEFT JOIN #__jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id '.
' WHERE sessionWho.user_id =' .$id) ;
if (!$database->query()) { //write data and if error occurs alert
echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}
//var_dump($database);
$tableStyle = "padding: 5px;border:1px";
$tdStyle = "padding:5px ";
echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">';
echo "<tr> <th> Quiz Title </th><th> Score </th><th>Maximum Score </th><th> Unanswered </th> <th>Finished On </th></tr>";
$row = $database->loadRowList();
foreach($row as $valuearray)
{
echo '<tr style=" align="center">';
foreach($valuearray as $field)
{
echo "<td>$field</td>";
}
echo "</tr>";
}
echo "</table>";
?>
You have 5 <th> headers but select 6 columns from your table. Either explicitly print the fields from the table you want (you should), or change the query to only select the 5 you want.

php table warning : Invalid argument supplied for foreach

I want to output a 5 column table based on the query below , the output on phpmyadmin is correct but I am getting error :
Invalid argument supplied for foreach() on the php page. Any help would be highly appreciated. Thanks
code :
<?php
$database =& JFactory::getDBO();
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
// Display quizzes
echo "</br>";
echo "Quizzes History for : " ;
echo "<b>";
echo $name;
echo "</b>";
echo "</br>";
echo "</br>";
$database->setQuery(" SELECT distinct qui.title AS name,
( SELECT GROUP_CONCAT(profiles.title)
FROM jos_jquarks_users_profiles AS users_profiles
LEFT JOIN jos_jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id
WHERE users_profiles.user_id = sessionWho.user_id ) AS profile,
( SELECT sum(score)
FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id AND status <> -1 ) AS score,
( SELECT count(distinct question_id) FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id ) AS maxScore,
DATE_FORMAT(quizSession.finished_on,'%M %d, %Y') AS FinishedOn
FROM jos_jquarks_quizsession AS quizSession
LEFT JOIN jos_jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id
LEFT JOIN jos_jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id
LEFT JOIN jos_jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id
LEFT JOIN jos_jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id
LEFT JOIN jos_jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id
LEFT JOIN jos_jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id
WHERE sessionWho.user_id = ' .$id " ) ;
if (!$database->query()) { //write data and if error occurs alert
echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}
//var_dump($database);
$tableStyle = "padding: 5px;border:1px";
$tdStyle = "padding:5px ";
$thStyle = "padding:7px ";
echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">';
echo "<tr> <th style=align:center>Quiz Title </th><th style=align:center> Score </th><th>Maximum Score </th><th> Unanswered </th> <th>Finished On </th></tr>";
$row = $database->loadRowList();
foreach($row as $valuearray)
{
echo '<tr style=" align="center">';
foreach($valuearray as $field)
{
echo "<td>$field</td>";
}
echo "</tr>";
}
echo "</table>";
?>
This typically means that there is an error in your query, and it's not returning any results so there is no object to run through the foreach. The easiest way to debug this is to turn on debug mode from the joomla administrator panel (it's in global settings -> system), and then going to the page this error is being thrown, and it should show the SQL error.
Anyway, looking at the query the only error I can find (assuming all fields/tables are correct) is that at the end you have:
WHERE sessionWho.user_id = ' .$id " ) ;
This should be:
WHERE sessionWho.user_id = $id " ) ;
or
WHERE sessionWho.user_id = ". intval($id) ) ;

Categories