开发者

for loop with count from array, limit output? PHP

print '<div id开发者_C百科="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));
        }

    }


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'];
}


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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜