Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 776943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:31:51+00:00 2026-05-14T19:31:51+00:00

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

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:31:52+00:00Added an answer on May 14, 2026 at 7:31 pm

    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'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Print all 12 verses of the popular holiday song . By 12 verses I
The print functionality of Excel (using VBA) is extremely slow. I'm hoping someone has
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
I want to print the first 10000 prime numbers. Can anyone give me the
When I try to print a string in a Windows console, sometimes I get
I need to print out data into a pre-printed A6 form (1/4 the size
Can i print out a url /admin/manage/products/add of a certain view in a template?
How can I elegantly print the date in RFC822 format in Perl?
When I print "\[\e[34m\]sometext" I get some text in blue, but can I specify
I find print_r in PHP extremely useful, but wonder if there is anything remotely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.