Current location: Hot Scripts Forums » General Web Coding » JavaScript » Incrementing a PHP loop with JS


Incrementing a PHP loop with JS

Reply
  #1 (permalink)  
Old 04-11-10, 09:39 AM
MesaFloyd MesaFloyd is offline
Newbie Coder
 
Join Date: Mar 2008
Location: Calif
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Incrementing a PHP loop with JS

I am an <older> rookie, please be gentle with me.
I make a table called from a DB using PHP
The table loops the DB untill all the occupied fields are displayed - works fine -
I am trying to insert a button for each row (JS), that when pressed will highlight and copy to clipboard one particular cell of that row
The button is not working properly. When I press any of the buttons on the table, only the first cell is higlighted and copied.
I know it is because all the buttons are calling the same cell, but I don't know how to make the button increment for each row.
Any of you programming gru's able to help me?. - yes, I know this will only work in IE -
I think the solution is in JS, but I am not sure if it can be fixed in PHP.
Please keep it simple for me.
Thanks in advance -
Floyd
Here's my code (i know is it not perfect, but neigher am I)
Code:
<script language='Javascript'>
	function doact(d)
	{
	var doc = eval("document.readme."+d);
	cp = doc.createTextRange();
	doc.focus();
	doc.select();
	cp.execCommand("Copy");
	}
</script>

<form name="readme">
<?php
	include("database.php");
	$query = "SELECT * FROM users"; 
	$result = mysql_query($query) or die(mysql_error());

		echo "<table border='1'>";
		echo "<tr> <th>Username</th> <th>IP Address</th><th>Email Address</th> <th>Last Visit Date-Time </th></tr>";

	// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array( $result ))
	{
		// Print out the contents of each row into a table
		echo "<tr><td>";
		echo $row['username'];
		echo "</td><td>";
?>
	<TEXTAREA name="text1" style="width: 150px; height: 20px;" >
<?php 
		echo $row['pcidip']; 	
?>
	</TEXTAREA>
		<input onclick="doact('text1')" type="button" value="Copy IP">
</form>
<?php		
		echo "</td><td>";
		echo $row['email'];
		echo "</td><td>";
		echo $row['datetime'];
		echo "</td></tr>"; 
	}
		echo "</table>";
?>
Reply With Quote
  #2 (permalink)  
Old 04-11-10, 10:35 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Add a counter (I used $i). Initialize it to zero before the while loop.

Change the button code like so:

<input onclick="doact('text<?php echo $i++; ?>')" type="button" value="Copy IP">

What it should do is increment the counter (so the first button refers to text1), then add the 1 to 'text' - so you get doact('text1') for the first row, doact('text2') for the second row, etc.

I'm not perfect either - this code and idea hasn't been tested, but I hope it helps you.
Reply With Quote
  #3 (permalink)  
Old 04-11-10, 10:57 AM
MesaFloyd MesaFloyd is offline
Newbie Coder
 
Join Date: Mar 2008
Location: Calif
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks wirehopper
I have two references to text1
I replaced my references from
text1
to
text<?php echo $i++; ?>

At the bottom on the web page, when a button is pushed I see
!Error on page.

you can see the results here
viewdatabase-testbutton-help.php

before the change to the above there was no error like stated earlier, any button would only highlight and copy the first IP address...

You mentioned " Initialize it to zero before the while loop"
am I suppose to add code to initialize it to zero, or does it do that by itself?

Floyd
Reply With Quote
  #4 (permalink)  
Old 04-11-10, 03:10 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
To initialize it to zero:

$i=0;
while (remainder of your code here)
Reply With Quote
  #5 (permalink)  
Old 04-11-10, 11:11 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your incrementing $i for each reference (once for the textarea and once for the button) and they are not lining up, so none of your buttons work.

Try it like this:
PHP Code:

<html>
<head>
<script>
    function doact(d)
    {
    var doc = eval("document.readme."+d);
    cp = doc.createTextRange();
    doc.focus();
    doc.select();
    cp.execCommand("Copy");
    }
</script>
</head>
<body>
<?php
   $i
=0;
    include 
"database.php";
    
$query "SELECT * FROM users";
    
$result mysql_query($query) or die(mysql_error());
   echo 
"<form name='readme'><table border='1'><tr> <th>Username</th> <th>IP Address</th><th>Email Address</th> <th>Last Visit Date-Time </th></tr>";
    
// keeps getting the next row until there are no more to get
    
while($row mysql_fetch_assoc$result ))
    {
     
// Print out the contents of each row into a table
     
echo "<tr><td>".$row['username']."</td>
           <td><TEXTAREA name='text
$i' style='width: 150px; height: 20px;'>".$row['pcidip']."</TEXTAREA>
                <input onclick=\"doact('text
$i')\" type='button' value='Copy IP'></td>
           <td>"
.$row['email']."</td>
           <td>"
.$row['datetime']."</td>
          </tr>"
;
    
$i++;
    }
        echo 
"</table></form>";
?>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #6 (permalink)  
Old 04-12-10, 12:27 AM
MesaFloyd MesaFloyd is offline
Newbie Coder
 
Join Date: Mar 2008
Location: Calif
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
********SOLVED****************
Mr. Broughton, thank you so much.
Absolutly perfect!!!
Works just as I trying.
It is nice to receive such professional level help
I will study your changes to help me in the future
Deep appreciation..
Floyd
Reply With Quote
  #7 (permalink)  
Old 04-12-10, 07:39 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your welcome.
__________________
Jerry Broughton
Reply With Quote
  #8 (permalink)  
Old 05-05-10, 10:49 PM
barbarahelen barbarahelen is offline
New Member
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Incrementing a PHP loop

It should be $counter otherwise PHP will see it always as 0 and not changing, therefore it will always be smaller than $endNum, therefore the condition will never be satisfied thus the loop never ends!
Alpine White Teeth
Reply With Quote
  #9 (permalink)  
Old 05-06-10, 08:50 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by barbarahelen View Post
It should be $counter otherwise PHP will see it always as 0 and not changing, therefore it will always be smaller than $endNum, therefore the condition will never be satisfied thus the loop never ends!
Alpine White Teeth
barbarahelen,
What are you talking about?
Where do you see $counter and $endNum?
__________________
Jerry Broughton
Reply With Quote
  #10 (permalink)  
Old 05-08-10, 05:38 AM
edwardbrian edwardbrian is offline
New Member
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
JavaScript

Try these solutions. Open your browser, click the menu button, scroll to Options. Go into Browser Configuration and check "Support JavaScript".

Acai Complete 6
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Array loop zerofill PHP 2 09-27-09 03:08 PM
[SOLVED] it works on localhost but not on hosting account? myslowquietlife PHP 42 12-19-08 09:31 AM
edit-in-place problem in AJAX and PHP with while loop littlegreen JavaScript 6 03-02-08 01:54 PM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM


All times are GMT -5. The time now is 02:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.