Jump to content

output not giving inside loop


shan

Recommended Posts

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);
 
        }
Link to comment
Share on other sites

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_dump
var_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.

Link to comment
Share on other sites

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 by mac_gyver
  • Like 1
Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.