I have a table showing the list of categories and subcategories, using a function to loop through the parent/child tree. Here is the markup of the script:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<?php
function category_list( $category_parent_id = 0 )
{
// build our category list only once
static $cats;
if ( ! is_array( $cats ) )
{
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id';
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
}
// populate a list items array
$list_items = array();
foreach ( $cats as $cat )
{
// if not a match, move on
if ( ( int ) $cat['parent_id'] !== ( int ) $category_parent_id )
{
continue;
}
// open the list item
$list_items[] = '<tr class="dataTableRow">';
$list_items[] = '<td class="dataTableContent"><li>';
// construct the category link
$list_items[] = $cat['categories_name'];
// recurse into the child list
$list_items[] = category_list( $cat['categories_id'] );
// close the list item
$list_items[] = '</li></td>';
$list_items[] = '</tr>';
}
// convert to a string
$list_items = implode( '', $list_items );
// if empty, no list items!
if ( '' == trim( $list_items ) )
{
return '';
}
// ...otherwise, return the list
return '<ul>' . $list_items . '</ul>';
}
echo category_list();
?>
<td class="dataTableContent"></td>
</table>
At the moment this currently prints the <tr class="dataTableHeadingRow"> and both the <td class="dataTableHeadingContent">correctly, but for the <td class="dataTableContent">, it is only printing the tag in the function correctly. How would I print both the dataTableContent tags correctly, and keep them both in the loop?
Looking at your recursive use of the function category_list you will be outputting <tr> elements inside <li>s. This is invaild HTML.
Also, each <tr> in the body will only have one cell, whereas the <tr> in the header row has two cells.
You should consider how the output should look, then write your code to account for that, and follow the flow through to see what is being written out. Also, check the actual HTML source returned to your browser, not how your browser renders it. Note that different browsers will render invalid HTML differently.
Also, (not related to your problem) consider replacing the border, width, cellspacing, cellpadding and align HTML attributes with CSS instead. Another consideration would be to use the <head> and <tbody> elements as additional semantic markup.
Further Explanation
Your problem is essentially these lines:
// open the list item
$list_items[] = '<tr class="dataTableRow">';
$list_items[] = '<td class="dataTableContent"><li>';
// construct the category link
$list_items[] = $cat['categories_name'];
// recurse into the child list
$list_items[] = category_list( $cat['categories_id'] );
// close the list item
$list_items[] = '</li></td>';
$list_items[] = '</tr>';
Every iteration of your loop is creating a new tr element! Try this instead:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow">
<td class="dataTableContent">
<?php
function category_list( $category_parent_id = 0 )
{
// NOTE THE ADDITIION OF THE PARENT ID:
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
if (count($cats) == 0)
{
// There are no categories to list
return '';
}
// Create a list HTML string
$list = '<ul>';
foreach ( $cats as $cat )
{
// open the list item
$list .= '<li>';
// construct the category link
$list .= $cat['categories_name'];
// recurse into the child list
$list .= category_list( $cat['categories_id'] );
// close the list item
$list .= '</li>';
}
// close and return the list
$list .= '</ul>';
return $list;
}
echo category_list();
?>
</td>
<td class="dataTableContent"></td>
</tr>
</table>
Note that further improvements can be made regarding data and presentation logic separation. You don't necessarily need to adopt a full-scale MVC framework; just use OOP to encapsulate your SQL queries within classes which represent your database entities.
EDIT
Following the specification that each category should be in its own <td>.
This requires the removal of the list elements. A <ul> can only contain <li> elements as children; it cannot contain <tr> children. A <td> MUST be a child of a <tr>. It is not allowed anywhere else. A <tr> can only be a child of a <thead>, <tbody>* or <tfoot>. These rules are all defined in the HTML DTD.
Therefore, the following is the sort of thing you need to be looking at:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<?php
function category_list( $category_parent_id = 0, $level = 0 )
{
// NOTE THE ADDITIION OF THE PARENT ID:
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
$list = '';
foreach ( $cats as $cat )
{
// start the next row:
$list .= "<tr class=\"dataTableRow\">\n";
// The cell for the category needs
$list .= "<td class=\"dataTableContent\">\n";
// construct the category link. Note we are now enclosing
// this in a div with a left-indent to show the sub level
// this category is at. Adjust the padding-left calculation
// to suit your page
$list .= '<div style="padding-left: ' . (2 * $level) . 'em;">';
$list .= "• {$cat['categories_name']}";
$list .= "</div>\n";
// close the row
$list .= "</td>\n";
$list .= "<td class=\"dataTableContent\"></td>\n";
$list .= "</tr>\n";
// recurse into the child list, incrementing $level
$list .= category_list( $cat['categories_id'], 1+$level );
}
// return the list
return $list;
}
echo category_list();
?>
</table>
*Note that <tbody> is an implied element. It doesn't have to be explicitly defined in the code, but it will always be there in the DOM.
To loop through an array and display the contents as part of a table I would separate your business logic from your display logic a little more. My solution here would look like:
<!-- populate the array of data to be displayed -->
<?php $list_items = category_list(); ?>
<!-- start the table, display header rows -->
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow">
<td class="dataTableContent">
<ul>
<!-- iterate over data array, create ul for each member -->
<?php
foreach($list_items as $item) {
?>
<li><?php print $item ?></li>
<?php
}
?>
</ul>
<!-- finish the table -->
</td>
</tr>
</table>
<!-- define function which will populate your array -->
<?php
function category_list( $category_parent_id = 0 )
{
/*
this function will look very similar to the one in the
one in the original post but will just return the whole
array instead of printing out one member.
*/
// populate $items_list...
return $items_list;
}
?>
Please note I didn't test this incomplete code but the idea should be right.
try replacing the following code:
$list_items[] = '<tr class="dataTableRow">';
$list_items[] = '<td class="dataTableContent"><li>';
with:
$list_items[] = "<tr class='dataTableRow'>";
$list_items[] = "<td class='dataTableContent'><li>";
Related
I want to implement a logic for creating a three column table using foreach loop. A sample code will look like this.
$array = ['0','1','2','3','4','5','6'];
$table = '<table class="table"><tbody>';
foreach($array as $a=>$v){
//if 0th or 3rd???????????wht should be here?
$table .= '<tr>';
$table .= '<td>$v</td>';
//if 2nd or 5th??????????and here too???
$table .= '</tr>';
}
$table = '</tbody></table>';
Any ideas?
Expected output is a simple 3X3 table with the values from the array
Use this you may looking for this
<?php
echo('<table><tr>');
$i = 0;
foreach( $array as $product )
{
$i++;
echo '<td>'.$product .'</td>';
if($i % 3==0)
{
echo '</tr><tr>';
}
}
echo'</tr></table>';
?>
Result Here:
<table>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
<tr>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
</table>
This should work for you:
(See that I added a tr at the start and end before and after the foreach loop. Also I changed the quotes to double quotes and made sure you append the text everywhere.)
<?php
$array = ['0','1','2','3','4','5','6'];
$table = "<table class='table'><tbody><tr>";
//^^^^ See here the start of the first row
foreach($array as $a => $v) {
$table .= "<td>$v</td>";
//^ ^ double quotes for the variables
if(($a+1) % 3 == 0)
$table .= "</tr><tr>";
}
$table .= "</tr></tbody></table>";
//^ ^^^^^ end the row
//| append the text and don't overwrite it at the end
echo $table;
?>
output:
<table class='table'>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</tbody>
</table>
Here is an easy solution with array_chunk():
<?php
$array = array('0','1','2','3','4','5','6');
$d = array_chunk($array, 3);
$table = "<table border='1' class='table'><tbody>";
foreach($d as $v)
$table .= "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
$table .= "</tbody></table>";
echo $table;
?>
Here is an easy solution with incrementing that works with a dynamically generated table like the one in Magento product view page to help display product attributes in two table columns preceded by attribute label -> practically we have 4 table columns together with attribute labels. This is useful for products with multiple attributes that take a long page to display.
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php $i = 1; ?>
<tr>
<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
<?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>
<?php } ?>
<?php endforeach; ?>
</tr>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
I have a table showing the list of categories and subcategories, using a function to loop through the parent/child tree. Here is the markup of the script:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow">
<td class="dataTableContent">
<?php
function category_list( $category_parent_id = 0 )
{
// NOTE THE ADDITIION OF THE PARENT ID:
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
if (count($cats) == 0)
{
return '';
}
// populate a list items array
$list_items = array();
$list_items[] = '<ul>';
foreach ( $cats as $cat )
{
// open the list item
$list_items[] = '<li>';
// construct the category link
$list_items[] = $cat['categories_name'];
// recurse into the child list
$list_items[] = category_list( $cat['categories_id'] );
// close the list item
$list_items[] = '</li>';
}
$list_items[] = '</ul>';
// convert to a string
return implode( '', $list_items );
}
echo category_list();
?>
</td>
<td class="dataTableContent"></td>
</tr>
</table>
Instead of printing as a list all in one <td>, how would i print each list element in an individual <td>?
First, remove the php function from the <td> and place it right on top of your table/html.
Instead of echo category_list();, do $list = category_list();
Then within HTML, do this:
<tr class="dataTableRow">
<?php foreach( $list as $v) { ?>
<td class="dataTableContent"><?php echo $v; ?></td>
<?php } ?>
Latest Edit:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow"><ul> <!-- change #1 -->
<?php
function category_list( $category_parent_id = 0 )
{
// NOTE THE ADDITIION OF THE PARENT ID:
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
if (count($cats) == 0)
{
return '';
}
// populate a list items array
$list_items = array();
// $list_items[] = '<ul>'; Change #2
foreach ( $cats as $cat )
{
// open the list item
$list_items[] = '<td class="dataTableContent"><li>'; //Change #3
// construct the category link
$list_items[] = $cat['categories_name'];
// recurse into the child list
$list_items[] = category_list( $cat['categories_id'] );
// close the list item
$list_items[] = '</li></td>'; //Change #4
}
$list_items[] = '</ul></tr>'; //Change #5
// convert to a string
return implode( '', $list_items );
}
echo category_list();
?>
<!--remove </tr> change #6 -->
</table>
Here, I cleaned this up for you. In particular, each function should really only do one thing, so you can keep straight what it's doing. I also removed unnecessary mishegus about your list/implode thing. You're outputting a string, use a string!
<?php
/**
* Gets all categories for a particular parent id.
*/
function get_categories( $category_parent_id = 0 ) {
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order'
.' from ' . TABLE_CATEGORIES . ' c, '
. TABLE_CATEGORIES_DESCRIPTION . ' cd '
.'where c.categories_id = cd.categories_id '
.'AND c.parent_id='.$category_parent_id;
$result = tep_db_query( $sql );//Really should use PDO
$categories = array();//Categories are not chased by dogs. Use full names for your variables!
while ( $category = tep_db_fetch_array( $result ) )
{
$categories[] = $category;
}
return $categories;
}
/**
* Outputs HTML for a list of categories, recursing into them if necessary for more data.
*/
function output_categories($categories, &$output = "") {
if (count($categories) == 0 || empty($categories)) { return; }//either works.
$output =. "<ul>";//change this to <tr> for a table row
foreach ($categories as $category) {
output =. "<li>";//change this to <td> for a table cell
$category['categories_name'];
output_categores(categories($category['categories_id']), $output);
output =. "</li>";//change to </td>
}
$output =. "</ul>";//change to </tr>
return;//Note that $output will be changed by this function.
}
%>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow">
<td class="dataTableContent">
<?php
echo output_categories(get_categories());
?>
</td>
<td class="dataTableContent"></td>
</tr>
</table>
Problem:
To style the last row in a SQL query without using any CSS3 selectors.
PHP code:
while ($item = mysql_fetch_assoc($itemsresult))
{
$answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
$answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$answer = mysql_fetch_assoc($answerresult);
if ($answer['IID'] == $item['IID'])
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
else
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"> </td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
$itemcounter++;
}
Last row in SQL query should instead print:
$html .= '
<tr>
<td class="arrow" style="'.$lastarrow.'"> </td>
<td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
</tr>
';
Question:
What needs to be added in the while loop so it recognizes the last row and print a different code instead?
Use a counter:
$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
$i++;
if ($i == $c) {
// This is a last row
} else {
// Regular row
}
}
there is several ways to do that :
use :last-child ( but this isn't working in IE8)
table tr:last-child {
background-color:#ff0000;
}
using jQuery method ( this is browser independent)
in document load function, add following jQuery code,
$("td:last-child").css({background-color:"#ff0000"})
Im writing a function to get the parent and child ids but for the third loop there is a
problem the loop gets even the previous loops id also .
How can i avoid it?
<?
$results = '
<table>
<thead>
<tr >
<td id="ticket" align="center" ><b>Task<br />ID</b></td>
<td id="ticket" align="center" ><b>col1</td>
<td id="ticket" align="center" ><b>col2</td>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$results .='
<tr >
<td align="center">
'.$row['Task_id'].'
</td>';
$results .= '<td align="center">';
$gg = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$row['Task_id']."'");
echo "<br>";
while ($rowdd = mysqli_fetch_assoc($gg))
{
$results .= $rowdd['Task_id']."<br><br>";
$gg2 = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$rowdd['Task_id']."'");
while ($rowdd2 = mysqli_fetch_assoc($gg2))
{
$results2 = $rowdd2['Task_id']."<br><br>";
}
echo "<br>";
}
// $results .= $car ;
// $results .= $t;
$results .='</td>';
$results .=' <td align="left" >'?>
<?
$results .= $results2;
$results .='</td>';
$results .='
</tr>';
}
?>
Is the $results variable empty? I only see it being concatenated.
Also, on your table you have multiple ids that are the same. You either need to change that to a class or have a unique value for each id.
I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png
But i cant get it to be like i want, instead it prints the rank on every row:
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = "<tr><td> </td>\n";
}
echo " <td>$g->name</td></tr>\n";
}
What I want:
<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td> </td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td> </td>
<td>Zazi The Beast</td>
</tr>
<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td> </td>
<td>Saint Sinner</td>
</tr>
<tr>
<td> </td>
<td>Midvalley the Hornfreak</td>
</tr>.......................
What I get:
<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>
<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner
</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.
Try changing your if statement to something like this:
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = $g->rankname;
} else {
echo "<tr><td> </td>\n";
}
It prints the rank name if it's a new rank name, else it prints empty space.
The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages:
- You don't need to depend on the order of the results returned
- You don't need to maintain dodgy logic (like 'old_rank')
- You can display them more nicely (with a rowspan for repeated ranks
I believe the total code is more compact too.
// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
if ( !in_array($g->rankname, $ranks) ) {
$ranks[htmlentities($g->rankname)] = array();
}
$ranks[$g->rankname][] = htmlentities($g->name);
}
// do other program logic here
// end of program
?>
<!-- display the page -->
<table>
<tr>
<th>Rank</th><th>Users</th>
</tr>
<?php foreach($ranks as $rankName => $userList): ?>
<tr>
<td rowspan="<?php echo (string)sizeof($userList); ?>">
<?php echo $rankName; ?>
</td>
<td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
</tr>
<?php endforeach; ?>
</table>
I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
echo '<tr>' . "\n";
echo '<td>';
if ($g->rankname != $old_rank) {
$old_rank = $g->rankname;
echo $old_rank;
} else {
echo ' ';
}
echo '</td>';
echo '<td>' . $g->name . '</td>' . "\n";
echo '</tr>' . "\n";
}