CodeIgniter Forums
ID in an html tag inside a php script - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: ID in an html tag inside a php script (/showthread.php?tid=48499)



ID in an html tag inside a php script - El Forum - 01-18-2012

[eluser]agdelacruz02[/eluser]
Hi, I'm getting problems when putting ID in my html tag inside an php script. What I want is to use a variable as an ID in my tag. Here's an example
Code:
<?php
  foreach ($myquery_domain->result() as $row ){
    $domain = $row->domain;
    $domain_id = $row->domain_id;
    echo '<tr>';
      echo '<td>';
        echo '<span class="flip_domain" id="$domain_id">+</span>'.$domain;
      echo '</td>';
    echo '</tr>';
?&gt;

I want my $domain_id to be the ID of my <span>. Could Someone help me pls. Thank you.


ID in an html tag inside a php script - El Forum - 01-18-2012

[eluser]isawhat[/eluser]
You need to have '. .' around $domain_id

Code:
&lt;?php
foreach ($myquery_domain->result() as $row ){
  $domain = $row->domain;
  $domain_id = $row->domain_id;
  echo '<tr>
   <td>
     <span class="flip_domain" id="'. $domain_id .'">+</span>'. $domain .'
   </td>
  </tr>';
}
?&gt;



ID in an html tag inside a php script - El Forum - 01-19-2012

[eluser]porquero[/eluser]
php only get variable values from double quote strings.
So you can use heredoc or nowdoc(php5.3):

Heredoc:
Code:
foreach ($myquery_domain->result() as $row ){
    $domain = $row->domain;
    $domain_id = $row->domain_id;
    echo <<<EOT
<tr>
<td>
  <span class="flip_domain" id="{$domain_id}">+</span>{$domain}
</td>
</tr>
EOT;
}

heredoc is more strict but you can get more maintanable code.


ID in an html tag inside a php script - El Forum - 01-19-2012

[eluser]CroNiX[/eluser]
According to the HTML standards, ID's are supposed to be unique, meaning no two elements share the same id. So you should be adding something to that id in that loop to make them unique.