phpretard Posted February 16, 2008 Share Posted February 16, 2008 The code below wont give the desired result. while($row = mysql_fetch_array($result)) { $url=$row['url']; $id=$row['id']; $num="0"; $pic="messages[$num] = new Array('../$url');"; echo $pic; $num++; } the desired result is messages[0] = new Array('../$url'); messages[1] = new Array('../$url'); messages[2] = new Array('../$url'); messages[3] = new Array('../$url'); and so on... Can someone please throw a bone here? -Anthony Link to comment https://forums.phpfreaks.com/topic/91364-hours/ Share on other sites More sharing options...
phpSensei Posted February 16, 2008 Share Posted February 16, 2008 Try <?php $num=0; while($row = mysql_fetch_array($result)) { $url=$row['url']; $id=$row['id']; $pic="messages[".$num."] = new Array('../$url');"; echo $pic; $num++;} Link to comment https://forums.phpfreaks.com/topic/91364-hours/#findComment-468166 Share on other sites More sharing options...
Ken2k7 Posted February 16, 2008 Share Posted February 16, 2008 Hello, You can't increment a string, which $num was before. Also, you won't get any results from putting $num inside the while loop either because it'll always reset itself to zero. To do this, you have to put it outside the loop. $num = 0; while($row = mysql_fetch_array($result)) { $url=$row['url']; $id=$row['id']; $pic="messages[$num] = new Array('../$url');"; echo $pic; $num++; } Ken Link to comment https://forums.phpfreaks.com/topic/91364-hours/#findComment-468173 Share on other sites More sharing options...
phpretard Posted February 16, 2008 Author Share Posted February 16, 2008 Thank you for the code help and especially the reason why! Link to comment https://forums.phpfreaks.com/topic/91364-hours/#findComment-468177 Share on other sites More sharing options...
Barand Posted February 16, 2008 Share Posted February 16, 2008 Hello, You can't increment a string, ... Want to put money on that? <?php $str = 'a'; for($i=0;$i<=25;$i++){ echo $str++; } ?> --> abcdefghijklmnopqrstuvwxyz Link to comment https://forums.phpfreaks.com/topic/91364-hours/#findComment-468267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.