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>
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
}
}
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