469,275 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Using GetElementById in PHP loop

27
hi,
I will like to use javascript GetElementById in a PHP for loop whereby the ids are generated as the loop executes and not fixed ids as in the example below. Therefore function displayRow() once executed will collapse all cells with ids variably assigned on looping. thus i will have $i or a variable id instead of captionRow, captionRow2 etc.

Expand|Select|Wrap|Line Numbers
  1. function displayRow(){
  2.  
  3.       var col = document.getElementById("captionRow");
  4.  
  5.       if (col.style.display == '') col.style.display = 'none';
  6.  
  7.       else col.style.display = '';
  8.  
  9.       var col2 = document.getElementById("captionRow2");
  10.  
  11.       if (col2.style.display == '') col2.style.display = 'none';
  12.  
  13.       else col2.style.display = '';
  14.  
  15.       var col3 = document.getElementById("captionRow3");
  16.  
  17.       if (col3.style.display == '') col3.style.display = 'none';
  18.  
  19.       else col3.style.display = '';
  20.  
  21.  
  22.       }
[HTML]
<table width="300" border="1">


<th class = 'hr' id="captionRow">TH-1</th><th>TH-2</th><th>TH-3</th></tr>

<tr><td class = 'hr' id="captionRow2">cell-11</td><td>cell-12</td><td>cell-13</td></tr>

<tr><td class = 'hr' id="captionRow3">cell-21</td><td>cell-22</td><td>cell-23</td></tr>

</table>

<p><button onclick="displayRow()" >Show / Hide</button></p>

</body>

</html>
[/HTML]

thanks in advance
Aug 25 '08 #1
4 8186
RamananKalirajan
607 512MB
You are creating the Table rows dynamically and once the dynamicRow() function has been called all the row element has to be made invisible. The Id's are generated at run time and u want that to be controlled through loop. are u creating the dynamic elements by DOM. wether u have tried any code for that. If so please post it

Regards
Ramanan Kalirajan
Aug 25 '08 #2
acoder
16,027 Expert Mod 8TB
You don't even need PHP. This could be done entirely in JavaScript. Get the number of rows in the table and use a for loop in JavaScript, then something like:
Expand|Select|Wrap|Line Numbers
  1. var col = document.getElementById("captionRow"+i);
Aug 25 '08 #3
prosad
27
hi,
what am trying to achieve is hide all cells/<td> with view, edit or delete actions (that will be 3 columns) so that on preview of this table these cells will not be visisble.Am trying to give these cells (view, edit, delete) ids so that on click of a button i can hide or display them. Here's the code, thanks:

[PHP]for ($i = $startrec; $i < $reccount; $i++)
{
$row = mysql_fetch_assoc($res);
$style = "dr";
if ($i % 2 != 0) {
$style = "sr";
}

?>[/PHP]

[HTML]<tr>
<td class="<?php echo $style ?>"><a href="analysis.php?a=view&recid=<?php echo $i ?>">View</a></td>
<td class="<?php echo $style ?>"><a href="analysis.php?a=edit&recid=<?php echo $i ?>">Edit</a></td>
<td class="<?php echo $style ?>"><a href="analysis.php?a=del&recid=<?php echo $i ?>">Delete</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["lp_client"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["lp_assign"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["year_end"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["prepared"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["received"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["schedule"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["date_from"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["date_to"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["date"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["part"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["amount"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["desc"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["remark"]) ?></td>
</tr>[/HTML]

[PHP]
<?php
}
mysql_free_result($res);[/PHP]
Aug 26 '08 #4
acoder
16,027 Expert Mod 8TB
PHP won't run once the page has loaded. If you need to hide columns on clicking a button, use JavaScript only. What you can do is use PHP to generate the IDs and JavaScript code.
Aug 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Dark Magician | last post: by
2 posts views Thread by OBAFGKM_RNS | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.