Only last result of an array showing in html table (php, mysql) - php

TEST SITE Look here to see code in action
I have two arrays which when echo'd show exactly what i want it to (look at the test site). I'm trying to get them both into a html table but it is only showing the last entry. I have shoved the entire array code into the table and it works fine (although there all in the same row and not separated) but as it is being used in a html email i'm not sure if this will be safe? I'm sorry if this is a really simple fix i'm new to php/mysql so the simple things seem impossible at the moment. I also know i could no doubt combine the two arrays but im on a KISS mantra at the moment and this is the easiest for me to understand. Any help would be appreciated. Thanks.
<?php
//Get Order Codes
foreach ($ids as $id) {
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
$result = mysql_query($sqlcode);
while($row = mysql_fetch_assoc($result)) {
$codes['codes'] = $row['od_code'];
}
echo "".$codes['codes']."<br />";
}
//Get Product Name
foreach ($ids as $id) {
$sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
$result = mysql_query($sqlname);
while($row = mysql_fetch_assoc($result)) {
$names['names'] = $row['pd_name'];
}
echo "".$names['names']."<br />";
}
?>
<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
</table>

A better solution
Change your logic so that you only use one SQL query and make use of a join.
SELECT p.pd_name,
oc.od_code
FROM tbl_product p
LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
WHERE p.pd_id = $id
So this should be the following using PDO:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("
SELECT p.pd_name,
oc.od_code
FROM tbl_product p
LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
WHERE p.pd_id = ?");
if ($stmt->execute(array($id))) {
while ($row = $stmt->fetch()) {
// print out table rows here
}
}
Immediate fix (yuck)
This should solve your immediate problem, but there is a much better way of doing this.
<?php
foreach ($ids as $id) {
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
$result = mysql_query($sqlcode);
$codes = array();
while($row = mysql_fetch_assoc($result)) {
$codes[] = $row['od_code'];
}
//Get Product Name
foreach ($ids as $id) {
$sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
$result = mysql_query($sqlname);
$names = array();
while($row = mysql_fetch_assoc($result)) {
$names[] = $row['pd_name'];
}
}
?>
<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<?php foreach($names as $key => $name): ?>
<tr>
<td> <?php echo $name .":"."<br>"?> </td>
<td> <?php echo $codes[$key]."<br>"?> </td>
</tr>
<?php endforeach; ?>
</table>

$names['names'][] = $row['pd_name'];
<td> <?php foreach($names['names'] as $name){ echo "". $name.":"."<br>" } ?> </td>
Or as suggested in comments:
while($row = mysql_fetch_assoc($result)) {
$names['names'] = $row['pd_name'];
$codes['codes'] = $row['pd_code'];
?>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
<?php } ?>
Please try to use PDO instead for interacting with mysql http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

You have to iterate through the array within the table, the same way you do it above the table-tags.

you are using
foreach($ids as $id)
make table to be populated inside foreach
or else use 2d array $name and $codes and count the array size and loop your table structure for count no of times

Related

Displaying SQL Query in HTML table (php)

Trying to get counts from 2 different tables into a simple HTML table.
This is my code:
$sql = "SELECT COUNT(*) FROM tasks";
$result = $conn->query($sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) { ?>
<table style="width:100%">
<tr>
<th>Total</th>
</tr>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?=$rowcount;?></td>
</tr>
<?php } ?>
</table>
<?php } else { echo "0 results"; } ?>
When I run the code, it shows the amount of rows in the table, but it also creates the amount of rows with the number in it (i.e. 281 rows).
Table
281
281
281
281 etc.
My idea was to copy and paste the above to show a second table set of results (if it was correct), but is there a better way of doing this? I've been looking at how I would display SELECT (select COUNT(*) from tasks) , (select count(*) from quotes) into the following format (HTML):
Table
Count
Tasks
281
Quotes
42000
First of all your query does produces only one row for a table, not 281.
The second - I omit usage of prepared SQL statements with placeholders, which should always be used in a real project whenever applyable.
$rows = [];
foreach(['Tasks', 'Quotes'] as $table ){
$result = $conn->query("SELECT '$table' as 'table', count(*) as 'count' FROM $table");
if( $result )
$rows[] = $result->fetch_assoc();
}
if( empty( $rows ) )
print "0 results";
else {?>
<table style="width:100%">
<tr><th>Table</th><th>Count</th></tr>
<?=implode(
"\n",
array_map(function($row){
return "<tr><td>${row['table']}</td><td>${row['count']}</td></tr>";
}, $rows)
)?>
</table>
<?php }
I've been looking at how I would display SELECT (select COUNT() from
tasks) , (select count() from quotes) into the following format
(HTML)
You can just run the queries query, and use the result of the first to create the first row of the table, then the result of the second to create the second row. Since COUNT queries always return exactly 1 row when there's no GROUP BY, it's quite simple to do really:
$sql1 = "SELECT COUNT(*) FROM tasks";
$result1 = $conn->query($sql1);
if ($row = $result1->fetch_array()) $taskCount = $row[0];
else $taskCount = "error";
$sql2 = "SELECT COUNT(*) FROM quotes";
$result2 = $conn->query($sql2);
if ($row = $result2->fetch_array()) $quoteCount = $row[0];
else $quoteCount = "error";
?>
<table style="width:100%">
<tr>
<th>Table</th>
<th>Count</th>
</tr>
<tr>
<td>Tasks</td>
<td><?php echo $taskCount; ?></td>
</tr>
<tr>
<td>Quotes</td>
<td><?php echo $quoteCount; ?></td>
</tr>
</table>
Another way, if you want the HTML structure to be less repetitive / dependent on the tables queries, is to UNION the SELECTs into a single query:
$sql = "SELECT 'Tasks' AS 'table', COUNT(*) as 'count' FROM tasks";
$sql .= " UNION ";
$sql .= "SELECT 'Quotes' AS 'table', COUNT(*) as 'count' FROM quotes";
$result = $conn->query($sql);
?>
<table style="width:100%">
<tr>
<th>Table</th>
<th>Count</th>
</tr>
<?php
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo $row["table"]; ?></td>
<td><?php echo $row["count"]; ?></td>
</tr>
<?php
}
?>
</table>

Print calculated Value of variable before its been calculated

This is a tricky question to search, hence my post here. I have a a header that displays the sum of all the values in a column of a table that is printed below it. However the table is generated from a MYSQL table and the sum of the column values is calculated as it is generated. So somehow I have to have a variable printed but only after the table is generated and I am not sure how to pass the variables back up to the print statement so it doesn't always print out a 0
I feel like the solution is that the sum should call a script (Javascipt) that generates and prints the table returning the sum of columns to then be printed. But I am not sure how I would do this
echo "
<h3>EPL</h3>
<h5>Total Score: $total</h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
<?php
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT * FROM $tablename ORDER BY position_order";
$users = $mysqli->query($sql);
while($user = $users->fetch_assoc()){
$con = mysqli_connect('localhost', 'root', 'root', 'predictions');
$sql1 = "SELECT * FROM `predictions`.`".$tablename."` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$sql2 = "SELECT * FROM `predictions`.`epl` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$result = mysqli_query($con, $sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$result->close();
$result = mysqli_query($con, $sql2);
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$total += abs($position1-$position2);
?>
<tr id="<?php echo $user['id'] ?>">
<td><?php echo $user['position_order'] ?></td>
<td><?php echo $user['title'] ?></td>
<td><?php echo abs($position1-$position2); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
To explain the table further, each user has their own table with the same format username_league, so I use some php code to grab the table. Additionally I have a base case table in this case 'epl' in which I compare the tables and calculate the scores based on the absolute difference in the column 'position_order'.
You should always aim for separation of your code and presentation. And beyond that, your database code is extremely inefficient. You're re-creating an entire database object for every instance of your original query.
Just put your PHP code before the HTML, or better yet in a separate file.
<?php
$total = 0;
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT id, position_order, title FROM `$tablename` ORDER BY position_order";
$users_result = $mysqli->query($sql);
while($user = $users_result->fetch_assoc()) {
$users[] = $user;
}
foreach ($users as $user) {
$sql1 = "
SELECT position_order FROM `$tablename` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
UNION
SELECT position_order FROM `epl` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
";
$result = $mysqli->query($sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$user["position1"] = $position1;
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$user["position2"] = $position2;
$total += abs($position1 - $position2);
}
?>
There you are; using a single database object, one third fewer queries, all your values in an array. They're ready to use later in the file, or in your separate HTML view:
<h3>EPL</h3>
<h5>Total Score: <?=$total?></h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>
<?php foreach($users as $user):?>
<tr id="<?=$user['id'] ?>">
<td><?=$user['position_order'] ?></td>
<td><?=$user['title'] ?></td>
<td><?=abs($user["position1"]-$user["position2"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I don't know enough about the structure here, but I'd be very surprised if you couldn't make this a single database query.
Output buffering is your friend! Simply call ob_start before outputting the table i.e.
ob_start();
echo "
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
...
then once you have done generating the table, you can collect the output of the table's content using ob_get_clean, output the header and then the table content:
...
$table = ob_get_clean();
echo "<h3>EPL</h3>
<h5>Total Score: $total</h5>";
echo $table;

PHP/MySQL - ORDER by not working

Here is my code:
<?php
if($_rate->getCode()=="matrixrate_matrixrate") {
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'select * from `extensa_econt_city_office` WHERE `delivery_type`="to_office" GROUP by `city_id`';
$results = $readConnection->fetchAll($query);
?>
<center><b>Населено място:</b></center>
<select name="shipping_city" id="shipping-city-select" title="">
<option value="">Изберете град</option>
<?php
foreach($results as $row){
$CityCode = $row['city_id'];
$query1 = "select * from `extensa_econt_city` WHERE `city_id`='$CityCode' AND `type`!='с.' ORDER by office_id DESC";
$results1 = $readConnection->fetchAll($query1);
foreach($results1 as $row1){
?>
<option value="<?PHP echo $row1['city_id']; ?>"><?PHP echo $row1['type']; ?> <?PHP echo $row1['name']; ?></option>
<?php
}
}
?>
</select>
For some reason the ordering with ORDER by office_id DESC is not working.
There is no change if I change DESC to ASC or even if I remove the ORDER by statement.
Do you have any suggestion why it is not ordering the rows properly ?
You Missing ``(quotes) around office_id
if you still find that query is not working then print whole query using echo statement..
copy that query and paste in php-myadmin and check which sql exception you getting..
hi maybe you can check your sintaxis your code maybe have a little error
for this i suggest that you run your query on phpmyadmin and check if the result is correct , then is correct the result you have a debug on your code php
this a little example for you
<?php
$link=Conect();
$query="SELECT * FROM clients ORDER BY client_name ASC";
$result=mysql_query($query,$link) or die("Error: ".mysql_error());
if(mysql_num_rows($result) > 0)
{
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>NAME</td>
<td>PHONE</td>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr>".
"<td>".$row["id"]."</td>".
"<td>".$row["client_name"]."</td>".
"<td>".$row["phone"]."</td>".
"</tr>";
} /
}
else
{
echo "don't exist clients for list";
}
mysql_close($link);
?>
</table>
good luck and try

using foreach and while together in PHP to generate table

I've done something similar to this table below quite a few times, where I use a while statement to populate the <td> section of a table, but I've never done it before where part of the <td> is populated with a foreach statement, and it's confusing me.
In this example, I create a table, then I populate the first column with a list of suppliers, based on the number of tables inside of my database. (each supplier has its own table).
$supplierList = array();
$showTable = "SHOW TABLES from dbOne";
$getSuppliers = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getSuppliers)) {
$supplierList[] = $row;
}
$supplierList = array_reduce($supplierList, 'array_merge', array());
Now I have my array that contains the list of all my suppliers, and I use that to generate the <td>s in my table.
<table>
<thead>
<th style="text-align: center;">Supplier</th>
<th style="text-align: center;">Earliest line</th>
<th style="text-align: center;">Latest line</th>
<th style="text-align: center;"># of total lines</th>
</thead>
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT date FROM $subList ORDER BY date DESC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT date FROM $subList ORDER BY date ASC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['date'];
?>
<tbody>
<td><img src = "/img/suppliers/<?= $supplierName ?>.png"></td>
<td style="text-align: center;"><?= $earlyExpDate ?></td>
<td style="text-align: center;"><?= $lateExpDate ?></td>
<td style="text-align: center;"><?= $countLinesCount ?></td>
</tbody>
<?php
}
}
?>
</table>
The table itself builds correctly, and displays each supplier in a unique row. I cannot figure out though how to populate the other parts of the row with the information based off the unique supplier in the foreach statement.
Okay, I sorted this out. In case it helps anyone else, here's how I did it:
This code goes in the same spot as my post above - so in between the header and the body of the table. I did not need to use any <tr>.
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne ASC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne DESC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
}
while ($row = mysqli_fetch_array($lateExpQuery)) {
$lateExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
?>
They key is that I had to close off each individual while statement except the last one - closing that after I closed the </tbody> tag but before the </table>, like so:
</tbody>
<?php
}
}
?>
</table>

fetching multiple columns of mysql table and echoing each row

I have this php/html code that echos a column from a mysql table called "tickets".
These are the 3 columns in the table (member_id, ticket_id, ticket_result).
I want to be able to grab the info from the other columns in a specific row and echo that with the foreach loop. I just don't know how I would be able to do that because you can only have 1 array in a foreach loop so if I was to make add this line to the PHP Code I don't see how I could echo it in the foreach loop.
$me3 = array();
$me2[] = $row->ticket_result;
PHP Code:
public function tickets() {
$this->db_connection = new mysqli('', '', '', '');
$sql = "SELECT ticket_result
FROM tickets
WHERE member_id = '1'";
$query = $this->db_connection->query($sql);
$me2 = array();
while ($row = $query->fetch_object()) {
$me2[] = $row->ticket_result;
}
return $me2;
}
}
HTML Code:
<?php $me2 = $classLogin->tickets(); ?>
<?php foreach($me2 as $value) { ?>
<table>
<thead>
<th>Result</th>
<th>ID</th>
</thead>
<tr>
<td><?php echo $value; ?> </td>
<td> </td>
<?php } ?>
</tr>
</table>
You're only selecting one column of your table:
$sql = "SELECT ticket_result FROM tickets WHERE member_id = '1'";
Your statement should look like this:
$sql = "SELECT member_id, ticket_id, ticket_result FROM tickets WHERE member_id = '1'";
Or:
$sql = "SELECT * FROM tickets WHERE member_id = '1'";
Then you can retrieve your data with this:
while ($row = $query->fetch_object()) {
$me2[$row->ticket_id]['ticket_result'] = $row->ticket_result;
$me2[$row->ticket_id]['member_id'] = $row->member_id;
}
So you'll have an array ($me2) that contains keys according to your ticked_ids and each key is an array with the keys ticket_result and member_id which contain that data from your db.
Then you can implement your foreach like this:
foreach($me2 as $key => $value) {
echo $key;
echo $value['ticket_result'];
echo $value['member_id'];
}
Also, if the HTML you have to print is not that much, I suggest you better use echo and concatenate a string withing a single php clause rather than opening and closing multiple php tags for every little php code you have to execute, your code would look like this:
<?php
$me2 = $classLogin->tickets();
echo
'<table>
<thead>
<th>Result</th>
<th>ID</th>
</thead>';
foreach($me2 as $key => $value) {
echo
'<tr>
<td>'.$value['ticket_result'].'</td>
<td>'.$value['member_id'].'</td>
</tr>';
}
echo '</table>';
?>

Categories