Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Iterative Best Practices in XHTML+PHP
 
John Josef
Jr. Member
 
Total Posts:  10
Joined:  2008-07-01
 

Hey all,

I have been doing some major tweaking of the template and core files on Magento and I have come across a strange way (that is also inefficient) of doing iterative tables in XHTML.

Example:

<?php $_odd ''?>
            <?php 
foreach ($_orders as $_order): ?>
                
<tr class="<?php echo ($_odd = ($_odd ? 'odd' : '')) ?>">
                    <
td><?php echo $_order->getRealOrderId() ?>&nbsp;</td>
                    <
td><?php echo $this->formatDate($_order->getCreatedAt()) ?> </td>
                    <
td><?php echo $this->htmlEscape($_order->getShippingAddress()->getName()) ?>&nbsp;</td>
                    <
td><?php echo $_order->formatPrice($_order->getGrandTotal()) ?>&nbsp;</td>
                    <
td><em><?php echo $_order->getStatusLabel() ?></em>&nbsp;</td>
                    <
td class="last a-center"><a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order'?></a>
                        
<?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
                            
<span class="separator">|</span>
                            <
a href="<?php echo $this->getReorderUrl($_order) ?>" style="font-weight: bold; color: #FF6600;"><?php echo $this->__('Reorder'?></a>
                        
<?php endif ?>
                    
</td>
                </
tr>
            
<?php endforeach; ?>

This is very strange and is performing way more processing than is necessary. Here is a better way to do this:

<?php foreach ($_orders as $k => $_order): ?>
                
<tr class="<?php echo ($k%2 == 1) ? 'odd' : '') ?>">
                    <
td><?php echo $_order->getRealOrderId() ?>&nbsp;</td>
                    <
td><?php echo $this->formatDate($_order->getCreatedAt()) ?> </td>
                    <
td><?php echo $this->htmlEscape($_order->getShippingAddress()->getName()) ?>&nbsp;</td>
                    <
td><?php echo $_order->formatPrice($_order->getGrandTotal()) ?>&nbsp;</td>
                    <
td><em><?php echo $_order->getStatusLabel() ?></em>&nbsp;</td>
                    <
td class="last a-center"><a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order'?></a>
                        
<?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
                            
<span class="separator">|</span>
                            <
a href="<?php echo $this->getReorderUrl($_order) ?>" style="font-weight: bold; color: #FF6600;"><?php echo $this->__('Reorder'?></a>
                        
<?php endif ?>
                    
</td>
                </
tr>
            
<?php endforeach; ?>

This way is not only faster, but semantically and logically to a developer it should make more sense utilizing features of the foreach function and a simpler way of calculating odd/even numbers.

Also throughout the code there are instances like this but a universal class structure is not used. This should be updated so that you don’t have so many classes defined for every table structure. If you want to customize the look then you use compound classes such as

<tr class="order <?php echo ($k%2 == 1) ? 'odd' : '') ?>

I hope this was informative and if this can be implemented it will save developers and designers a lot of time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
longlong
Jr. Member
 
Total Posts:  5
Joined:  2010-07-21
 

thanks for your reply,
Christian louboutin Shoes

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top