Jump to content

If statement not working after loop


narutofan

Recommended Posts

Hi,
i’m trying to get html output from a loop. The problem is i get results up untill the loop, but i’m not getting the results to be embedded in html after the if statement the $d and $tag1 are not printing when i tried print_r.
the code is to get tag value from url and then they are exploded and compared with the $tag1 variable it prints well untill the loop but when it reaches the if st i don’t know what goes wrong in my code it isn’t outputting anything after that

$tag=  htmlentities($_GET['tag']); 
$sql3="select * from updates";
$stmt_t=$conn->prepare($sql3);
$stmt_t->execute();
function hashtags($dat) {
$regex="/!+([a-zA-z0-9._-]+)/";
$dat1=  preg_replace($regex, '<a href="hash_sys.php?tag=$1">$0</a>', $dat);
return $dat1;
    
}
foreach ($tag_fetch as $row_s) {
           $data= htmlentities($row_s['update_body']);
          $data1=  explode(" ", $data);
         
foreach($data1 as $d){
   $tag1="!".$tag;
 
 
    //$dat1= preg_match("$tag1", "$d");
if($d==$tag1){
    
    $post_data1=implode(" ", $data1);
    print_r($post_data1);
    
                  $updateid=$row_s['update_id'];
           

           < html content that is embedded with loop results... >

}

 

Link to comment
Share on other sites

<?php
$tag1="";
include 'includes/header.php';
if (isset($_SESSION['uname'])){
$tag=  htmlentities($_GET['tag']); //$tag="!qwerty";
$data1=array();
$sql3="select * from updates";
$stmt_1=$conn->prepare($sql3);
$stmt_1->execute();
$tag_fetch=$stmt_1->fetchAll();
$sql1="select * from group_posts where type <> '1'";
$stmt_t=$conn->prepare($sql1);
$stmt_t->execute();
$data_fetch=$stmt_t->fetchAll();

function hashtags1($dat, $tag) {
$regex="/#+([a-zA-z0-9._-]+)/";
return $post_data1;
    
}
function hashtags($dat) {
$regex="/!+([a-zA-z0-9._-]+)/";
$dat1=  preg_replace($regex, '<ahref="hash_sys.php?tag=$1">$0</a>', $dat);
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) {
           $data= stripslashes($row_s['update_body']);
    
        $data1=  explode(" ",$data);
$tag1="!$tag"; 
$tag2=' <ahref="hash_sys.php?tag='.$tag.'">'.$tag1.'</a>';
$tag3= preg_replace('#<a.*?>(.*?)</a>#i', '\1', $data1);
//print_r($data1);
foreach($data1 as $d){
 // echo '<pre>'; print_r($d); echo '</pre>';
  $tag1="!$tag"; 
$tag2=" #<a.*?>(.*?)</a>#i', '\1'".$tag1."</a>";
$tag3= preg_replace('#<a.*?>(.*?)</a>#i', '\1', $d);
//print_r($tag2);
$dat1= preg_match("~<a(?=[^>]* name=[\"']([^'\"]*)|)(\s+[^>]*)?>(.*?)</a>~", "$d");

if($tag1==$d || $tag2==$dat1 || $tag3==$d){
    $post_data1= implode(" ",(array)$data1);

                  $updateid=$row_s['update_id'];
            
                $account_name=$row_s['account_name'];
                $u_id=$row_s['user_id_u'];
                $author=$row_s['author'];
                $post_date=$row_s['time'];
                $title= stripslashes($row_s['title']);
                $data= stripslashes($row_s['update_body']);
             $data1=  hashtags($post_data1);
           //   $data1=  taggingsys($data0);
              $pic=$project->viewByUname($author);
              $uid=$pic['user_id'];
         $datemade = strftime("%B %d, %y", strtotime($post_date));
           $avatar=$pic['avatar'];
          if ($avatar!=""){
          $feed_pic='../user/user/'.$uid.'/'.$avatar;
      }  else {
          $feed_pic='img/avatardefault.png';
          }
            
                         
          $vote_up_count=$project->voteUpdateCheck($updateid, $_SESSION['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.php';
        
          if ( $vote_like==TRUE && $vote_dislike==FALSE) {
           
          
             $like='<a  type="like" class="btn liked status_like_'.$updateid.' like" attr="'.$updateid.'">Liked</a>('.$num_likes[0].')';
             $unlike='<a  type="unlike" class="btn status_unlike_'.$updateid.' unlike" attr="'.$updateid.'">Dislike</a>('.$num_unlikes[0].')';   
          }
 elseif ($vote_dislike==TRUE && $vote_like==FALSE) {
              
             $like='<a  type="like" class="btn status_like_'.$updateid.' like" attr="'.$updateid.'">Like</a>('.$num_likes[0].')';
             $unlike='<a  type="unlike" class="btn disliked status_unlike_'.$updateid.'" attr="'.$updateid.'">Disliked</a>('.$num_unlikes[0].')';
          }  else {
             $like='<a  type="like" class="btn status_like_'.$updateid.' like" attr="'.$updateid.'">Like</a>('.$num_likes[0].')';
             $unlike='<a  type="unlike" class="btn status_unlike_'.$updateid.' unlike" attr="'.$updateid.'">Dislike</a>('.$num_unlikes[0].')';
             
          }
          $share_button="";
                 $hidden_text= '<textarea class="hidden_textarea hidden_value'.$updateid.'" type="hidden" disabled>'.$data1.'</textarea>';
                   $share_button="<a class='btn share_btn share_".$updateid."' title='".$title."' type='share' id='".$updateid."'>Share</a>";
                   
             
                if ($author==$_SESSION['uname'] || $account_name==$_SESSION['uname']) {
                $statusdeletebutton='<li>'
                           . '<a  type="'.$updateid.'" class="btn delete_4_session hidden_text_delete_'.$updateid.' glyphicon glyphicon-trash delete_status_btn" title="Delete this status and its replies">Remove</a></li>';
                $edit_btn='<li>'
                        . '<a attr="'.$updateid.'" type="'.$updateid.'" class="btn edit_4_session hidden_text_edit glyphicon glyphicon-pencil" title="Edit this status" >Edit</a></li>';
                $statusui_edit="<div type='".$updateid."' class='hidden_edit_4_session session_editor".$updateid." jumbotron'>"
            . "<a type='".$updateid."' class='btn pull-right close_edit' title='Close without editing'>Close X</a>"
            . "<input type='text' class='form-control title_s_edit title_s_".$updateid."' name='status_title' value='".html_entity_decode($title)."' placeholder='Title' >"
            . "<span> </span>"
            . "<textarea id='wall_edit_".$updateid."' type='".$updateid."' rows='5' cols='50'  class='session_edit text_value_".$updateid."' wrap='hard' placeholder='whats up ".$session_uname."'>
             ".html_entity_decode($data1)."</textarea><br>"
            . "<button style='float:right;' value='".$updateid."' type='a' class='btn btn-warning btn btn-large btn-lg post-s-edit'>Update</button></div>" ;
 
                }else{
                    $statusdeletebutton="";
                    $edit_btn="<li class='posted'>You are not the owner of this Post</li>";
                    $statusui_edit="";
                }
           
       echo $statusui_edit.''. $hidden_text.'<div attr="'.$updateid.'" type="'.$updateid.'" class="statusboxes status_'.$updateid.'  jumbotron">'
                        . '<h3 class="pull-left title">'
                        . '<div id="'.$updateid.'" class="title_s_2copy posted" 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">'
                         .$edit_btn .' '. $statusdeletebutton .'</ul></div></span><br><hr>'
                        . '<legend><span style="font-size: 13.5px;" class=" data_s_2copy" type="'.$updateid.'" >'
                        . html_entity_decode($data1).'</span></legend><b class="posted"><small>Posted by:-  <a href="home.php?u='.$author.'"><img src="'.$feed_pic.'" height="20px" width="20px"> '.$author.   '</a>   '.$datemade.'</small></b>'
                        . '<br><legend>'.$like.' | '.$unlike. ' | '.$share_button.'<h4><a id="'.$updateid.'" class="btn collap-btn">Comments</a></h4></legend>';
                       
       ?>

    <!-- Collapsible Element HTML -->
  
   <?php
// Fetch Comments of update
  echo '<div id="toggle'.$updateid.'" class="collapse">'; 
   $totalComments=$project->totalComments($_SESSION['uname'],$friend,$updateid);   
        foreach($totalComments as $row1){
             $status_reply_id=$row1['comment_id'];
                $reply_d=htmlentities($row1['comment_body']);
                $reply_data=  stripslashes($reply_d);
                $reply_osid=$row1['os_id'];
                $reply_date=$row1['time_c'];
                $reply_author=$row1['author_c'];
               $reply_data1=  hashtags($reply_data);
               // $reply_data1=  taggingsys($reply_data0);
                $datemade_r = strftime("%B %d, %y", strtotime($reply_date));
                 include 'ratings/vote_count4comments.php';
        
          $vote_up_count_r=$project->voteReplyCheck($_SESSION['id'], $status_reply_id);
          
        if($vote_up_count_r[0][0]>0){
            $vote_like_r=TRUE;
            
        } else {
        $vote_like_r=FALSE;    
        }
          if($vote_up_count_r[0][1]>0){
            $vote_dislike_r=TRUE;
            
        } else {
        $vote_dislike_r=FALSE;    
        }
      
        
          if ( $vote_like_r==TRUE && $vote_dislike_r==FALSE) {
           
          
               $like_r='<a  type="like" class="btn status_liker_'.$status_reply_id.' liked_r" attr="'.$status_reply_id.'">Liked</a>('.$num_likes_r[0].')';
             $unlike_r='<a  type="unlike" class="btn status_unliker_'.$status_reply_id.' unlike_r" attr="'.$status_reply_id.'">Dislike</a>('.$num_unlikes_r[0].')';
          }
 elseif ($vote_dislike_r==TRUE && $vote_like_r==FALSE) {
              
         $like_r='<a  type="like" class="btn status_liker_'.$status_reply_id.' like_r" attr="'.$status_reply_id.'">Like</a>('.$num_likes_r[0].')';
             $unlike_r='<a  type="unlike" class="btn status_unliker_'.$status_reply_id.' disliked_r" attr="'.$status_reply_id.'">Disliked</a>('.$num_unlikes_r[0].')';
          }  else {
             $like_r='<a  type="like" class="btn status_liker_'.$status_reply_id.' like_r" attr="'.$status_reply_id.'">Like</a>('.$num_likes_r[0].')';
             $unlike_r='<a  type="unlike" class="btn status_unliker_'.$status_reply_id.' unlike_r" attr="'.$status_reply_id.'">Dislike</a>('.$num_unlikes_r[0].')';
                     
          }  
          $pic=$project->viewByUname($reply_author);
              $uid=$pic['user_id'];
         
           $avatar=$pic['avatar'];
          if ($avatar!=""){
          $feed_r_pic='../user/user/'.$uid.'/'.$avatar;
      }  else {
          $feed_r_pic='img/avatardefault.png';
          }
               
                 
        if ($reply_author==$_SESSION['uname'] ) {
                   $reply_delete_button='<li><a type="'.$status_reply_id.'" class="btn delete_reply_btn glyphicon glyphicon-trash delete_reply_"'.$status_reply_id.'" title="Delete this comment"> Remove</a></span></li>';
               } else{
                   $reply_delete_button="";
               }
              echo '
                <div  class="replyboxes pull-left reply_' .$status_reply_id.'">'
                      . 'Reply by:- '
                      . '<a href="home.php?u='.$reply_author. '"><img src="'.$feed_r_pic.'" height="20px" width="20px">'.$reply_author.'</a>'
                      . '<span class="pull-right">'.$datemade_r 
                       . '<b class="dropdown">
                         <small><span class="btn btn-xs btn-danger dropdown-toggle pull-right" data-toggle="dropdown"  >
                         <span class="glyphicon glyphicon-edit"></span></span>
                        <ul class="dropdown-menu">'.$reply_delete_button
                      . '</ul></span>'
                      . '</small></b><br><legend>'.  html_entity_decode($reply_data1).'<br>'.$like_r.'|'.$unlike_r.'</legend></div>';
               
              
               } 
        echo '<textarea id="reply_textarea_'.$updateid.'"  class="status_reply_'.$updateid.' input-custom2" placeholder="comment\'s"></textarea>'
                            . '<button id="reply_butn_'.$updateid.'" attr="'.$updateid.'" type="b" class="btn btn-warning pull-right btn-sm reply_butn reply_'.$updateid.'">Reply</button></div></div>';
                                
}

               }


               }
 

echo "</div></td><td class='div-col-right1 '><div class='overflow'><h3>Groups</h3>";


        foreach ($data_fetch as $row_d) {
        $dat=  html_entity_decode($row_d['data']);
            $dat1=  explode(" ", $dat);
$tag1="!$tag";

foreach($dat1 as $d){
    
//$dat1= preg_match("$tag1", "$d");
if($tag1==$d){
    $post_data1=implode(" ", $dat1);
  


                   
            $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= hashtags($post_data);
       // $data11= hashtags1($post_data,$tag);
        $post_date=$row_d['pdate'];
        $post_avatar=$row_d['avatar'];
        $post_uid=$row_d['user_id_g'];
           if ($post_avatar != ""){
          $g_pic='../user/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']);  
      
        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 "<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'>"
                         ."</ul></div></span><br></legend>"
                . "<legend>{$data1}<br><span class='spinner_g_u{$postid}'></span>{$like} | {$unlike} <br>Posted by:<a target='_blank' href='home.php?u={$post_auth}&v={$_SESSION['uname']}'>{$user_image} {$post_auth}</a>-- {$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=  $data11;
                        $reply_date=$row1['pdate'];
                        $reply_avatar=$row1['avatar'];
                        $reply_uid=$row1['user_id'];
                       
                        if ($reply_avatar != ""){
          $r_pic='../user/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>

here is the full code and a sampple mysql fiddle:http://sqlfiddle.com/#!9/32149a/1

Link to comment
Share on other sites

Now you are being ridiculous in showing us tons and tons of code.  Be sensible - would you want to look at this much code from a stranger?

Also - it is great that you are thinking of using prepared queries but your use of them here is unnecessary.  You have no arguments or values in your queries so what do you need to have "prepared"?  RTFM if you don't understand why I am pointing this out.

Another point that shows the difference between good programming style and yours.  Don't bury functions inside your mainstream code!!!  Way Too Confusing.  Put your functions at the end of your mainline code so as not to mess up your thought process as you read thru the script.  Same with your html.  Put that at the end after you have done all of your php work and place any variables that you have built with your code in the appropriate place within the html block of code.  Again - much easier reading!

Pseudo-script:

- start your session and set any needed values

- check the current status by seeing if you have any inputs available - if not then issue your html form/starting page

- retrieve the input values and sanitize them and ensure that they are what you expect.  If not send the starting page back with the values embedded in the form

-- handle the valid inputs and do all of your necessary php work.  Build html blocks like html tables and such with the output saved in php vars.

- put your php functions here

- output your html page with all of the html headers (doctype/meta/head/script/style/body/etc.) and JS code and the php vars embedded inside it and exit

Now you have a readable script with good organization that becomes so much easier to read and follow along with especially 6 months from now when you come back for some improvements!

 

PS - I use a php function to output my entire block of html code.  Easy to do your output from any point in your script when needed.  Use the heredocs operator to make outputting SO MUCH EASIER!

Just my $.02.

 

Link to comment
Share on other sites

finally i found a solution all i had to do was add spaces to the anchor tag in hashtag function and the explode function started to separate the tag from the anchor tag and now the if st is working after the loop.

function hashtags($dat) {
$regex="/!+([a-zA-z0-9._-]+)/";
$dat1=  preg_replace($regex, '<a href="hash_sys.php?tag=$1"> $0 </a>', $dat);<=see the space
return $dat1;
    
}....

 

foreach ($tag_fetch as $row_s) {
           $data= stripslashes($row_s['update_body']);
    
        $data1=  explode(" ",$data); <= this took care of the rest

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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