• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] View Table Layout Question

#1
On my view I have a table which has foreach in side a foreach.

As you can see I also have in the tbody. TR inside a TR if you understand.

My question is is it safe and OK to place a tr inside another tr like I have done

Quote:<div class="panel-body">

<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td>Category Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>


<?php foreach($categories as $category) { ?>

<tr>
    <td><?php echo $category['name']; ?></td>
    <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' . $category['category_id']);?>">Edit</a></td>
    <?php foreach($category['sub_categories'] as $subcategory) { ?>
    <tr>
        <td><?php echo $subcategory['name']; ?></td>
        <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' . $subcategory['category_id']);?>">Edit</a></td>
    </tr>
    <?php }?>
</tr>
<?php }?>


</tbody>
</table>
</div>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
No, it is definitely not ok to do that.

If you do this the browser will do the best it can to interpret your HTML, but you will get wildly different interpretations of your markup errors on different browsers.

You can embed a table in a table cell, but you would need the full table markup.
Reply

#3
(09-22-2015, 01:06 AM)PaulD Wrote: No, it is definitely not ok to do that.

If you do this the browser will do the best it can to interpret your HTML, but you will get wildly different interpretations of your markup errors on different browsers.

You can embed a table in a table cell, but you would need the full table markup.

Do you have any examples? I have attached image to what it looks like now.


Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#4
try something like this and make your own css rule on the tr.category class and its childs

PHP Code:
<?php foreach($categories as $category) : ?>

<tr class="category">
    <td><?php echo $category['name']; ?></td>
    <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $category['category_id']);?>">Edit</a></td>
</tr>
<?php foreach($category['sub_categories'] as $subcategory) : ?>
    <tr>
        <td><?php echo $subcategory['name']; ?></td>
        <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $subcategory['category_id']);?>">Edit</a></td>
    </tr>
<?php 
endforeach;
endforeach;
?>
Reply

#5
(09-22-2015, 02:23 AM)sintakonte Wrote: try something like this and make your own css rule on the tr.category class and its childs


PHP Code:
<?php foreach($categories as $category) : ?>

<tr class="category">
    <td><?php echo $category['name']; ?></td>
    <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $category['category_id']);?>">Edit</a></td>
</tr>
<?php foreach($category['sub_categories'] as $subcategory) : ?>
    <tr>
        <td><?php echo $subcategory['name']; ?></td>
        <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $subcategory['category_id']);?>">Edit</a></td>
    </tr>
<?php 
endforeach;
endforeach;
?>

Ok Thank you.

And I see you use endforeach but I use <?php } ?> and end of it most of the times what's better?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#6
you can use both
i use it in cases where i have nested loops because its more clear in my opinion Wink


i think in your case its debatable to use more than one view for simplification
for example:

your main view
PHP Code:
<div class="panel-body">

<
table class="table table-striped table-bordered table-hover">
<
thead>
<
tr>
<
td>Category Name</td>
<
td class="text-right">Action</td>
</
tr>
</
thead>
<
tbody>


<?
php foreach($categories as $category) { ?>

    $arrViewData = array("category" => $category);
    $this->load->view("category-list-view", $arrViewData);
}
?>
</tbody>
</table>
</div> 

the category-list-view.php should look like

PHP Code:
<tr>
 
   <td><?php echo $category['name']; ?></td>
    <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $category['category_id']);?>">Edit</a></td>
</tr>

<?php 
foreach($category['sub_categories'] as $subcategory) { ?>
    <tr>
        <td><?php echo $subcategory['name']; ?></td>
        <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $subcategory['category_id']);?>">Edit</a></td>
    </tr>
<?php
}
?>

it looks much more clearer and you shifted away from your nested foreach approach in one view
Reply

#7
(09-22-2015, 02:50 AM)sintakonte Wrote: you can use both
i use it in cases where i have nested loops because its more clear in my opinion Wink


i think in your case its debatable to use more than one view for simplification
for example:

your main view

PHP Code:
<div class="panel-body">

<
table class="table table-striped table-bordered table-hover">
<
thead>
<
tr>
<
td>Category Name</td>
<
td class="text-right">Action</td>
</
tr>
</
thead>
<
tbody>


<?
php foreach($categories as $category) { ?>

    $arrViewData = array("category" => $category);
    $this->load->view("category-list-view", $arrViewData);
}
?>
</tbody>
</table>
</div> 

the category-list-view.php should look like


PHP Code:
<tr>
 
   <td><?php echo $category['name']; ?></td>
    <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $category['category_id']);?>">Edit</a></td>
</tr>

<?php 
foreach($category['sub_categories'] as $subcategory) { ?>
    <tr>
        <td><?php echo $subcategory['name']; ?></td>
        <td class="text-right"><a href="<?php echo base_url('admin/catalog/category/edit/' $subcategory['category_id']);?>">Edit</a></td>
    </tr>
<?php
}
?>

it looks much more clearer and you shifted away from your nested foreach approach in one view


Thanks will keep this in mind thanks
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.