0

Current I use toggle to show/hide details. I need to give each div a unique ID in foreach loop. I'm not sure how to make it works on an echo string. Can you help me?

<?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;
        }
    }
?>

I need to make $count in 2 position on the code is same. I got error with this code.

Thank you

Updated: Actually the code is not just as I give here. I tried with your code but doesnt work.

You can view full at 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;

第二:

不要只使用数字作为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 回答