FOR loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

FOR loop

Post by S_henry »

This is part of my code in javascript function:

Code: Select all

'<?php echo $i = 1; ?>'
var j = 1;
for(var a=1; a<='<?php echo $x;?>'; a++){
  alert('<?php echo $i;?>');
  alert(j);

  '<?php $i = $i + 1; ?>'
  j = j + 1;
}
In this loop, the result is 'j' value will increase each time base on the value of '$x'. The problem is value for '$i' is not increase, the value is still the same (ie. 1) until looping was finished. Anybody know how to solve this problem?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

server side php is...client side js is. Mix the two like you're trying you can not.

increment $i will not on the client side.

the code the client will receive this is:

Code: Select all

1
var j = 1;
for(var a=1; a<={whatever $x is on the server side say 3 for this example} 3; a++){
  alert('1');
  alert(j);
  ''
  j = j + 1;
}
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Actually i want to use index array in that loop. Here is the example (code in javascript):

Code: Select all

'<?php
$query = &quote;SELECT studName FROM Student&quote;;
$result = mysql_query ($query, $db)
             or die (&quote;Query failed&quote;);
$b=1;
while ($point=mysql_fetch_object ($result)) { 
   $sn&#1111;$b] = $point->studName;
   $b=$b+1;
}
?>'
'<?php echo $i = 1; ?>'
var j = 1;
for(var a=1; a<='<?php echo $x;?>'; a++){
  alert('<?php echo $sn&#1111;$i];?>');
  alert('<?php echo $i;?>');
  alert(j);
  '<?php $i = $i + 1; ?>'
  j = j + 1;
}
So anybody has any idea to solve this problem?
Post Reply