Jump to content

Recommended Posts

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

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)) {

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?

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>';}
}
}
}
?>

Edited by nonspacial

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){

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.