With the help of another Stack Overflow post I've now got dynamic tables working correctly.
I'm looping though an array and creating multiple tables. This works well.
This is the code I'm using :
foreach ($id as $k => $v) {
$maxcols = $cols[$unit[$k]];
$maxid = $cells[$unit[$k]];
$startid = 1;
// maxcols = 4
// maxid = 8
echo "<table id='table$v'><tr>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {
echo "<tr>\n";
for ($j=1;$j<=$maxcols;$j++) {
if ($startid <= $maxid)
echo " <td class='mark'>ID".$startid++."</td>\n";
else
echo " <td class='mark'> </td>\n";
}
echo "</tr>\n<tr>\n";
for ($j=1;$j<=$maxcols;$j++) {
$p = ($i==1) ? $j : ($j+$maxcols);
echo "<td id='$p'></td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}
If maxcols = 4 and maxid = 8 I get a table that looks like this :
Viewing the code for that table I can see each TD has an ID that matches the header ID value. eg : row 1 column 2 TD ID =2 header = ID2
The ID value is being created using this :
$p = ($i==1) ? $j : ($j+$maxcols);
If I change maxcols = 2 then I get a table that looks like this :
However the TD ID values are not matching the Header ID. They go
1,2
3,4
3,4
3,4
How do I make each cell have the correct TD ID ?
Simple solution:
$maxcols = $cols[$unit[$k]];
$maxid = $cells[$unit[$k]];
$startid = 1;
$idCounter = 1;
…
…
for ($j=1;$j<=$maxcols;$j++) {
echo "<td id='$idCounter'></td>\n";
$idCounter++;
}
There's probably a way to do it using $j, $i, and $maxcols, but why complicate things.
If you really only want to assign ids to cells that will have values, you could make this change
echo "<td id='".($idCounter<=$maxid ? $idCounter : "")."'></td>\n";
Related
Please forgive me as I am beginner in PHP.
I am trying to populate 2 two column table with inventory information. With my current code, I have the below image:
I would like to have two stores next to each other in this table. i.e Store Number 2 and Store Number 7 on the table row in the table, then Store 10 and 11 on the same row, and so on. Below is the code I am using so far to achieve this:
global $wpdb;
$result = $wpdb->get_results(
$wpdb->prepare( "
SELECT STORE_NAME,REPLACE(STORE_NAME, ' ', '-') as STOREURL, INVENTORY, STORE_NUMBER FROM StoreInventory
WHERE SKU = %s",
$product_sku
)
);
if ($result){
echo '<table class=\'inventory\'>';
foreach($result as $row) {
echo '<tr><td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td></tr>';
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}
The problem I am having is that I am looping through the record set row by row, and adding the data to each row. I have attempted to have a loop inside a loop, which will not give me the correct results.
Is it possible to split the result set into two multi-dimensional arrays and loop through each one separately then add them to the table? Can I call out a specific row with a counter within the loop?
Any advise or direction would be a great help.
A relatively simple approach would be to create a dummy variable to store the column. EX:
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</tr></table>';
Final answer based on ARubiksCube's answer:
if ($result){
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td \' width=\'50%\' ><div><a href=\'https://shopliquornl.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>' . $row->STORE_NAME . '</a><br/>' .$row->INVENTORY. ' on hand</div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}
I have following item list fetch from table, I need to add dynamic rowspan at the end of the row if item is from same supplier, but I have no idea how to work with this.
I tried:
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($group->countRow > 1){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
if($group->countRow > 1){
echo '<td rowspan="'.$group->countRow.'"><a>manage</a></td>';
}else{
echo '<td><a>test</a></td>';
}
echo '</tr>';
}
but cell Manage will always appear at every row with mess format.
the idea results that I want:
You can try something like that:
$lastId = null;
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($lastId != $group->Id){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
echo '</tr>'
$lastId = $group->Id;
}
Everytime there is a new group you can set the $i back to 0.
I have problem with looping in table. i want to loop a <td>, when it reach 10 , it build <tr> automatically.
Here my code:
$sd=mysql_query("SELECT a.*,b.* FROM surat_jalan a inner join packing_list b on a.id_surat = b.identitas_packing WHERE b.identitas_packing = '$ben[id_surat]' ORDER BY netto_packing ASC");
while($pack=mysql_fetch_array($sd)){
$komapack = number_format($pack['netto_packing'],2);
echo"<td>$komapack</td>";
}
I am stuck on here and don't know what to do but I know how to use mod but " don't know how to loop it.
You can use a counter.
$i = 0;
echo "<tr>";
while($pack=mysql_fetch_array($sd)){
$komapack = number_format($pack['netto_packing'],2);
echo"<td>$komapack</td>";
$i++;
if($i % 10 == 0) // check
echo "</tr><tr>";
}
echo "</tr>";
I recently started learning PHP on my own and started to use MySQL and Apache as well. I made a basic table in MySQL, and using some PHP code, I displayed the table in a HTML table in a browser. Now I'd like to add a delete button beside each row, and when clicked, it would delete that row. I am very new to this, and I'm just practicing. Could anyone please help me? This is the code I have so far:
From: phpcode on Pastebin
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
mysql_connect('localhost', 'root', '');
mysql_select_db('testdb');
$result = mysql_query('select * from products');
$numrows = mysql_numrows($result);
//****************************************************************
print "<table border = 3 style = width:400px>";
for($i = 0; $i < $numrows; $i++)
{
$row = mysql_fetch_row($result);
print "<tr>";
foreach($row as $cell)
{
print "<td>";
print $cell;
print "</td>";
}
print "</tr>";
}
print "</table>";
mysql_close();
?>
I also have a delete.php page, but I really don't know where to start. I've looked for online tutorials, and many say different ways.
Just add another "" inside the loop with delete text and pass the id for that
for($i = 0; $i < $numrows; $i++)
{
$row = mysql_fetch_row($result);
print "<tr>";
foreach($row as $cell)
{
print "<td>";
print $cell;
print "</td>";
print "<td>";
print "DELETE";
print "</td>";
}
print "</tr>";
}
Let say your table like below;
$result = mysql_query('select * from products');
print "<table>";
while ($row = mysql_fetch_assoc($result)) {
print "<tr><td>" . $row["name"] . "</td><td>" . $row["surname"] . "</td><td>Delete</td></tr>";
}
print "</table>";
Here, while iterating your row, you need to put $row["id"] in delete link id.
And in your delete.php
$id = $_GET["id"];
// Delete sql here. Do not forget to validate id here.
header("Location: index.php"); // I assume, table page is this
In your table products you need a field with different value for each register, name it id or whatever you want.
If you have table but doesn't have that field, good option would be and auto_increment field.
Code for add an auto_increment field:
ALTER TABLE `products` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;
Then id would be unique for each register and you don't need to worried about it because you don't need to insert it, it is generated automatically.
print "<table border = 3 style = width:400px>";
for($i = 0; $i < $numrows; $i++)
{
$row = mysql_fetch_row($result);
print "<tr>";
foreach($row as $cell)
{
print "<td>";
print $cell;
print "</td>";
}
print "<td><a href='delete.php?id=".$row["id"] ."' > delete </a> " ;
print "</tr>";
}
$row["id"] is the id of register you want to delete.
in delete.php
$id = $_GET["id"];
$delete = " DELETE from yourTable where id = ". $id ;
mysq_query ( $delete ) ;
PD: Use mysqli_
The array $carry_over returns a really long list of entries, too long for my printout page. I would love to make it in such a way that after 4 entries, it breaks and goes to the next line and breaks at the next four entries until all entries are in.
How can i do this?
Thanks
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>".$row8['course_code']."</th>";
if ( $count == 7 ){
echo "</tr>\n";
echo "</table>";
}
}
}
Update : Now only the first 7 entries are covered inside the table tags, the subsequent ones are outside the table tags. How can i put them in the tabke tags?
Thanks
Not sure if this is what you are looking for, but you could try:
$count = 0;
echo "<tr>";
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>" . $row8['course_code'] . "</th>";
$count++;
if(count == 4) {
echo "</tr><tr>";
$count = 0;
}
}
echo "</tr>";
It looks like you're using a table for list data. Instead of using a <table>, you should be using a <ul> with <li> for each element. This can then be styled so that each li has display: inline-block;, for example.
I cannot tell what the table looks like from the code above, but you can put an iterator in to count to four then wrap. You also may want to check out Wordwrap