Click here to Skip to main content
15,849,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have php file with this code

PHP
$html .=  "\n\t".
        '<label id="myID" class="radio" style="min-height:42px;">'.
        '<input type="radio" style="float:none;" name="'.$var.'" id="'.$var.'" value="'.$item->value.'" '.$checked.'>'.
        $img.
        '<span style="margin-left: 10px;">'.JText::_($item->text).'</span>'.
        '</label>';
}
$html .= "\n";
return $html;


This code dynamically generates label tags. The label tags have id = myID, but I need to make different id for each label element. How can I reach that?

What I have tried:

I have tried to add increment but it doesn't work for me. Also I have tried to use the loop 'for'. And it works not correctly. I am new to php and I don't know how to do it. I will be appreciate for any help.


PHP
$idlist = array("ida", "idb", "idc");
     foreach($idlist as $id){
     $html .=  "\n\t".

          '<label id="' . $id . '" class="radio" style="min-height:42px;">'. "\n".

           '<input type="radio" style="float:none;" name="'.$var.'" id="'.$var.'" value="'.$item->value.'" '.$checked.'>'.
           $img.
           '<span style="margin-left: 10px;">'.JText::_($item->text).'</span>'.

             '</label>';

     }

   }

   $html .= "\n";
   return $html;
Posted
Updated 23-Aug-20 4:41am
v4

1 solution

A simple PHP loop that increments the value of $id:
PHP
$idlist = array("ida", "idb", "idc");
foreach($idlist as $id){
    echo '<label id="' . $id . '" class="radio" style="min-height:42px;">' . "\n";
}

Or a number counting alternative:
PHP
$num = 1;
while($num <= 3){
    echo '<label id="id' . $num . '" class="radio" style="min-height:42px;">' . "\n";
$num += 1;
}


See also PHP Tutorial[^]
 
Share this answer
 
v3
Comments
Member 14920672 23-Aug-20 5:29am    
Thank you for the solution. But when I use your options I have got the error "syntax error, unexpected 'echo' (T_ECHO)"

This is due to this line $html .= "\n\t".


Richard MacCutchan 23-Aug-20 5:34am    
The echo statement was just to illustrate the results in a test. You do not need it in your own code, just add the variable items in the places that I have shown.
Member 14920672 23-Aug-20 6:31am    
I removed the "echo" and now payment elements in a browser are not showing. I see these label tags with correct IDs in DEV dashboard. But in browser there is empty place. What is wrong?
Richard MacCutchan 23-Aug-20 7:57am    
I do not know what you are doing; if you are just copying my lines of code without adjusting them to fit your requirements then it probably will not work. Please use the Improve question link above, and add the exact code that you are using, so I can see it.
Member 14920672 23-Aug-20 8:13am    
I inserted the part of code, please check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900