views:

212

answers:

3
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.

EDIT: This is my selectQuery() for kemp and or anyone else who can maybe fix it up a little, its still very much a WIP so its not complete.

$_SESSION['errors'] = array();

    function selectQuery($select, $tbl, $where, $order, $scroll, $limit)
    {
        global $mysqli;
        require_once  '../config/mysqli.php';
        $query = "SELECT $select FROM $tbl $where $order $scroll LIMIT $limit";

        if($result = mysqli_query($mysqli, $query))
        {
            $num = mysqli_num_rows($result);

            if(!$num > 0)
            {
                array_push($_SESSION['errors'], 'No Results found : ');
            }
            else
            {

                    for($i=0; $i<=$limit; $i++)
                    {


                    $data[$i] = mysqli_fetch_assoc($result);
                    }
                return  $data;
                mysqli_free_result($result);
            }
        }
        else
        {
            print('Error: ' . mysqli_error($mysqli));
        }

    }
A: 
Darkerstar
Hi Darkerstar, thanks for your responce.The array is returning correct, thats not the issue I have. In this case it returns the correct amounts (2) comments but when I add some text like "comment by: " It prints that 11 times. Therefore I need to stop it from displayin for empty array values, if you follow?
Philip
A: 

MySQL's LIMIT is just that; a maximum limit. It doesn't enforce the number of results returned. The behaviour of the selectQuery() function is wrong if it is returning an array with 10 elements, regardless of the number of results found. I would fix this behaviour rather than working around it.

If that's not possible for whatever reason, you can add a conditional within your for loop:

for($i=0; $i<count($news_comments); $i++) 
{
    if( ! empty($news_comments[$i])
    {
        print '<tr>...</tr>'; 
    }
}

For the record, I'm with DarkerStar on the foreach. There are certain situations where a traditional for loop is needed, but your's isn't one of them.

foreach($news_comment as $comment)
{
    echo $comment['comment'];
    echo $comment['comment_by'];
    echo $comment['comment_date'];
}
MatW
Top man matt! That has fixed her! Many thanks guys.
Philip
A: 

Judging from your comments, I believe the problem is in the function that builds the $news_comments array, i.e. it always builds an array with 11 values, and the point is that you don't see them printed if you don't add external content, because it just prints empty rows/cells. A loop can't be executed a different number of times if you only change what it prints inside.

You should post the selectQuery() function.

kemp