papillonstudios Posted June 4, 2009 Share Posted June 4, 2009 i have research for about almost an hour now and haven't quite found out how to do it I want to use a php for each statement to display the news from a database. i would like to know how to do this. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/ Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 What's wrong with a while loop? Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-848983 Share on other sites More sharing options...
papillonstudios Posted June 4, 2009 Author Share Posted June 4, 2009 a for each only goes to the a certain number a while goes until error and i have only used For each loops, while testing Codeigniter Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-848991 Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 What do you mean a foreach goes to a certain number? You make no sense. Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-848994 Share on other sites More sharing options...
papillonstudios Posted June 4, 2009 Author Share Posted June 4, 2009 theres nothing wrong with an while statement but i want to use a foreach Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849563 Share on other sites More sharing options...
elis Posted June 5, 2009 Share Posted June 5, 2009 I believe s/he means that a while loop will continue until it fails a condition while a foreach will continue until it cycles completely through an array. Sample usage <?php //declare an array - or use an array pulled from a query $var1 = array(); $var1[0] = "a"; $var1[1] = "b"; $var1[2] = "c"; $var1[3] = "d"; $var1[4] = "e"; foreach( $var1 as $content){ echo $content . "<br>"; } ?> Or through using a while: <?php //declare an array - or use an array pulled from a query $var1 = array(); $var1[0] = "a"; $var1[1] = "b"; $var1[2] = "c"; $var1[3] = "d"; $var1[4] = "e"; $count = 4; $i = 0; while($i <= $count) { echo $var1[$i] . "<br>"; $i++; } foreach is a little shorter Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849676 Share on other sites More sharing options...
papillonstudios Posted June 5, 2009 Author Share Posted June 5, 2009 thanks elis for explaining that, but could i get help with what i started this topic for Could you guys help me with getting all the data pulled from my database table for my news feed into an array so i can display them Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849684 Share on other sites More sharing options...
Philip Posted June 5, 2009 Share Posted June 5, 2009 $query = "SELECT * FROM `table`"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo $row['col'], '<br>'; } If you wanted to use a foreach statement, it would seem rather pointless because it would do something like: $query = "SELECT * FROM `table`"; $result = mysql_query($query); $array = array(); // gather each row into the array while($row = mysql_fetch_assoc($result)) { $array[] = $row['col']; } foreach($array as $key=>$value) { echo $value,'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849691 Share on other sites More sharing options...
papillonstudios Posted June 5, 2009 Author Share Posted June 5, 2009 hey thanks man for the help. your a great help. I love this forum, will never leave Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849693 Share on other sites More sharing options...
papillonstudios Posted June 5, 2009 Author Share Posted June 5, 2009 got one more question i want it to how to make the while loop go in reverse order. to the first message is displayed last. <?php //Selecting the News From trhe Table news $query = "SELECT * FROM `news`"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo '<div class="post">'; echo '<h2>', $row['title'], '</h2>'; echo '<small>', $row['date'], ' by ', $row['username'], '</small>'; echo '<br />'; echo '<br />'; echo typography($row['body']); echo '<br />'; echo '<br />'; echo '</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849707 Share on other sites More sharing options...
Philip Posted June 5, 2009 Share Posted June 5, 2009 Use ORDER BY in your MySQL query. Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-849714 Share on other sites More sharing options...
papillonstudios Posted June 6, 2009 Author Share Posted June 6, 2009 thanks i got it working Quote Link to comment https://forums.phpfreaks.com/topic/160867-solved-mysql-php-foreach/#findComment-850381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.