phphtmlsmartysmarty3smarty2

php Smarty section loop two empty <td> are getting created unnecessarily


I am trying to list the nested while loop data from PHP in my site. I am using smarty template engine. I am able to transfer the data and list them successfully. Everything is going as needed but one strange problem has occurred. I never got this before. In my smarty template under section looping in every first <tr> block one ending blank <td> and on every second <tr> block a starting blank <td> is automatically created.

PHP Code

// Fetch Product Specification Groups
$pro_attr = $pdo->prepare("SELECT * FROM product_attributes WHERE pat_product = :id GROUP BY pat_group");
$pro_attr-> bindValue(':id', $_GET['id']);
$pro_attr-> execute();

$data = array();
while($pro_attr_res = $pro_attr->fetch()){
  $nameattrGroup = $pdo->prepare("SELECT * FROM attribute_groups WHERE atg_id = :id");
  $nameattrGroup-> bindValue(':id', $pro_attr_res['pat_group']);
  $nameattrGroup-> execute();
  $nameattrGroup_res = $nameattrGroup->fetch();

  $atrGrpName = $nameattrGroup_res['atg_name'];

  $selectAttr = $pdo->prepare("SELECT * FROM attributes WHERE atr_group = :group");
  $selectAttr-> bindValue(':group', $pro_attr_res['pat_group']);
  $selectAttr-> execute();

  while($selectAttr_res = $selectAttr->fetch()){
    $patValue = $pdo->prepare("SELECT * FROM product_attributes WHERE pat_id = :id");
    $patValue-> bindValue(':id', $selectAttr_res['atr_id']);
    $patValue-> execute();
    $patValue_res = $patValue->fetch();

    $v1 = $selectAttr_res['atr_name'];
    $v2 = $patValue_res['pat_value'];

    $data[$atrGrpName][] = $selectAttr_res;
    $data[$atrGrpName][] = $patValue_res;
    // echo "<pre>";
    // print_r($data);
  }
}

$smarty->assign('attrs', $data);

Smarty Code

{foreach from=$attrs key=atrGroup item=atrs}
    <h3>{$atrGroup}</h3>
    <table class="table table-striped table-hover">
        {section loop=$atrs name=d}
            <tr>
                <td class="col-md-4">{$atrs[d].atr_name|default}</td>
                <td>{$atrs[d].pat_value|default}</td>
            </tr>
        {/section}
    </table>
{/foreach}

Expected Output inside {section loop=$atrs name=d}

Attribute 1     Value 1
Attribute 2     Value 2
Attribute 3     Value 3
Attribute 4     Value 4

Current Output

Attribute 1     
                Value 1
Attribute 2     
                Value 2

These blank <td> blocks were showing error Notice: Undefined index: pat_value in E:\xampp\htdocs\flexicart\tmp\903f4c940c0e368df27c7e92a672b94ff4534717_0.file.product.tpl.cache.php on line 163 and Notice: Undefined index: atr_name in E:\xampp\htdocs\flexicart\tmp\903f4c940c0e368df27c7e92a672b94ff4534717_0.file.product.tpl.cache.php on line 161. But since I used |default the error is hidden but still exists creating those extra <td> blocks. Please help.


Solution

  • The problem is coming from here:

    $data[$atrGrpName][] = $selectAttr_res;
    $data[$atrGrpName][] = $patValue_res;
    

    This puts your atr_name and pat_value in consecutive rows in the array, and is the cause of the errors that you are masking.

    I'm not exactly sure what other data from $selectAttr_res and $patValue_res you might need elsewhere, but addressing just the issue at hand, you can do the following...

    $data[$atrGrpName][] = array('atr_name' => $selectAttr_res['atr_name'], 'pat_value' => $patValue_res['pat_value']);