Welcome Guest, Not a member yet? Register   Sign In
ID in an html tag inside a php script
#1

[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.
#2

[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;
#3

[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.
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB