0

番号付きのクラスが必要なforeachループがあるので、カウンターを設定します。

 <?php
 $counter = 0;
  ?>
  <?php
$html = '';
foreach ($json['feed']['entries'] as $entries){ 
$counter++;
$html .= '<a href="' .$entries['link']. '"><span class="feeddisplay_title">'.$entries['title']. ' </span></a>';
    if($entries['publishedDate'] != '') {
        $html .='<span class="feeddisplay_date">' .$entries['publishedDate'].    '</span>';
    }
    $html .= '<p>' .$entries['contentSnippet']. '</p>';
}
?>
<div class="box feed-<?php echo $counter; ?>">
 <div class="box-heading"><?php echo $heading_title; ?>:</div>
 <div class="box-content">
<div class="feeddisplay"><?php echo $html ?></div>
 </div>
 </div>

そして、私の出力クラスは何度も「feed-1」です。上がりません。いくつかのバリエーションを試しましたが、行き詰まりました。

4

3 に答える 3

0

これは、foreachループの外側でカウンター変数をエコーし​​ているためです。

$ counterが呼び出されるまでに、counterの値はforeachループを介して加算されているため、最終的なカウントを表示しているだけです。

于 2013-01-16T04:05:35.340 に答える
0

出力を次のようにしたくないのは確かですか。

<?php
$counter = 0;

$html = '';
foreach ($json['feed']['entries'] as $entries){
    $counter++;
    $html = '<div class="box feed-'.$counter.'">
    <div class="box-heading">'.$heading_title.':</div>
    <div class="box-content">
        <div class="feeddisplay">
            <a href="' .$entries['link']. '">
                <span class="feeddisplay_title">'.$entries['title']. ' </span>
            </a>';
    if($entries['publishedDate'] != '') {
        $html .='<span class="feeddisplay_date">'.$entries['publishedDate'].'</span>';
    }
    $html .= '<p>'.$entries['contentSnippet'].'</p>';
    $html .= '        </div>
    </div>
</div>';
}
?>
<?php echo $html; ?>
于 2013-01-16T15:26:25.567 に答える
0

解決策を見つけました:

これはopencartであるため、これは.tplファイルであり、カウンターが機能しない理由は、.phpコントローラーファイルで同じ機能を実行しようとしている別の変数があるためです。「counter」ではなく「module」をエコーし​​ただけで動作しました。

   <div class="box feed-<?php echo $module; ?>">
于 2013-01-16T19:33:10.660 に答える