首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当序列化字符串为空时,如何在php中隐藏在文本中?

当序列化字符串为空时,如何在php中隐藏在文本中?
EN

Stack Overflow用户
提问于 2018-08-17 02:08:02
回答 1查看 55关注 0票数 0

我在一个网站上工作,我想隐藏文本(Hello World,点A和点B)时,serialized字符串为空。下面是我的代码:

代码语言:javascript
复制
<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/>";
      }
   }
?>

问题陈述:

Hello World内部,我有两个部分-- Point APoint B

  • 如果Point APoint B都为空(表示serialized字符串为空),并且没有要显示的数据,则我根本不想显示Hello WorldPoint APoint B<p>文本。但如果其中任何一个存在(表示D20字符串不为空),则我希望显示D22和D23的文本(可以是D24或Point B).

这有可能做到吗?当序列化字符串为空时,它显示如下:

我不希望在Point APoint Bserialized字符串为空时打印这些文本。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-17 04:56:46

您应该将html输出放在几个变量中(包括'Hello World'),并在完成所有测试时在最后输出。

代码语言:javascript
复制
<?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
?>

这只是您所拥有的东西的快速修复。会有更优雅的方式来做到这一点。我会将pointA和pointB放在一个共享函数中,因为您通常会做两次相同的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51882998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档