If statement not working after loop

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

}

if your expected values are not visible through print_r() you should first try var_dump() as it shows you null values and otherwise invisible characters. But you have a leading exclamation mark that should be visible anyway, so it looks like the loop is not running for, maybe, $data1 is empty.

the data1 is printing values above the if statement and not inside it.plus tried your advice of using var_dump inside if statement and it isn’t producing any results what to do??? @chorn

Where do $tag_fetch and $row_s come from?

it is coming from a query that fetches results for $data1 above the loop

And you have the expected data within those variables, before the loop?

@dropsnoot yes

no it’s not, $tag_fetch is undefined.

I’m presuming the OP means that both the variables are defined elsewhere and not shown in the post. Neither of the foreach() loops are closed in the sample code we have, so it just wouldn’t run unless there’s other code around that hasn’t been shown. It is a bit more difficult to figure out what might be going wrong when people remove code, though I understand the need to remove any confidential stuff. Conversely we have the function definition for hashtags() here, but it’s not used at all as far as I can see.

Personally I’d also steer clear from similarly-named variables, here we use $data and $data1 quite close to each other where the latter, IMO of course, would be better called $data_array as it’s the same content as $data, exploded into an array. Doubt that is causing the problem, though.

Also, this is a strange line to have inside a second-level foreach() loop:

$tag1 = "!".$tag;

as $tag is created from a variable passed in to the script and doesn’t appear to change, what’s the point of re-defining it every time the inner loop runs?

I wonder if the OP has missed out some code from the post and hasn’t noticed, which is making it difficult to see the problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.