Kien
Kien

Reputation: 39

generate unique ID for on foreach loop?

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)

Upvotes: 0

Views: 7588

Answers (3)

Itay
Itay

Reputation: 16777

  1. You've writen ++count instead of ++$count
  2. It seems you're not using $i.
  3. You're initializing $count on every loop.

This is supposed to work:

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

Upvotes: 1

Mohammad Masoudian
Mohammad Masoudian

Reputation: 3491

first:

remove this or put it outside of the foreach loop :

$count = 0;

second:

don't use only number for id and use it with a character or word like this :

id = "element'.$count.'"

Third :

what is $i ?

if it's useless remove it!

Forth

change ++count to ++$count

CODE :

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

Upvotes: 0

berfuser
berfuser

Reputation: 46

++count is wrong, it should be ++$count;

try this

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

Upvotes: 0

Related Questions