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));
        }

    }
share|improve this question

80% accept rate
would you able to post the part of added text/html/icons inside the for loop, i.e error getting part in html? – Karthik May 10 '10 at 7:44
Hi Karthik, Sorry I didn't seen your responce there. Any text or icon I use in the table inside the loop print's 10 times, which was called from mysqli_query(LIMIT 10). Even though only 2 comments were made its adding that text or icon to those other 8 empty values. – Philip May 10 '10 at 8:11
Just like to point out, you should really sanitize the user input and be using mysqli_real_escape_string – Wizzard May 10 '10 at 8:32
escape what values wizzard? ive not created a form to add comments yet until i setup my sessions. The return values will be striped of any slashes when returned + converted to htmlentities. – Philip May 10 '10 at 8:53
feedback

3 Answers

up vote 0 down vote accepted

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'];
}
share|improve this answer
Top man matt! That has fixed her! Many thanks guys. – Philip May 10 '10 at 8:28
feedback

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.

share|improve this answer
feedback

As I understand from your code.

  1. $news_comments should be the data result from your sql query. Just to clarify limit 10 doesn't force the result to be 10 rows. the number of rows return depend on your sql results.

  2. You are using for($i=0; $i

If you want to loop through all $news_comments, you can just do

foreach ($news_comments as $i=>$news_comment) {
  printf($news_comment);
}

$news_comment would be equal to your $news_comments[$i]

share|improve this answer
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 May 10 '10 at 7:57
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.