0

I am working on a website in which I want to hide text (Hello World, Point A, and Point B) when serialized string is empty. Below is my code:

<p class="font-weight-bold">Hello World</p>
<p class="font-weight-bold">Point A</p>

<?php

   $serialized = '';
   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0)
       {
          $serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($serialized != '')
   {
      $unserialized = unserialize( $serialized );    
      foreach($unserialized  as $key=>$value)
      {
         echo $key." ".$value['start']." ".$value['end']."<br/>";
      }
   }
?>

<p class="mt-2 font-weight-bold text-center">Point B</p>
<?php

   $serialized = '';

   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0)
       {
          $serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($serialized != '')
   {
      $unserialized = unserialize( $serialized );  
      foreach($unserialized  as $key=>$value)
      {
         echo $key." ".$value['start']." ".$value['end']."<br/>";
      }
   }
?>

Problem Statement:

Inside Hello World, I have two sections - Point A and Point B.

  • If both Point A and Point B are empty (meaning serialized string is empty) and there is no data to show then I don't want to show <p> text for Hello World, Point A and Point B at all.

  • But if either of them is present (meaning serialized string is not empty) then I want to show <p> text for Hello World and <p> text for whatever it is present (it can be Point A or Point B).

Is this possible to do? When the serialize string is empty it is displaying like this:

enter image description here

I don't want those texts to print when the serialized string is empty for Point A or Point B.

Jeff
  • 6,895
  • 1
  • 15
  • 33
flash
  • 1,455
  • 11
  • 61
  • 132

1 Answers1

1

You should put the html output in several vars (including the 'Hello World') and do the output at the end when you did all the testing.

<?php
// pre-set/initialize some html templates/strings to be used later
$html = '';
$helloWorld_header = '<p class="font-weight-bold">Hello World</p>';
$pointA_header = '<p class="font-weight-bold">Point A</p>';
$pointA_content = '';
$pointB_header = '<p class="mt-2 font-weight-bold text-center">Point B</p>';
$pointB_content = '';
$pointA_found = $pointB_found = false;  // initialize both to false



   $pointA_serialized = '';
   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0) {
          $pointA_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($pointA_serialized != '') {
      $pointA_found = true;
      $unserialized = unserialize( $pointA_serialized );
      foreach($unserialized  as $key=>$value) {
         $pointA_content .= $key." ".$value['start']." ".$value['end']."<br/>";
      }
      // add everything from pointA to $html
      $html .= $pointA_header . $pointA_content;
   }

   $pointB_serialized = '';

   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0) {
          $pointB_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($pointB_serialized != '') {
      $pointB_found = true;
      $unserialized = unserialize( $pointB_serialized );  
      foreach($unserialized  as $key=>$value) {
         $pointB_content . $key." ".$value['start']." ".$value['end']."<br/>";
      }
      // add everything from pointB to $html
      $html .= $pointB_header . $pointB_content;
   }

// if pointA or pointB is set/found...
if($pointA_found || $pointB_found) {
   // ... add the main header before the rest
   $html = $helloWorld_header + $html;
}

// do the actual output
echo $html
?>

This is just a quick fix of what you've got. There would be more elegant ways to do that. I'd put pointA and pointB in a shared function, as you mostly do twice the same thing.

Jeff
  • 6,895
  • 1
  • 15
  • 33