从使用PHP绘制的表中删除空列

时间:2011-08-12 16:11:28

标签: php mysql html

我已经为我的网站使用php和mysql创建了一个大小的图表。该表是9列宽,但并非总是使用所有列。

到目前为止,我还没有找到一种方法让它跳过列标题为NA的列。希望有人可以对此有所了解或指出我正确的方向。

以下是代码:

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
}

非常感谢! 克里斯

编辑:这是剩下的信息。

/* <!--- 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 ---> */

/* <!--- 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 ---> */

/* <!--- 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 ---> */

我是否应该能够使用标记(例如NA和0)的if,else语句来存储在数据库中的数据?像这样:

<?php if ($heading != "NA") {?>
                  <td><?php echo ($product_info["$heading"]); ?></td><?php }} else { ?>
                  <td>Price</td>
                </tr>
                <?php } ?>

<?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
                  }}
                ?>  

但是我似乎无法正确使用语法并继续抛出T_ELSE错误。

3 个答案:

答案 0 :(得分:0)

跳过列会导致表格绘制错误。

<table>
<tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
</tr>
<tr>
   <td>1</td>
   ... skipped
   <td>3</td>
</tr>

将呈现为

1 2 3
1 3

1 2 3
1   3

答案 1 :(得分:0)

您应修改$ headings和$ insizes以隐藏某些列或更改脚本的逻辑。但我认为你可以很容易地修改用于生成表的数组。请展示如何构建这两个数组。

答案 2 :(得分:0)

我认为您的错误来自if else语句的括号过多。试试这个:

<?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
                  }
                ?>  

此外,如果您使用短标记来回显表中的变量,那么您的代码看起来会更清晰:

<?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 } ?>