nonspacial Posted January 23, 2013 Share Posted January 23, 2013 i'm trying to get all table entries from a table called abstrktmainblogs with reference to the page number $pg. the connection and SQL are fine it is something in the foreach loop that is only displaying the 2nd (currently last) entry 7 times on the page instead of both the 1st and 2nd only once each. I want this to effectively be infinite posts always adding new ones to the top of the page. $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); foreach ($blogPosts as $key => $value) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } here it is in code snippet some people prefer either or: <?php $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); foreach ($blogPosts as $key => $value) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } ?> Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/ Share on other sites More sharing options...
Barand Posted January 23, 2013 Share Posted January 23, 2013 your foreach loop is iterating over each of the columns in the first row change $blogPosts= mysqli_fetch_assoc ($r); foreach ($blogPosts as $key => $value) { to while ($blogPosts= mysqli_fetch_assoc ($r)) { Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407700 Share on other sites More sharing options...
nonspacial Posted January 23, 2013 Author Share Posted January 23, 2013 ok so that's got back the 1st one but the 2nd is missing so it's not continuing after it retrieves the 1st row in DB table? Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407704 Share on other sites More sharing options...
nonspacial Posted January 23, 2013 Author Share Posted January 23, 2013 hang on i'm even more confused now this get's more repetitions of the 2nd post: $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); while ($blogPosts= mysqli_fetch_assoc ($r)){ foreach ($blogPosts as $key => $value) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } } ?> <?php $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); while ($blogPosts= mysqli_fetch_assoc ($r)){ foreach ($blogPosts as $key => $value) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } } ?> and this does what i said just a second ago: wysiwyg (); $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); while ($blogPosts= mysqli_fetch_assoc ($r)) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } ?> <?php $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); while ($blogPosts= mysqli_fetch_assoc ($r)) { echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } ?> what the hell? Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407710 Share on other sites More sharing options...
Barand Posted January 23, 2013 Share Posted January 23, 2013 you should have something like this pseudo code run query while (fetch result row) { process row } Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407715 Share on other sites More sharing options...
nonspacial Posted January 23, 2013 Author Share Posted January 23, 2013 ok i think i'm even making it too complicated by ommitting parts and such so here are 3 variations with 3 different results of the full function i am calling in code form only, sorry for any confusion before: This get's me 1 copy of the 1st entry: <?php function blogs ($dbc, $pg) { $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); while ($blogPosts= mysqli_fetch_assoc ($r)) { //foreach ($blogPosts as $key => $value){ echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } //} ?> This gets me 7 copies of the 1st entry: <?php function blogs ($dbc, $pg) { $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); $blogPosts= mysqli_fetch_assoc ($r); while ($blogPosts= mysqli_fetch_assoc ($r)) { foreach ($blogPosts as $key => $value){ echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } } ?> and this gets me 7 copies of the 2nd entry followed by 2 copies of the 1st entry: <?php function blogs ($dbc, $pg) { $q="SELECT * FROM abstrktmainblogs WHERE page_id='$pg' AND pgassoc='$pg' ORDER BY date_posted DESC"; $r = mysqli_query($dbc, $q); //$blogPosts= mysqli_fetch_assoc ($r); while ($blogPosts= mysqli_fetch_assoc ($r)) { foreach ($blogPosts as $key => $value){ echo'<div class="blogPosts"> <table> <tr>'.$blogPosts['title'].' by '.$blogPosts['username'].' on '.$blogPosts['date_posted'].'</tr> <br></br> <tr>'.$blogPosts['post'].'</tr> </table></div> <br></br>'; if (isset($_COOKIE['userid'])) { echo '<form method="post" autocomplete="on"> <input type="text" width="100%" height="30px"> </input> </form>';} } } } ?> Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407720 Share on other sites More sharing options...
Barand Posted January 23, 2013 Share Posted January 23, 2013 You aren't listening. I told what the foreach was doing. Get rid of it. get rid of that first call to mysqli_fetch_assoc(). All you need is the while () loop Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407723 Share on other sites More sharing options...
cyberRobot Posted January 23, 2013 Share Posted January 23, 2013 In the first code block, have you tried getting rid of the duplicate call to mysqli_fetch_assoc()? $blogPosts= mysqli_fetch_assoc ($r); //<-- REMOVE THIS ONE while ($blogPosts= mysqli_fetch_assoc ($r)) { //foreach ($blogPosts as $key => $value){ Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407726 Share on other sites More sharing options...
nonspacial Posted January 23, 2013 Author Share Posted January 23, 2013 brilliant; thankyou. Link to comment https://forums.phpfreaks.com/topic/273537-what-is-wrong-with-this-foreach/#findComment-1407734 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.