Alex7
Alex7

Reputation: 560

Best practice when echo repeated code

I have a question that bothers me for a long time. Lets say i have a code with several nested div's and span's. All these compose a square with an image inside.

echo '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';

Only that this code has about 15 rows.

From what i know when echo-ing the results from db in that form i put in the loop the whole html code. It looks clumsy this way.

It is there a better practice ?

foreach ($query->result() as $row)
{
   $row->address_link=strtolower($row->network);
    echo '<li class="col-md-3 isotope-item '.$row->network.'">';    
                                echo '<div class="portfolio-item img-thumbnail">';
                               echo '<table border="0"><tr>';
                                    echo '<a href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info">';                                     
                                       echo '<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">';
                                        echo '<span class="thumb-info-title">';
                                            echo '<span class="thumb-info-inner">'.$row->value.' Euro</span>';                                            
                                        echo '</span>';
                                        echo '<span class="thumb-info-action">';
                                            echo '<span title="Universal" href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>';     
                                        echo '</span>'; 
                                    echo '</a>';
                                echo '</div>';
                                 echo '</tr><tr>';
                            echo '<span class="thumb-info-type">'.$row->value*$row->rate.' Eur</span>';
                            echo '</tr></table>';
                            echo '</li>';    
}

Upvotes: 0

Views: 121

Answers (2)

d.raev
d.raev

Reputation: 9546

If you are new to php you can define a function for this:

function wrapImage($src){
   return '<div> <div> <div> <div> <span> <img src='.$src.'> </span></div></div></div>';
}

And just use echo wrapImage($src) where you need it with different params.

EDIT: consider following way of presenting the data:

<?php 
    $query = 'select * from Unicorns';
    foreach ($query->result() as $row){
    $row->address_link=strtolower($row->network);
?>   
    <!-- html -->
    <li class="col-md-3 isotope-item <?php echo $row->network; ?>">  
        <div class="portfolio-item img-thumbnail">
            <table border="0"><tr>
                <a href="order/<?php echo $row->address_link.'/'.$row->value; ?>" class="thumb-info">                                   
                    <img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">
                        <span class="thumb-info-title">
                        <span class="thumb-info-inner"><?php echo $row->value; ?> Euro</span>                                            
                    </span>
                    <span class="thumb-info-action">
                        <span title="Universal" href="order/<?php echo $row->address_link.'/'.$row->value ?>" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>  
                    </span>
                </a>
            </div>
            </tr><tr>
        <span class="thumb-info-type"><?php echo ($row->value*$row->rate); ?> Eur</span>
        </tr></table>
        </li>
     <!-- /html -->
<?php } ?>

It is called spaghetti code .. and it is NOT the best practice but is better then your example in case the HTML is more then the PHP data.

Upvotes: 3

Adam Fischer
Adam Fischer

Reputation: 1100

first of all, dont use echo in loops (optimalization), store your output in variable and print it only once.

Repeated code can be stored inside function

function square($image){
  return '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
}
$output = '';
while ($loop){
  $output .= square($image);
}
echo $output

Upvotes: 1

Related Questions