Remove empty columns from table drawn using PHP
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>
</d开发者_运维问答iv>
</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.
精彩评论