Jump to content

not able to print values in php


shan2batman
Go to solution Solved by mac_gyver,

Recommended Posts

hi, guys im trying to echo out some values. but, am having a hard time to echo it out. when i try to print it with print_r its printing but not echoing.So, checked the source code and foundout that the document is not printing after the header. so, changed the file permissions to 777 (im using ubuntu).still it is not working, dont know what to do???

 

i have attached a screenshot of my source code in the browser below:-

 

 

here is my code:-

<?php

include 'includes/header.php';
if(!isset($_SESSION['uname'])){
    echo 'You must login to view this';
   
}
$g="";
$gname="";
$gcreation="";
$glogo="";
$invrule="";
$privrule="";
$creator="";
$gmembers="";
$moderators=array();
$approved=array();
$pending=array();
$all=array();
$joinbtn="";
$addmembers="";
$addadmin="";
$profile_pic_btn="";
$avatar_form="";
$mainposts="";
if(isset($_GET['g'])){
    $g=htmlentities($_GET['g']);
    print_r($g);
}  else {
    header("location: index.php?nogroup4u");
    //exit();
}
#make sure the group exists & get data.
$query="select * from groups where name=:g limit 1";
$stmt=$conn->prepare($query);
$stmt->bindValue(":g",$g);
$stmt->execute();
$numrows=$stmt->rowCount();
$fetch1=$stmt->fetchAll();
if ($numrows<1) {
    echo 'That group doesnt exist';
    //exit();
}
 else {
    foreach ($fetch1 as $row) {
    
        $g_id=$row['g_id'];
        $gname=$row['name'];
        $gcreation=$row['creation'];
        $glogo=$row['logo'];
        $invrule=$row['invrule'];
        $creator=$row['creator'];
    
        $profile_pic_btn.='<img src="group/'.$g_id.'/'.$glogo.'" alt="'.$gname.'">';
    }
  
}
#set session for group.
$_SESSION['group']=$gname;

#get member data
$sql1="select g.mname,g.approved,g.admin,u.avatar,u.user_id,u.uname from gmembers as g left join user as u on u.uname=g.mname where g.gname=:g";
$stmt1=$conn->prepare($sql1);
$stmt1->bindValue(":g",$g);
$stmt1->execute();
$row_fetch=$stmt1->fetchAll();

foreach ($row_fetch as $row1) {
    $mName=$row1['mname'];
    $app=$row1['approved'];
    $admin=$row1['admin'];
    $avatar=$row1['avatar'];
    $u_id=$row1['user_id'];
       
    #set user image
    if ($avatar!="") {
        $member_pic="user/".$u_id."/".$avatar;
    }  else {
        $member_pic="img/avatardefault.png";
    }
    #determine if approved
    switch($app){
        case 0:
            array_push($pending, $mName);
            array_push($all, $mName);
            break;
        case 1:
            array_push($approved, $mName);
            array_push($all, $mName);
            break;
       }
       print_r($app);   
#determine if admin
       if($admin==1){
           array_push($moderators, $mName);
       }
       $mod_count=count($moderators);
       $app_count=  count($approved);
       $pend_count=count($pending);
       if ($app==1) {
           $gmembers.='<a href="home.php?u='.$mName.'?v="'.$_SESSION['uname'].'"><img src="'.$member_pic.'" width="70" height="70"></a>';
       }
       
       
       }
    if(isset($_SESSION['uname'])&&(!in_array($_SESSION['uname'], $all))){ 
        $joinbtn.= "<button id='joinBtn' action='joingroup'>Join group</button>";
  
        
    }
    #pending members section for admin
    if(in_array($_SESSION['uname'], $moderators)){
        $addmembers="<h3>Pending Members</h3>";
        for ($x = 0; $x < $pend_count; $x++) {
            $addmembers="<a href='home.php?u={$pending[$x]}?v={$_SESSION["uname"]}'>{$pending[$x]}</a><br>
            <button id='appBtn' action='approve_member'>Approve</button> | "
            . "<button id='decBtn' action='decline_member'>Decline</button>";
        }
    }
    if(in_array($_SESSION['uname'], $moderators)){
        $addadmin="<h3>Add Admin</h3><input type='text' name='new_admin' id='new_admin'><button id='add_adm'>Add</button>";
     }
$sql="select g.*,u.avatar,u.user_id from group_posts as g left join user as u on username=g.author where g.gname=:g and type='0' order by pdate desc";
$stmt2=$conn->prepare($sql);
$stmt2->bindValue(":g",$g);
$stmt2->execute();
$num_rows=$stmt2->rowCount();
$fetch=$stmt2->fetchAll();
if ($num_rows>0) {
    foreach ($fetch as $v) {
        $postid=$v['id'];
        $post_auth=$v['author'];
        $post_type=$v['type'];
        $post_data=$v['data'];
        $post_date=$v['pdate'];
        $post_avatar=$v['avatar'];
        $post_uid=$v['user_id'];
           if ($post_avatar != ""){
          $g_pic='user/'.$post_uid.'/'.$avatar;
      }  else {
          $g_pic='img/avatardefault.png';
          }
        $user_image="<img src='".$g_pic."' alt='{$post_auth}' title='{$post_auth}' width='50' height='50'>";
       #build threads.
        $main_posts="<div id='PB_".$postid."' class='jumbotron'>"
                . "<legend>Posted by:{$g_pic}' '{$post_auth}-- {$post_date}</legend><br>"
                . "{$post_data}<br><hr><br>";
                #get replies and user image using inner loop
                $sql2="select g.author,g.data,g.pdate,u.avatar,u.user_id from group_posts as g left join users as u on u.uname=g.author where pid=:post_id";
                $stmt3=$conn->prepare($sql2);
                $stmt3->bindValue(":post_id",$postid);
                $stmt3->execute();
                $num_rows1=$stmt3->rowCount();
                $fetch1=$stmt3->fetchAll();
                if ($num_rows1>0) {
                    foreach ($fetch as $row1) {
                        $reply_auth=$row1['author'];
                        $reply_data=$row1['data'];
                        $reply_date=$row1['date'];
                        $reply_avatar=$row1['avatar'];
                        $reply_uid=$row1['user_id'];
                        if ($reply_avatar != ""){
          $r_pic='user/'.$reply_uid.'/'.$avatar;
      }  else {
          $r_pic='img/avatardefault.png';
          }
          $reply_img="<img src='".$r_pic."' alt='{$reply_auth}' title='{$reply_auth}' width='50' height='50'>";
      #build replies
          $main_posts="<div id='reply_body' class='jumbotron'>"
                . "<legend>Posted by:{$g_pic}' '{$reply_auth}-- {$reply_date}</legend><br>"
                . "{$reply_data}</div><br><hr><br>";
          }
          $main_posts='<br>'
                  . '<textarea id="reply_textarea_'.$postid.'"  class="group_reply_ input-custom2" placeholder="comment\'s"></textarea>'
                            . '<button id="'.$postid.'" attr="'.$postid.'" type="b" class="btn btn-warning pull-right btn-sm reply_butn reply">Reply</button></div>';
                }
                
                }
}

?>
<div class="container-fluid">
<br><div class="row"> </div><br>
<div class="row">
    <div class="col-sm-2">

</div>    
<div class="col-lg-8">
<?php echo $gmembers."".$addadmin."".$addmembers;?>
</div>
<div class="col-sm-2">

</div>  
</div>
</div>
<script type="text/javascript">
$(".fancybox-effects").fancybox();
$("button.delete_photo").click(function(){
 
    var del_id = $(this).attr('id');
var del_attr=$(this).attr('attr');
 $.post("others/delete_photo.php",
 {id:del_id},function(data){
$("."+del_id).slideUp('slow', function() {$(this).remove();});
 
    }
 );   

 
});
$(".upload_button").click(function()
{
    
});
</script>
</body>
</html>

any help would be appreciated :(

post-185217-0-17405800-1452543537_thumb.png

Link to comment
Share on other sites

  • Solution

do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects?

 

do you have the PDO error mode set to exceptions so that any PDO statement that throws an error would cause an exception so that your main code only has to deal with error free execution of the PDO statements?

Link to comment
Share on other sites

@requinix yes, it is defined. turned error_reporting to E_ALL as suggested in header. and got a few mysql query errors.So, troubleshooted them. thanks@mac_gyver. still there are some bugs in the code even though yours is the best answer im going to keep the thread open till i troubleshoot them all@mac_gyver.

Link to comment
Share on other sites

you have far too much code, making it hard for you and us to see what the code is supposed to be doing.

 

just some of the problems are -

 

1) you have several places where you are using data that may not even exist, which will result in errors or incorrect output when it doesn't. if code requires an input value, all the code dependent on that input should only be executed if the input is present.

 

2) your login check isn't stopping program execution when the visitor isn't logged in. all the code is still running for a non-logged in visitor.

 

3) you are running queries inside of loops. this is a performance killer.

 

4) looping over query data where there will be at most one row. this just adds clutter to the code.

 

5) not using each set of the retrieved data inside of loops. this will typically result in only the last set of data being used after the end of the loop.

 

6) you have all kinds of variables that you are producing but don't use at all.

 

i recommend laying out the code on your page with the different concerns separated. see the following code layout that will help you do this - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095

 

the biggest help will come through separating the get method business logic from the get method presentation logic. this will group all the database dependent code together, allowing you or us to see just what the query/data portion of the code is doing so that it can be simplified.

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.