473,224 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 8469
RamananKalirajan
608 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

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

Similar topics

15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
1
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
6
by: Jake Barnes | last post by:
This function has always worked for me just fine: function nl2br_js(myString){ // 02-18-06 - this function imitates the PHP command nl2br, which finds newlines in a string // and replaces them...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
11
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application...
2
by: OBAFGKM_RNS | last post by:
Let's say I have a Javascript function that loops over and over. In that function i have it alternating images on a button this way: if(var==0){ var myHTML = "<input type='button'...
1
by: anniefs | last post by:
hi help me i m so much stuck int he code and i have no time .... i used ASP VBscipt and javascript functions with MS database javascript function add records in MS DB by using ASP vbscript...
1
by: J Smale | last post by:
Greetings folks, I have been trying for sometime now to figure out what I am sure is this very simply problem. I have been unscuessful in finding the answer though so if anyone can help I would...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.