tags:

views:

195

answers:

3
+7  A: 

Personally I would keep the HTML section for printing out values only, not doing database connections, calling functions, and so on. Something like this:

<?php
$rows = array();
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{
    $row['cquote_hi'] = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
    $rows[] = $row;
}
?>

<table class="result">
    <?php foreach ( $rows as $row ) : ?>
    <tr>
    <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
    <td style="font-size:16px;"><?php echo $row['cquote_hi']; ?></td>
    <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
    <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
    </tr>
    <?php endforeach; ?>
</table>

If your server setup allows it, you can echo variables with short tags: <?=$row['cquote_hi']?>

I don't know what the h() function is doing but you could perhaps call it for each variable in the original while loop, then just echo the variables.

DisgruntledGoat
I'm much happier with the concept of keeping the database stuff separate from the output. Our web app doesn't do this, and it's a bloody nightmare now when we want to refactor it.BUT, don't use short_tags. EVER. You might save a little bit of typing, but the clarity of using the long version is much better (and works no matter what your server configuration is).
Stephen Orr
Stephen: short tags FTW. There's a reason every templating language uses some form of them.
notJim
A: 

UPDATED

if you really want separate your HTML from your PHP code IMHO you need to do like this:

  // first, separate the msql_query and put it into a function
  // this will give you always an array for easy access every time you need it!

    function getAthors() {
    $rows = array();
    while ( $row = mysql_fetch_array( $result , MYSQL_ASSOC ) ) {
    extract($row);
    $cQuote = highlightWords(htmlspecialchars($cQuotes), $search_result);
    array_push( $rows ,  h($cArabic) , $cQuote , h($vAuthor) , h($vReference) );
    } return $rows; }     

  // use printf for Output a formatted string - built it in a function that 
  // support array like this...
    function genAuthorTbl($format, $arr) { 
    return call_user_func_array('printf', array_merge((array)$format, $arr)); 
    }     

  // now in your HTML simply call the two function...       
<?php genAuthorTbl('
     <table>  
      <tr>
       <td>%s</td>
        <td>%s</td>
         <td>%s</td>
        <td>%s</td>  
       </tr>
    </table>', getAthors() ); ?>
aSeptik
A: 

This would be my way:

<?

echo '<table class="result">';

while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {

    $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);

    echo '
    <tr>
        <td style="text-align:right; font-size:15px;">'.h($row["cArabic"]).'</td>
        <td style="font-size:16px;">'.$cQuote.'</td>
        <td style="font-size:12px;">'.h($row["vAuthor"]).'</td>
        <td style="font-size:12px; font-style:italic; text-align:right;">'.h($row["vReference"]).'</td>
    </tr>';
}

echo '</table>';

?>

All php...

Zuul
Still no reason for this answer to have a vote down...
Zuul