0

現在、トグルを使用して詳細を表示/非表示にしています。foreach ループで各 div に一意の ID を与える必要があります。エコー文字列で機能させる方法がわかりません。手伝って頂けますか?

<?php
$i = 0; 
    foreach ( $payment as $payment_id => $items ) {
        foreach ( $items as $item ) {
            $i++;
            $count = 0;
            // Echo, need to show unique ID in both $count, it must be the same
            echo '<p><strong><a href="#">Link</a></strong>
            <br/><a href="#" class="show_info" id="'.$count.'">Show Details</a>
            <div id="payment_info_'.$count.'" style="display:none;">';
            ++count;
        }
    }
?>

コードの 2 つの位置に $count を作成する必要がありますが、同じです。このコードでエラーが発生しました。

ありがとうございました

更新: 実際には、コードはここで示したものとは異なります。私はあなたのコードを試しましたが、うまくいきません。

http://www.codesend.com/view/7d58acb2b1c51149440984ec6568183d/ (pasw:123123)で完全に表示できます。

4

3 に答える 3

1
  1. あなたは++count代わりに書いた++$count
  2. を使用していないようです$i
  3. $countすべてのループで初期化しています。

これはうまくいくはずです:

<?php
$i = 0; /* Seems redundant */
$count = 0;
    foreach ( $payment as $payment_id => $items ) {
        foreach ( $items as $item ) {
            $i++; /* Seems redundant */
            // Echo, need to show unique ID in both $count, it must be the same
            echo '<p><strong><a href="#">Link</a></strong>
            <br/><a href="#" class="show_info" 
                    id="dtls'.$count.'">Show Details</a>
            <div id="payment_info_'.$count.'" style="display:none;">';
            ++$count;
        }
    }
?>
于 2013-08-17T02:42:24.167 に答える
0

最初:

これを削除するか、 foreach ループの外に置きます。

$count = 0;

2番目:

idに数値だけを使用せず、次のような文字または単語と共に使用してください。

id = "element'.$count.'"

三番 :

は何$iですか?

ダメなら削除!

前方へ

++countに変更++$count

コード :

<?php
$i = 0; 
$count = 0;
    foreach ( $payment as $payment_id => $items ) {
        foreach ( $items as $item ) {
            $i++;
            // Echo, need to show unique ID in both $count, it must be the same
            echo '<p><strong><a href="#">Link</a></strong>
            <br/><a href="#" class="show_info" id="info_'.$count.'">Show Details</a>
            <div id="payment_info_'.$count.'" style="display:none;">';
            ++$count;
        }
    }
?>
于 2013-08-17T02:41:27.837 に答える
0

++countが間違っています。++$count のはずです。

これを試して

<?php
$i = 0; 
    foreach ( $payment as $payment_id => $items ) {
        foreach ( $items as $item ) {
            $i++;
            $count = 0;
            // Echo, need to show unique ID in both $count, it must be the same
            echo '<p><strong><a href="#">Link</a></strong>
            <br/><a href="#" class="show_info" id="'.$count.'">Show Details</a>
            <div id="payment_info_'.$count.'" style="display:none;">';
            ++$count;
        }
    }
?>
于 2013-08-17T02:44:51.807 に答える