shan2batman Posted August 11, 2016 Share Posted August 11, 2016 Hi, guys i'm trying to fetch all results from DB through a foreach loop. But it is not working as intended, as it brings only the first record and not the others. here is my code:- $sql1="select * from group_posts"; $stmt_t=$conn->prepare($sql1); $stmt_t->execute(); $data_fetch=$stmt_t->fetchAll(); foreach ($data_fetch as $row_d) { $postid=$row_d['gp_id']; $post_auth=$row_d['author_gp']; $post_type=$row_d['type']; $post_title= html_entity_decode($row_d['title']); $post_data= html_entity_decode($row_d['data']); $data1= array($post_data); // $data1= taggingsys($data0); $post_date=$row_d['pdate']; $post_avatar=$row_d['avatar']; $post_uid=$row_d['user_id']; if ($post_avatar != ""){ $g_pic='user/'.$post_uid.'/'.$post_avatar; } else { $g_pic='img/avatardefault.png'; } $user_image="<img src='{$g_pic}' alt='{$post_auth}' title='{$post_auth}' width='30' height='30'>"; $vote_up_count=$project->voteGroupCheck($postid, $_SESSION['id']); } Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/ Share on other sites More sharing options...
Jacques1 Posted August 11, 2016 Share Posted August 11, 2016 The loop doesn't do anything but overwrite the same variables over and over again. How is this supposed to work? Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1535994 Share on other sites More sharing options...
shan2batman Posted August 12, 2016 Author Share Posted August 12, 2016 no, for simplicity's sake i kept the code small. the problem is it isn't fetching the results from $row_d, but i was able to print the $data_fetch results finely.@jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536008 Share on other sites More sharing options...
Jacques1 Posted August 12, 2016 Share Posted August 12, 2016 So did you solve the problem? If not, what is the problem? Be specific and post the real code. Â no, for simplicity's sake i kept the code small. Â Providing fantasy code is a very bad idea when you expect real answers. Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536010 Share on other sites More sharing options...
shan2batman Posted August 12, 2016 Author Share Posted August 12, 2016 (edited) here is the full code. The problem is that the $data_fetch is able to pull all the desired results when i try to var_dump it. But, the variable $row_d is only pulling the first row from the DB. How may i solve it. Please forgive me for not giving adequately specific. @jacques1 <?php include 'includes/header.php'; if (isset($_SESSION['uname'])){ $tag= htmlentities($_GET['tag']); $sql3="select * from user where uname like '%$tag%' and activated='1'"; $stmt_t=$conn->prepare($sql3); $stmt_t->execute(); $tag_fetch=$stmt_t->fetchAll(PDO::FETCH_ASSOC); $sql1="select * from group_posts"; $stmt_t=$conn->prepare($sql1); $stmt_t->execute(); $data_fetch=$stmt_t->fetchAll(); function hashtags($dat, $tag) { $regex="/#+([a-zA-z0-9._-]+)/"; foreach($dat as $d){ $dat1= preg_match($tag, $d); print_r($dat1); } return $dat1; } ?> <div class="container-fluid"> <br><div class="row">Â </div><br> <div class="row"> <div class="col-sm-2"> <?php include_once 'notification/friend_list.php'; ?> <?php include 'groups/group_list.php';?> </div> <div class="col-lg-8"> <table class="main_page"> <td class="div-col-left1"><div class="overflow"><h3>Users</h3> <?php foreach ($tag_fetch as $row_s) { $id_s=$row_s['user_id']; $fname_s=$row_s['fname']; $lname_s=$row_s['lname']; $uname_s=$row_s['uname']; $email_s=$row_s['email']; $phone_s=$row_s['phone']; $photo_s=$row_s['avatar']; $city_s=$row_s['city']; if($photo_s !=""){ echo '<div class="" align="left">'; echo '<a href="home.php?u='.$uname_s.'" target="_blank"><img width="130" height="130" src="user/'.$id_s.'/'.$photo_s.'">'.$fname_s.' '.$lname_s.'('.$uname_s.')</a><br>City:- '.$city_s.'<hr>'; echo '</div>'; } else { echo '<div class="" align="left">'; echo '<a href="home.php?u='.$uname_s.'" target="_blank"><img width="130" height="130" src="img/avatardefault.png">'.$fname_s.' '.$lname_s.'('.$uname_s.')</a><br><hr>'; echo '</div><br>'; } } echo "</div></td><td class='div-col-right1 '><div class='overflow'><h3>Groups</h3>"; foreach ($data_fetch as $row_d) { $postid=$row_d['gp_id']; $post_auth=$row_d['author_gp']; $post_type=$row_d['type']; $post_title= html_entity_decode($row_d['title']); $post_data= html_entity_decode($row_d['data']); $data1= array($post_data); // $data1= taggingsys($data0); $post_date=$row_d['pdate']; $post_avatar=$row_d['avatar']; $post_uid=$row_d['user_id']; if ($post_avatar != ""){ $g_pic='user/'.$post_uid.'/'.$post_avatar; } else { $g_pic='img/avatardefault.png'; } $user_image="<img src='{$g_pic}' alt='{$post_auth}' title='{$post_auth}' width='30' height='30'>"; $vote_up_count=$project->voteGroupCheck($postid, $_SESSION['id']); /*foreach ($data1 as $d) { $d1= explode(" ", $d); if(strpos("$d1", "#$tag") !=FALSE){ echo "hurray"; } } */ if($vote_up_count[0][0]>0){ $vote_like=TRUE; } else { $vote_like=FALSE; } if($vote_up_count[0][1]>0){ $vote_dislike=TRUE; } else { $vote_dislike=FALSE; } include 'ratings/vote_count_g.php'; if ( $vote_like==TRUE && $vote_dislike==FALSE) { $like='<a type="liked" class="btn liked status_like_'.$postid.' liked" attr="'.$postid.'">Liked</a>('.$num_likes[0].')'; $unlike='<a type="unlike" class="btn status_unlike_'.$postid.' unlike" attr="'.$postid.'">Dislike</a>('.$num_unlikes[0].')'; } elseif ($vote_dislike==TRUE && $vote_like==FALSE) { $like='<a type="like" class="btn status_like_'.$postid.' like" attr="'.$postid.'">Like</a>('.$num_likes[0].')'; $unlike='<a type="disliked" class="btn disliked status_unlike_'.$postid.'" attr="'.$postid.'">Disliked</a>('.$num_unlikes[0].')'; } else { $like='<a type="like" class="btn status_like_'.$postid.' like" attr="'.$postid.'">Like</a>('.$num_likes[0].')'; $unlike='<a type="unlike" class="btn status_unlike_'.$postid.' unlike" attr="'.$postid.'">Dislike</a>('.$num_unlikes[0].')'; } #build threads. echo $edit."<div id='PB_".$postid."' value='".$postid."' class='jumbotron'><span id='spinner".$postid."'></span>" . "<legend>{$post_title}" . "<span class='pull-right'>" . "<div class='dropdown'>" . "<button type='button' class='btn btn-danger dropdown-toggle' data-toggle='dropdown' >" . "<span class='glyphicon glyphicon-edit'></span></button>" . "<ul class='dropdown-menu'>" .$edit_btn ." ". $statusdeletebutton ."</ul></div></span><br></legend>" . "<legend>{$post_data}<br><span class='spinner_g_u{$postid}'></span>{$like} | {$unlike} <br>Posted by:{$user_image} {$post_auth}-- {$post_date}<hr><br></legend> <h4><a id='{$postid}' class='btn collap-btn'>Comments</a></h4><div class='collapse scroll-comments' id='toggle{$postid}'>"; #get replies and user image using inner loop $num_rows1=$project->groupcount_replies($postid); $fetch1=$project->group_replies($postid); if ($num_rows1>0) { foreach ($fetch1 as $row1) { $reply_id=$row1['gp_id']; $reply_pid=$row1['pid']; $reply_auth=$row1['author_gp']; $reply_data=$row1['data']; $data11= hashtags($reply_data); $data12= taggingsys($data11); $reply_date=$row1['pdate']; $reply_avatar=$row1['avatar']; $reply_uid=$row1['user_id']; if ($reply_avatar != ""){ $r_pic='user/'.$reply_uid.'/'.$reply_avatar; } else { $r_pic='img/avatardefault.png'; } /* include 'ratings/vote_count_g4comments.php'; $like_r='<a type="like" class="btn status_liker_'.$reply_id.' like_r" attr="'.$reply_id.'">Like</a>('.$num_likes_r[0].')'; $unlike_r='<a type="unlike" class="btn status_unliker_'.$reply_id.' unlike_r" attr="'.$reply_id.'">Dislike</a>('.$num_unlikes_r[0].')'; */ $vote_up_count=$project->voteGroupReplyCheck($_SESSION['id'],$reply_id); if($vote_up_count[0][0]>0){ $vote_like=TRUE; } else { $vote_like=FALSE; } if($vote_up_count[0][1]>0){ $vote_dislike=TRUE; } else { $vote_dislike=FALSE; } include 'ratings/vote_count_g4comments.php'; if ( $vote_like==TRUE && $vote_dislike==FALSE) { $like_r='<a type="liked" class="btn status_liker_'.$reply_id.' liked_r" attr="'.$reply_id.'">Liked</a>('.$num_likes_r[0].')'; $unlike_r='<a type="unlike" class="btn status_unliker_'.$reply_id.' unlike_r" attr="'.$reply_id.'">Dislike</a>('.$num_unlikes_r[0].')'; } elseif ($vote_dislike==TRUE && $vote_like==FALSE) { $like_r='<a type="like" class="btn status_liker_'.$reply_id.' like_r" attr="'.$reply_id.'">Like</a>('.$num_likes_r[0].')'; $unlike_r='<a type="unliked" class="btn disliked_r status_unliker_'.$reply_id.'" attr="'.$reply_id.'">Disliked</a>('.$num_unlikes_r[0].')'; } else { $like_r='<a type="like" class="btn status_liker_'.$reply_id.' like_r" attr="'.$reply_id.'">Like</a>('.$num_likes_r[0].')'; $unlike_r='<a type="unlike" class="btn status_unliker_'.$reply_id.' unlike_r" attr="'.$reply_id.'">Dislike</a>('.$num_unlikes_r[0].')'; } $reply_img="<img src='{$r_pic}' alt='{$reply_auth}' title='{$reply_auth}' width='15' height='15'>"; #build replies echo "<div id='reply_body'>" . "<small>Posted by:{$reply_img} {$reply_auth}-- {$reply_date}</small><br>" . "<legend>{$data12}<br><span class='spinner_g_r{$reply_id}'></span>{$like_r} | {$unlike_r} </legend></div>"; } } echo '<br>' . '<textarea id="reply_textarea_'.$postid.'" class="group_reply1 input-custom2" placeholder="comment\'s"></textarea>' . '<br><span class="group_reply"></span><button action="post_reply" id="'.$postid.'" attr="'.$postid.'" type="b" class="btn btn-warning pull-right btn-sm reply_btn reply">Reply</button><br></div></div>'; } } echo '</div></td>'; ?> </div> <div class="col-sm-2" style="float: right;"> <?php include 'notification/friend_suggestion.php';?> </div> </div> </div> Edited August 12, 2016 by shan2batman Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536023 Share on other sites More sharing options...
Jacques1 Posted August 12, 2016 Share Posted August 12, 2016 But, the variable $row_d is only pulling the first row from the DB. Â That's not possible. Unless the script crashes or the loop is aborted, $row_d will contain each row one after another. Â Put a var_dump($row_d) at the beginning of the loop body (line 68) and tell us the result. Does this show each row? Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536026 Share on other sites More sharing options...
Solution mac_gyver Posted August 12, 2016 Solution Share Posted August 12, 2016 (edited) setting php's error_reporting to E_ALL and display_errors to ON (which should already both be set this way in the php.ini on your development system), should help with finding the cause of the problem. Â does the 'view source' of the page show all the repeated sections of content? maybe there's a broken html comment or tag that's preventing the output from being rendered by the browser. Â also, does the content, that's after the end of the loop, get displayed? Edited August 12, 2016 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536030 Share on other sites More sharing options...
shan2batman Posted August 12, 2016 Author Share Posted August 12, 2016 @mac_gyver after looking in to the source i found a fatal error from a user function that was not yet written. You saved my time boss. Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536038 Share on other sites More sharing options...
Jacques1 Posted August 12, 2016 Share Posted August 12, 2016 And you've hopefully learned to check your error log. Quote Link to comment https://forums.phpfreaks.com/topic/301859-foreach-loop-doesnt-bring-all-the-records-from-the-database/#findComment-1536039 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.