Create table with foreach without repeating table headings - php

I want to create a html table dynamically with php and include data from two arrays.
However, the code I have written repeats the table headings for each row (pun not intented).
How can I create a table that only has the table heading tags at the top?
Here is my code:
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
echo "</table>";
}
}

Just put the table header out of your foreach look:
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
}
echo "</table>";

Related

Echo Mysql into a table - presentation and DOM

Two questions in one:
First question, mainly about presentation:
I'm echo-ing the following code which should create a table. The table should have a single column, but it's being rendered with the elements above the image as a single line. can anyone see why?
<?php
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
?>
Question 2:
Can I show the second result in a second column, or as a separate table? Also, is it possible to access the $result elements like say, $[manufacturer][1]?
Always look at the emitted source your code generates by either "View Source" or using a tool like curl. You'll find this mistake:
echo "</tr";
You're missing a >.
Many people who write HTML have browser plugins that can link through to an HTML validator to ensure they've got the correct syntax. You may want to find and install one of these.
To generate the second result is a separate table follow the following code.
echo "<table>";
$count = 1;
while($row = mysqli_fetch_array($result)){
if($count == 2){
echo "<table>";
echo "<tr>";
echo "<td>put your code here</td>";
echo "</tr>"
echo </table>
}else{
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
$count ++;
}
echo "</table>";
As you are limiting your query to only two record so this method is not bad for that and hope this will help.

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

Table creation with php and mysqli

i'm trying to do a table with php which takes the data from my database and build's the table im html but despite my while loop , only the 1st row is getting into the table. If i change the while loop before the
echo "<table border='1' cellpadding='2' cellspacing='2'";
I get all of them but in 3 tables.
What's wrong with the code ?
<?php
require 'connect.php';
$query = $link->query("SELECT * FROM fornecedor");
echo "Fornecedores";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr>
<td>Nome</td>
<td>NIF</td>
<td>Cidade</td>
<td>Rua</td>
<td>NrPorta</td>
<td>Website</td>
<td>email</td>
</tr>";
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row["Nome"] . "</td>";
echo "<td>" . $row["NIF"] . "</td>";
echo "<td>" . $row["Cidade"] . "</td>";
echo "<td>" . $row["Rua"] . "</td>";
echo "<td>" . $row["NrPorta"] . "</td>";
echo "<td>" . $row["Website"] . "</td>";
echo "<td>" . $row["Email"] . "</td>";
echo "</tr>";
echo "</table>";
};
?>
Move echo "</table>"; out of the while loop-
....
while(){
.....
// don't close the table tag here
}
echo "</table>";
For each row you are adding the closing </table> tag which should not be like that.
echo "</table>"; code must be out of the while loop. It must be written once only.

How can I join these two html tables together?

I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>

Categories