【问题标题】:Most efficient way to loop through data with PHP使用 PHP 循环数据的最有效方法
【发布时间】:2012-10-03 14:46:02
【问题描述】:

我目前正在重构一个网站,我遇到了一组看起来都一样但有一些不同的变量/文本的链接。

我了解循环和所有内容,这绝对是需要的,但我不确定如何最好地处理发生变化的各种数据。

HTML如下:

<div class="featuredSide" style="border-color:#fff">
    <h3 style="font-size: 20px; margin-bottom: 12px;">$sectionName</h3>
    <img src="images/$imageName.jpg" width="50" height="60" style="float: left; margin: 4px 8px 16px 0px; border: 2px solid #fff;" />
    <h4 style="font-size: 11px;">$author</h4>
    <p class="lineAboveCol" style="clear: both; margin-bottom: 0px;"><a href="resources.php?section=$section">Click here for more information</a></p>
</div>

我已经放入变量来替换将要更改的数据(这些目前都是在 HTML 中完成的)。

那么循环遍历这些数据的最佳方法是什么,我曾想过使用简单的数组,但我认为从长远来看这并不容易维护。我是否缺少一个简单的解决方案,或者是否值得将这些数据设置在 MySQL 表中并直接从那里提取?

谢谢。

【问题讨论】:

  • 只需创建一个 array('sectionName' =&gt; 'author', 'anotherSectionName' =&gt; 'anotherAuthor'); 数组并执行 foreach ($array as $sectionName =&gt; $author) EDIT 哦,等等,我也注意到了其中的其他变量 - 但这个概念是合理的,只创建数组数组并访问子数组的各个键。
  • 我们能否提供更多信息。比如包含这个的页数。这个div等的出现次数
  • 这是一个包含文件,这段代码在该文件中重复了 8 次。该文件包含在少数几页中,可能是 9 页。

标签: php loops


【解决方案1】:

我会从一个数组开始:

$links = array(
    'section1' => array(
        'section'    => '...',
        'sectioName' => '...',
        'imageName'  => '...',
        'author'     => '...',
    //    'text' => 'Click here for more information',
    ),
);

然后我会运行一个循环。它很容易适应,你保留了不同的表示和数据,你可以在未来将它移动到 MySQL 或其他持久层,也许提供一个后端接口允许维护人员编辑部分。

如果部分之间需要一些变化,您可以将它们拉入数组(或 MySQL 中的列)的键中。

您还可以从外部提供 HTML 并运行特殊标记的 preg_replace,例如{{ author }}$currentSection['author'] 的内容替换,以此类推。这也很容易移植到任何模板引擎。

【讨论】:

  • 太好了,我认为这是最优雅的解决方案,也易于维护和阅读。谢谢。
【解决方案2】:

可以这样做..

<?php
$theVars[0]['sectionName'] = "section name 1";
$theVars[0]['section'] = "section 1";
$theVars[0]['author'] = "author 1";
$theVars[1]['sectionName'] = "section name 2";
$theVars[1]['section'] = "section 2";
$theVars[1]['author'] = "author 2";
$theVars[2]['sectionName'] = "section name 3";
$theVars[2]['section'] = "section 3";
$theVars[2]['author'] = "author 3";

$htmlStr = "";    

for($i=0;$i<=2;$i++){
    $htmlStr .= '<div class="featuredSide" style="border-color:#fff">
                 <h3 style="font-size: 20px; margin-bottom: 12px;">'.$theVars[$i]['sectionName'].'</h3>
                 <img src="images/$imageName.jpg" width="50" height="60" style="float: left; margin: 4px 8px 16px 0px; border: 2px solid #fff;" />
                 <h4 style="font-size: 11px;">'.$theVars[$i]['author'].'</h4>
                 <p class="lineAboveCol" style="clear: both; margin-bottom: 0px;"><a href="resources.php?section='.$theVars[$i]['section'].'">Click here for more information</a></p>
                 </div>';
}

echo $htmlStr;

?>

我在球场上吗?

【讨论】:

    【解决方案3】:

    我会为这个特定的东西使用 php 数组,但将数组放在不同的文件中,以便更新/上传

    我使用的代码类似于

    section.values.php

    $sections = array(
        'Section 1' => 'Author 1',
        'Section 2' => 'Author 2',
    );
    

    然后在html/php页面上:

    <?php
        include("section.values.php"); //or any file name
    
        foreach($sections as $sectionName => $author){
            echo '<div class="featuredSide" style="border-color:#fff">
                <h3 style="font-size: 20px; margin-bottom: 12px;">' . $sectionName . '</h3>
                <img src="images/$imageName.jpg" width="50" height="60" style="float: left; margin: 4px 8px 16px 0px; border: 2px solid #fff;" />
                <h4 style="font-size: 11px;">' . $author . '</h4>
                <p class="lineAboveCol" style="clear: both; margin-bottom: 0px;"><a href="resources.php?section=' . $section . '">Click here for more information</a></p>
                </div>';
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2016-08-03
      相关资源
      最近更新 更多