for loop with count from array, limit output? PHP

Posted by Philip on Stack Overflow See other posts from Stack Overflow or by Philip
Published on 2010-05-10T07:33:21Z Indexed on 2010/05/10 8:24 UTC
Read the original article Hit count: 263

Filed under:
|
|
|
print '<div id="wrap">';
print "<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\">";

 for($i=0; $i<count($news_comments); $i++)
{
 print '
  <tr>
     <td width="30%"><strong>'.$news_comments[$i]['comment_by'].'</strong></td>
      <td width="70%">'.$news_comments[$i]['comment_date'].'</td>
    </tr>
    <tr>
      <td></td>
      <td>'.$news_comments[$i]['comment'].'</td>
  </tr>
    '; 

 }

print '</table></div>';

$news_comments is a 3 diemensional array from mysqli_fetch_assoc returned from a function elsewhere, for some reason my for loop returns the total of the array sets such as [0][2] etc until it reaches the max amount from the counted $news_comments var which is a return function of LIMIT 10. my problem is if I add any text/html/icons inside the for loop it prints it in this case 11 times even though only array sets 1 and 2 have data inside them. How do I get around this?

My function query is as follows:

function news_comments()
 {

   require_once  '../data/queries.php';
   // get newsID from the url
   $urlID = $_GET['news_id'];
   // run our query for newsID information
   $news_comments = selectQuery('*', 'news_comments', 'WHERE news_id='.$urlID.'', 'ORDER BY comment_date', 'DESC', '10'); // requires 6 params
   // check query for results
   if(!$news_comments)
   {
    // loop error session and initiate var
    foreach($_SESSION['errors'] as $error=>$err)
   {
    print  htmlentities($err) . 'for News Comments, be the first to leave a comment!';
   } 
   }
   else
   {

    print '<div id="wrap">';
    print "<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\">";
      for($i=0; $i<count($news_comments); $i++)
             {
      print '

      <tr>
          <td width="30%"><strong>'.$news_comments[$i]['comment_by'].'</strong></td>
         <td width="70%">'.$news_comments[$i]['comment_date'].'</td>
        </tr>
        <tr>
         <td></td>
         <td>'.$news_comments[$i]['comment'].'</td>
      </tr>



     '; 

    }


   print '</table></div>';

   }

 }// End function

Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about array