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