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>
Related
I'm quite new to html and php, I've tried a few things trying to achieve what i want but it never works.
It's a shopping cart, I'm trying to display data from mysql using php in a table, it would be simple if I didn't have other elements that I want to display in the table as well ($value and $sub) which aren't stored in mysql but are calculations.
When I don't try to display it in a table it all works perfectly, and when I add the table I can only get the 'name' to display in the table, If I try to include anything else it fails.
Code below displays 'name' under the table name column, next thing to display would be '$value' under quantity and so on (haven't added anymore columns yet)
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Possible code to render column and row properly.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Thanks
Amit
First thing i want to tell you is mysql_* is officially deprecated as of PHP 5.5. And removed entirely as of PHP 7.0. So please dont use it.
Refer : http://php.net/manual/en/migration55.deprecated.php
For mysqli_* : http://php.net/manual/en/book.mysqli.php
Now about you code :
You are using table tag inside the loop. This will generate multiple tables. Try out this code :
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query("SELECT id_product, name, price FROM elec_guit WHERE id_product='$id'");
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' # $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' [-] [+]'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
$total += $sub;
}
}
?>
</table>
Added new code as per your comment.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. $value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br /></td> </tr>';
}
}
$total += $sub;
}
}
?>
</table>
I moved the table outside of your loop, that will fix a majority of your problems. Another issue is you only had one <td> wrapping the name column in your while loop, but you didn't have a <td> wrapping the other information in that row, so I wrapped that.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' # $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' [-] [+]'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
}
}
?>
</table>
</div>
Use the below code:
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<td><?php echo $get_row['name']; ?></td>
<td><?php echo $value;?> # $<?php echo number_format($get_row['price'], 2); ?> = $<?php echo number_format($sub, 2); ?> [-] [+] <a href="cart.php?delete=<?php echo $id; ?>" >[Delete]</a><br /></td>
</tr>
<?php }
}
$total += $sub;
}
}
?>
</table>
</div>
How can I incorporate my loop below to be exported as a CSV?
<?php $answers = new WP_Query(array('cat' => 25, 'order' => 'DESC')); ?>
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td valign="top">Name</td>
<td valign="top">Submission/production title</td>
<td valign="top">Performance space</td>
</tr>
<?php if ($answers->have_posts()) : while ($answers->have_posts()) : $answers->the_post(); ?>
<tr>
<td><?php echo the_title(); ?></td>
<td><?php echo the_field('submit_submission_production_title'); ?></td>
<td><?php echo the_field('submit_performance_space'); ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
How can I integrate this with fputcsv?
<?php
$list = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
$fp = fopen('file.csv', 'w');
foreach ($list as $line) {
fputcsv($fp, split(',', $line), ',', '"');
}
fclose($fp);
?>
First, the_title and the_field* are functions that do echo by themselves, you don't need echo the_title().
* Assuming that this is from Advanced Custom Fields.
Just build an array, like:
$answers = new WP_Query( array('cat' => 25, 'order' => 'DESC') );
if ( $answers->have_posts() ) {
$list = array( 'Name,Submission/production title,Performance space' );
while ( $answers->have_posts() ) {
$title = get_the_title();
$field1 = get_field('submit_submission_production_title');
$field2 = get_field('submit_performance_space');
$list[] = $title . ',' . $field1 . ',' . $field2;
// IF ENCLOSING DOUBLE QUOTES ARE NEEDED
// $list[] = '"' . $title . '","' . $field1 . '","' . $field2 . '"';
}
}
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>";
I've created a size chart using php and mysql for my website. The table is 9 columns wide, however not all columns are always used.
So far I haven't figured out a way to have it skip columns with NA for the column heading. Hoping someone can shed some light on this or point me in the right direction.
Here is the code:
if (!empty($insizes)) {
?>
<div class="ui-widget infoBoxContainer">
<div class="ui-widget-header ui-corner-top infoBoxHeading">
<span><?php $products_name = $product_info['products_name']; echo $products_name; ?> Sizing Chart</span></div>
<table border="0" cellpadding="5" cellspacing="0" id="sizeChart" bgcolor="#ffffff" width="100%">
<tbody width="90%">
<tr>
<td>Size</td>
<?php foreach ($headings as $headingo) { $heading = strtolower(str_replace(" ", "<br>", $headingo)); ?>
<td><?php echo ($product_info["$heading"]); ?></td><?php } ?>
<td>Price</td>
</tr>
<?php
foreach ($insizes as $size)
{
$sizeo = strtolower(str_replace(" ", "", $size));
$sizeo = str_replace("-", "", $sizeo);
?>
<tr>
<td> <?php echo $size; ?></td>
<?php
foreach ($measurements as $measurement) {
$measurementx = $sizeo . '_' . $measurement;
?>
<td><?php echo number_format($product_info["$measurementx"], 0, '.', ''); ?>"<br><span class="sizeChartSm">(<?php echo number_format($product_info["$measurementx"] * 2.54, 1, '.', ''); ?>cm)</span></td>
<?php
}
?>
<td>
<?php
echo $sizeprices["$size"];
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</p>
<?php
}
Thanks a bunch!
Chris
Edit: Here is the rest of the info.
/* <!--- SIZE CHART ---> */
$sizes = array('3X Small', '2X Small', 'X Small', 'Small', 'Medium', 'Large', 'X Large', '2X Large', '3X Large', '4X Large', '5X Large', 'Twin', 'Full', 'Queen', 'King', 'Standard', 'Queen Deep Pocket', 'King Deep Pocket');
$measurements = array('waistmin', 'waistmax', 'legmin', 'legmax', 'crotchwidth', 'maxhip', 'height');
$headings = array ('heading_1', 'heading_2', 'heading_3', 'heading_4', 'heading_5', 'heading_6', 'heading_7',);
$insizes = array();
$sizeprices = array();
/* <!--- END SIZE CHART ---> */
and
/* <!--- SIZE CHART ---> */
$product_info_query = tep_db_query("select
p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity,
p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added,
p.products_date_available, p.manufacturers_id,
s.*
from
" . TABLE_PRODUCTS . " p,
" . TABLE_PRODUCTS_DESCRIPTION . " pd,
" . products_size_measurements . " s
where
p.products_status = '1' and
p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and
pd.products_id = p.products_id and
s.product_id = p.products_id and
pd.language_id = '" . (int)$languages_id . "'");
/* <!--- END SIZE CHART ---> */
and
/* <!--- SIZE CHART ---> */
if ($products_options_name['products_options_name'] == 'Size') {
array_push($insizes, $products_options['products_options_values_name']);
}
$products_options_array[] = array(
'id' => $products_options['products_options_values_id'],
'text' => $products_options['products_options_values_name']);
/* $products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options[''] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) .') '; */
$final = $products_options['options_values_price'] + $product_info['products_price'];
if ($new_price = tep_get_products_special_price($product_info['products_id'])) {
$price = '<del>' . $currencies->display_price($product_info['products_price'] + $products_options['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($new_price + $products_options['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';
} else {
$price = $currencies->display_price($final, tep_get_tax_rate($product_info['products_tax_class_id']));
}
$name = $products_options['products_options_values_name'];
$sizeprices = array_push_assoc($sizeprices, "$name", "$price");
/* <!--- END SIZE CHART ---> */
Shouldn't I be able to use an if, else statement using flags (such as NA and 0) for the data stored in the DB? Something like so:
<?php if ($heading != "NA") {?>
<td><?php echo ($product_info["$heading"]); ?></td><?php }} else { ?>
<td>Price</td>
</tr>
<?php } ?>
and
<?php if ($measurement != 0) {?>
<td><?php echo number_format($product_info["$measurementx"], 0, '.', ''); ?>"<br><span class="sizeChartSm">(<?php echo number_format($product_info["$measurementx"] * 2.54, 1, '.', ''); ?>cm)</span></td>
<?php
}} else {
?>
<td>
<?php
echo $sizeprices["$size"];
?>
</td>
</tr>
<?php
}}
?>
However I can't seem to get the syntax right and keep throwing T_ELSE errors.
I think your error is coming from having too many brackets for the if else statement. Try this:
<?php if ($measurement != 0) {?>
<td><?php echo number_format($product_info["$measurementx"], 0, '.', ''); ?>"<br><span class="sizeChartSm">(<?php echo number_format($product_info["$measurementx"] * 2.54, 1, '.', ''); ?>cm)</span></td>
<?php
} else {
?>
<td>
<?php
echo $sizeprices["$size"];
?>
</td>
</tr>
<?php
}
?>
Also, your code would look way cleaner if you used short tags to echo your variables in the table like so:
<?php if ($measurement != 0) {?>
<td>
<?=number_format($product_info["$measurementx"], 0, '.', '')?>
<br>
<span class="sizeChartSm">
(<?=number_format($product_info["$measurementx"] * 2.54, 1, '.', '')?>cm)
</span>
</td>
<?php } else { ?>
<td>
<?=$sizeprices["$size"]?>
</td>
</tr>
<?php } ?>
skipping columns will cause the table to be drawn incorrectly.
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
... skipped
<td>3</td>
</tr>
would render as
1 2 3
1 3
not
1 2 3
1 3
You should modify $headings and $insizes to hide some of the columns or change the logic of your script. But I think for you will be easy to modify the arrays that you use to genertate the table.. Please show how you build these 2 arrays.
I'm trying to write some sort of forum homepage, based off work someone else did. Here's what I've got so far:
Forums
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as &$value) {
$data = $db->query("SELECT * FROM tbl_depts WHERE id = '" . $value . "'");
$crumb = $data->fetch_assoc();
$data = $db->query("SELECT * FROM tbl_forums WHERE deptid = '" . $value . "'");
$forumcount = $data->num_rows;
$forum = $data->fetch_assoc();
?>
<div class="h3top"><?php echo $crumb['name']; ?></div>
<div class="info2alt">
<?php
while (($row = $data->fetch_array()) !== FALSE) {
$forum[] = $row;
}
foreach ($forum as $row) {
if ($forumcount >= 1) {
if ($forum['lastpost'] == "") {
$forum['lastpost'] = "--";
}
?>
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo $row['name']; ?></td>
<td width="10%">Threads: <?php echo $forum['threadcount']; ?><br />Posts: <?php echo $forum['postcount']; ?></td>
<td width="35%">Last Post: <?php echo $forum['lastpost']; ?></td>
</tr>
</table>
</div>
<?php
}
?>
<?php if ($forumcount >= 1) { ?>
<div class="info3bottom"></div>
<?php
}
}
?>
</div>
<?php
}
?>
This is the current data in the table:
Now, when I try to use this code, I get this:, such that the first one is shown so many times, however, the next one does not appear.
How do I fix this?
you want to use $value['threadcount'] etc. $forum is the array containing all the rows. i wonder that you get some output at all …
fetch_assoc() will only fetch a single row from the resultset. you'd have to use a loop to fill an array:
while(($row = $data->fetch_assoc()) !== FALSE) {
$forum[] = $row;
}
you can then iterate over your $forum array with a foreach loop:
foreach($forum as $row) {
echo htmlspecialchars($row['threadcount']);
// etc.
}
trying to fix this mess …
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as $crumb) { ?>
<div class="h3top"><?php echo htmlspecialchars($crumb['name']);?></div>
<?
}
$result = $db->query("SELECT * FROM tbl_forums WHERE deptid = " . (int)$id . "");
$forumcount = $result->num_rows;
while(($row = $data->fetch_assoc()) !== FALSE) {
if ($row['lastpost'] == "") { $row['lastpost'] = "--";}
?>
<div class="info2alt">
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo htmlspecialchars($forum['name']); ?></td>
<td width="10%">Threads: <?php echo htmlspecialchars($forum['threadcount']); ?><br />Posts: <?php echo htmlspecialchars($forum['postcount']); ?></td>
<td width="35%">Last Post: <?php echo htmlspecialchars($forum['lastpost']); ?></td>
</tr>
</table>
</div>
<div class="info3bottom"></div>
<?php
}
?>