PDA

View Full Version : Problem Array



james182
December 10th, 2006, 10:09 PM
Whats happened?? it doesn't output the numbers correctly it should look like this:
pausecontent[0]=» title1
pausecontent[1]=» title2
pausecontent[2]=» title3

NOT this:
pausecontent[0]=» title1
pausecontent[0]=» title2
pausecontent[0]=» title3



<?
include ("connect.php");
$result = mysql_query("select * from news ");
$count = mysql_num_rows($result);

for ( $i=0; $i <= $count; $i++) {
while($r=mysql_fetch_array($result)){
$title=$r["title"];
$blurb=$r["blurb"];
$nid=$r["nid"];

echo "pausecontent[$i]=<span class='title'>&raquo; $title<br></span><span class='date'>$date<br></span><span class='content'>$blurb<br><br></span>";
}
}
?>

MTsoul
December 11th, 2006, 01:27 AM
Your for loop is redundant. The while loop loops through all the results one by one. So if you want to display the [$n], you will need a counter that starts out to be 0 (before the while loop begins) and is incremented and displayed in each loop. Get rid of the for loop - the while loop already does what you want.

bwh2
December 12th, 2006, 10:30 PM
mtsoul is right. on top of that, you shoudn't be using the for($i..) loop with results from a db. it is slower than the while. i would also encourage you to echo out that line like this because it will be faster and cleaner:


echo 'pausecontent['.$i.']=<span class="title">&raquo; '.$title.'</span><br /><span class="date">'.$date.'</span><br /><span class="content">'.$blurb.'</span><br /><br />';