Jump to content

problem with limiting foreach loop


ryan king

Recommended Posts

I want to limit my results printed. and I cant seem to figure it out since I have other if statements around my loop for display purposes. here is my code:

 

$row_count = 0; 
$z = new zipcode_class; 
$zips = $z->get_zips_in_range($_GET['zip_code'], $_GET['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); 
// add searched for zip to $zips array 
$zips[$_GET['zip_code']] = 0; 
$row_count = 0; 

// this is just to initialise the variable:
$result_array = array();

// first we gather all the results into a single array:
foreach ($zips as $key => $value){
//find all locations within range using returned zipcode values 
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key'") or die (mysql_error()); 
while ($row = mysql_fetch_array($sql_events)) 
{
	// we just add this row to the array:
	$result_array[] = $row;
}
}

// config i guess 
$entriesperline=4;
$counter=1;


print "<table>";


// loop through each result row.

foreach ($result_array As $row){






 if($counter%$entriesperline==1){
	print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}else if($counter%$entriesperline==0){
	print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>";
}else{
	print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}
$counter++;

} //exit loop
if($counter%$entriesperline!=0){
print "</tr>";
}
print "</table>";

?>

Link to comment
https://forums.phpfreaks.com/topic/142402-problem-with-limiting-foreach-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.