【问题标题】:Displaying PHP Functions With HTML/CSS使用 HTML/CSS 显示 PHP 函数
【发布时间】:2011-10-17 03:37:51
【问题描述】:

我有一些 php 和 css 代码,我试图将它们分解为函数。我需要一些关于如何解决这个问题的建议。

 <?php
 $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
 while ($info_row = mysqli_fetch_array($info))
 {
      $info_id = $info_row['info_id'];
      $info_title = $info_row['info_title'];
 ?>
 <div style="width: 100%;">
      <div style="float: left;">
      <?php echo $info_id; ?>
      </div>
      <div style="float: left;">
      <?php echo $info_title; ?>
      </div>
      <div style="clear: both;"></div>
 </div>
 <?php } ?>

如下所示:

1 个

2 两个

3 三

4 四个

以下是我到目前为止将所有内容分解为函数的内容:

 function get_info()
 {
      $dbc = get_dbc();
      $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
      while ($info_row = mysqli_fetch_array($info))
      {
           $result[] = $info_row;
      }
      mysqli_free_result($info);          
      return $result;
 }

 function display_info_id()
 {
      $result=forum();
      foreach ($result as $info_row) 
      {
           $info_id = $info_row['info_id'];
           echo $info_id;
      }
 }

 function display_info_title()
 {
      $result=forum();
      foreach ($result as $info_row) 
      {
           $info_title = $info_row['info_title'];
           echo $info_title;
      }
 }



 <?php get_info(); ?>
 <div style="width: 100%;">
      <div style="float: left;">
      <?php display_info_id(); ?>
      </div>
      <div style="float: left;">
      <?php display_info_title(); ?>
      </div>
      <div style="clear: both;"></div>
 </div>

这显示为: 第1234章 一二三四

我明白它为什么会这样显示,但我不明白如何让它以我想要的方式显示。任何建议都会有所帮助。

编辑--

这可能与我拥有的 3 个功能有关吗?我不希望 info_id 和 info_title 在同一个函数中。

当我查看 WordPress 等软件时,似乎我可能不得不将我的 HTML 包围在某种循环中,并改变函数获取信息的方式?

【问题讨论】:

标签: php html css function mysqli


【解决方案1】:

您需要将显示逻辑重构为一个循环,而不是两个单独的循环。

例如:

<?php get_info(); ?>
 <div style="width: 100%;">
      <div style="float: left;">
      <?php display_info_id(); ?>
      </div>
      <div style="float: left;">
<?php 
      $result=forum();
      foreach ($result as $info_row) 
      {
           $info_id = $info_row['info_id'];
           $info_title = $info_row['info_title'];
           echo $info_id . "&nbsp; . $info_title . "<br/>\n";
      }
?>
      </div>
      <div style="clear: both;"></div>
 </div>

【讨论】:

    【解决方案2】:

    您正在为每个变量调用一个单独的循环。没有必要将其拆分为多个函数,因为循环要求两个变量一起显示。

    我将如何处理它?我会将 SQL 结果存储到一个完整的数组中,然后遍历该数组以输出您的 HTML。如果您愿意,可以将其分为两个功能。如果您需要在页面上多次使用结果,可能会很有用。

    我正在使用HEREDOC string syntax 将PHP 变量(包装在{} 中)与HTML 块结合起来。这使您不必通过&lt;?php ?&gt; 跳入和跳出PHP

    // This function is fine.
    function get_info()
     {
          $dbc = get_dbc();
          $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
          while ($info_row = mysqli_fetch_array($info))
          {
               $result[] = $info_row;
          }
          mysqli_free_result($info);          
          return $result;
     }
    
    function display_result($result)
    {
      // Loop over your results and display each
      foreach ($result as $r)
      {
      // Now use the HEREDOC syntax to form your HTML block instead
      // of intermingling PHP function calls
    
        echo <<<HTML
       <div style="width: 100%;">
          <div style="float: left;">
            {$r['info_id']}
          </div>
          <div style="float: left;">
            {$r['info_title']}
          </div>
          <div style="clear: both;"></div>
       </div>
    HTML;
    // There must be no whitespace before or after the HTML; on the previous line!
      }
    }
    

    现在检索您的数据并显示它:

    <?php
    $results = get_info();
    display_result($results);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-10-07
      • 2011-04-25
      • 2012-07-24
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2013-12-01
      相关资源
      最近更新 更多