shan Posted October 18, 2015 Share Posted October 18, 2015 hi, guys im actually following a tutorial here and i'm perplexed with this error, where there is no output for one object inside a loop. since the tutorial is procedural code and im doing it in oop i dont know where im going wrong.please help me. the code for the complete article.php is here: <?php include_once '../includes/dbconfig.inc.php'; $status2view=$project->statusView($session_uname, $f_uname); //gives output on var dump #row vars to extract user's update data. for ($i= 0; $i >=0 ; $i++) { $id=array_column($status2view ,'update_id'); //gives output on var_dump if ($id==NULL) { break; }else { continue; } $status_replies_=$project->reply2StatusView($id[$i]); var_dump($status_replies_); //no output on var_dump while ($row = $status_replies_) { echo "<pre>"; var_dump($row); echo "</pre>"; $status_reply_id=$row['update_id']; $reply_author=$row['author']; $reply_d=htmlentities($row['update_body']); $reply_data= stripslashes($reply_d); $reply_t= htmlentities($row['title']); $reply_title= stripslashes($reply_t); $account_name=$row['account_name']; $reply_date=$row['time']; $reply_delete_button=""; if ($reply_author==$session_uname || $account_name==$session_uname) { $reply_delete_button="<li><span id='$status_reply_id' class='delete_reply_btn glyphicon glyphicon-remove'><a href='#' title='Delete this comment'>Remove X</a></span></li>"; } $status_replies="<div id='".$status_reply_id."' class='replyboxes'><div><b>Reply by<a href='search_results.php?u=".$reply_author."'>".$reply_author."</a>".$reply_date ."<legend>" . "<b class='caret'><button type='button' class='btn btn-danger dropdown-toggle pull-right' data-toggle='dropdown' aria-expanded='true' ><span class='glyphicon glyphicon-edit'></span> <ul class='dropdown-menu'>".$reply_delete_button." " . "<li><a href='#' class='hidden_text_area glyphicon glyphicon-pencil' title='Edit this comment' >Edit</a></li>" . "<li><a href='report.php?u='".$reply_author."'>Report</a><li></ul>" . "</button></b></legend><br>".$reply_data."</div></div>"; } } while ($row1 = $status2view) { for ($j = 0; $j >= 0; $j++) { $updateid=$row1[$j]['update_id']; $account_name=$row1[$j]['account_name']; $os_id=$row1['os_id']; $author=$row1['author']; $post_date=$row1['time']; $title= htmlentities($row1['title']); $data= htmlentities($row1['update_body']); if ($row1==NULL) { break; } $status_list='<fieldset><div id="'.$updateid.'" class="statusboxes"><div>' . '<legend><h3 class="pull-left">'.$title.'</h3><span class="pull-right">'.$statusdeletebutton.'</span></legend><br>' . $data.'<br><b>Posted by<small><a href="search_results.php?u='.$author.'">'.$author.'</a>'.$post_date.'</small></b>' . '<br><hr style="2px dashed #080808">'.$status_replies . '</div></div>'.$statusui_edit.'</fieldset>'; here is the code for the method: public function statusView($session_uname,$f_uname) { $sql="select * from updates where account_name=:either and type='a' or account_name=:either and type='c' order by time desc limit 20"; $stmth=$this->_db->prepare($sql); $stmth->bindValue(":either",($session_uname|$f_uname)); $stmth->execute(); return $stmth->fetchAll(PDO::FETCH_ASSOC); } public function reply2StatusView($updateid){ $stmth= $this->_db->prepare("select * from updates where update_id=:statusid and type='b' order by time asc"); $stmth->bindparam(":statusid", $updateid); $stmth->execute(); return $stmth->fetchAll(PDO::FETCH_ASSOC); } Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/ Share on other sites More sharing options...
requinix Posted October 18, 2015 Share Posted October 18, 2015 Where is this tutorial you found? Because for ($i= 0; $i >=0 ; $i++) { $id=array_column($status2view ,'update_id'); //gives output on var_dump if ($id==NULL) { break; }else { continue; }is very wrong. For at least three different reasons. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523590 Share on other sites More sharing options...
shan Posted October 18, 2015 Author Share Posted October 18, 2015 (edited) actually the tutorial is not in oop code, im trying to replicate it by using oop. @requinix where am i going wrong?? Edited October 18, 2015 by shan Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523592 Share on other sites More sharing options...
requinix Posted October 18, 2015 Share Posted October 18, 2015 Yeah, I saw the bit where you said the original wasn't OOP code. But that bit I posted isn't OOP either, so I figured it came from the tutorial and you were only putting stuff into classes.  The three problems I saw with it: 1. If $i starts at 0 and only ever increases, $i will always be >=0. The loop cannot possibly end on its own. That's a sign that it's either the wrong structure to use (a while or do/while may be more appropriate, if anything at all) or there is something wrong with the loop as a whole. 2. $id gets its value the exact same way each time through the loop. Nothing changes. You're needlessly recalculating the value over and over again. 3. If $id is null then the loop ends. If $id is not null then the loop begins again at the top (ie, with $i++ then $i>=0 then the $id line). The code after that if block will never execute.  More on topic, var_dump($status_replies_); //no output on var_dumpvar_dump() will always produce output. If there is no output then either you aren't seeing it or the code isn't even executing in the first place. (See #3.) And there are other problems. while ($row = $status_replies_) {4. One = is assignment, two ==s is comparison. Unfortunately neither is appropriate here.5. I take it $status_replies_ is supposed to be an array? Then this should probably be a foreach loop instead.  while ($row1 = $status2view) { for ($j = 0; $j >= 0; $j++) {Same as before. if ($row1==NULL) { break; }6. $row1 never changes value within the loop. Either this code executes on the first time through or the loop never ends. Unless whatever code comes after the stuff you've posted alters it. Or does some weird stuff where it alters $status2view and then manually (see #1) leaves the for loop.  So. What is this tutorial you're working from? I'm worried that you're starting off with bad information. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523595 Share on other sites More sharing options...
mac_gyver Posted October 18, 2015 Share Posted October 18, 2015 (edited) your two class methods will return an array. it's an empty array if the query didn't match any rows or it's an array of the all the rows the query did match, assuming that you are still throwing exceptions for any errors. Â all your for(){} and while(){} loop logic, besides not working, is not necessary. you can test if the returned array is empty or not using, well, empty(). you can loop over the arrays using a foreach(){} loop. Â next, $status_replies_=$project->reply2StatusView($id[$i]); you are running a select query inside of a loop. this is a performance killer due to the round-trip communication it adds between php and the mysql server. for what you are doing, you should run one JOINed query to get the data that you want, then just loop over that data. Â edit: public function statusView($session_uname,$f_uname) { you are passing an array and an index into your function/method to supply an account name. this is not how to write general purpose code. the function/method expects an account name as in input, that's all you should supply as an input parameter, so that you can reuse the function/method regardless of where the input comes from. Â the definition should just be - public function statusView($account_name) {. the code inside the function should just use $account_name, and the calling code would be -Â $status2view=$project->statusView($session_uname[$f_uname]); // supply the account name input, from wherever it is stored Edited October 18, 2015 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523610 Share on other sites More sharing options...
shan Posted October 20, 2015 Author Share Posted October 20, 2015 @mac_gyver thanks for the tips, but while using it i'm able to only get a single output can you please recheck the updated code here and tell me where im going wrong??? <?php $status2view=$project->statusView($session_uname, $f_uname); //gives output on var dump #row vars to extract user's update data. foreach($status2view as $row){ //print_r($row); $id=$row['update_id']; //gives output on var_dump $status_replies_=$project->reply2StatusView($id); foreach ($status_replies_ as $row) { $status_reply_id=$row['update_id']; $reply_author=$row['author']; $reply_d=htmlentities($row['update_body']); $reply_data= stripslashes($reply_d); $reply_t= htmlentities($row['title']); $reply_title= stripslashes($reply_t); $account_name=$row['account_name']; $reply_date=$row['time']; $reply_delete_button=""; if ($reply_author==$session_uname || $account_name==$session_uname) { $reply_delete_button="<li><span id='$status_reply_id' class='delete_reply_btn glyphicon glyphicon-remove'><a href='#' title='Delete this comment'>Remove X</a></span></li>"; } $status_replies="<div id='".$status_reply_id."' class='replyboxes'><b>Reply by<a href='search_results.php?u=".$reply_author."'>".$reply_author."</a>".$reply_date ."<legend>" . "<b class='caret'><button type='button' class='btn btn-danger dropdown-toggle pull-right' data-toggle='dropdown' aria-expanded='true' ><span class='glyphicon glyphicon-edit'></span> <ul class='dropdown-menu'>".$reply_delete_button." " . "<li><a href='#' class='hidden_text_area glyphicon glyphicon-pencil' title='Edit this comment' >Edit</a></li>" . "<li><a href='report.php?u='".$reply_author."'>Report</a><li></ul>" . "</button></b></legend><br>".htmlentities($reply_data)."</div>"; } } foreach ($status2view as $row1) { $updateid=$row1['update_id']; $account_name=$row1['account_name']; $os_id=$row1['os_id']; $author=$row1['author']; $post_date=$row1['time']; $title= $row1['title']; $data= $row1['update_body']; $statusdeletebutton=''; if ($author==$session_uname || $account_name==$session_uname) { $statusdeletebutton='<li>' . '<a href="#" id="'.$updateid.'" class="hidden_text_area glyphicon glyphicon-trash delete_reply_btn" title="Delete this status and its replies">Remove</a></li>'; } $status_list= $statusui_edit .'<div id="'.$updateid.'" type="'.$updateid.'" class="statusboxes jumbotron">' . '<h3 style="color:black; margin-bottom:5px; margin-top:5px;" class="pull-left"><div id="'.$updateid.'" class="title_s_2copy" value="'.html_entity_decode($title).'">'.html_entity_decode($title).'</div></h3>' . '<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">' . '<li><a href="#" class="hidden_text_area glyphicon glyphicon-pencil" title="Edit this status" >Edit</a></li>'.$statusdeletebutton.'</ul></div></span><br><hr><span class="pull-left data_s_2copy" id="'.$updateid.'" value="'.html_entity_decode($data).'" style="font-size:9px; margin-bottom:0px; margin-top:0px; text-align:left; color:black;">' . html_entity_decode($data).'</span><br><br><hr><b style="text-align:right; color:black;"><small>Posted by:- <a href="search_results.php?u='.$author.'">'.$author. '</a> '.$post_date.'</small></b>' . '<br>'.$status_replies.'</div>'; if ($is_friend==TRUE||$session_uname==$f_uname) { $status_list.= '<textarea id="'.$updateid.'" class="status_update input-custom2" placeholder="comment\'s"></textarea>' . '<button id="'.$updateid.'" type="b" class="btn btn-warning pull-right btn-sm">Reply</button></div>'; } } thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523791 Share on other sites More sharing options...
mac_gyver Posted October 20, 2015 Share Posted October 20, 2015 your program logic makes no sense. you are looping over the first query result to just get the id values, looping over any results from the second query for each id, then at the end looping over the first query result again. you are also building the output in variables but you are re-assigning the variable, rather than concatenating to it, on each pass through the loops. Â do what i suggested and run one JOINed query to get the data you want. you will have just one loop and a minimum of code to produce the output. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523817 Share on other sites More sharing options...
mac_gyver Posted October 20, 2015 Share Posted October 20, 2015 i looked at your CLASS code closer (i initially misread the $session_uname|$f_uname) and doing that won't do what you think. that's producing the bitwise OR between those two values, in the php code. Â i suspect you want a logical OR in the sql query statement, which would require sql syntax to do that. you cannot bind sql syntax, only values. you would need to form the correct sql syntax, with place-holders for EACH value, then bind each value. Â also, using the same place-holder name more than once is not proper usage. it does work with emulated prepared statements, but this is likely a bug that could get fixed at any time and you shouldn't rely on it in your code. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523818 Share on other sites More sharing options...
shan Posted October 21, 2015 Author Share Posted October 21, 2015 ah! finally got the values looped after echoing the $status_list at the end of the last loop. thanks for the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/298666-output-not-giving-inside-loop/#findComment-1523887 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.