combining PHP and javascript, beginner's frustration

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
DashMasterful
Forum Newbie
Posts: 3
Joined: Thu Nov 13, 2008 11:32 am

combining PHP and javascript, beginner's frustration

Post by DashMasterful »

so i'm new to php, and I'm working on an assignment for class, here is my problem


i grab items from a database, lat and lng are varchar, type, owner, and desc are char

Code: Select all

while($row = mysql_fetch_array($result,MYSQLI_ASSOC)){      
                $markers[$counter] = array( $row['lat'], $row['lng'] , $row['type'] , $row['owner'] , $row['desc'] );
                $counter++;
               
     
} //end while
 
then later on down the road I want to call a function that passes the items that i grabbed from the database

Code: Select all

<?php $counter = 0; ?>
 
    for(i = 0; i < 3; i++){
         addMarker(<?php echo $markers[$counter][0] ?>, <?php echo $band[$counter][1] ?> ,<?php echo $band[$counter][2] ?>, <?php echo $band[$counter][3] ?> , <?php echo    $band[$counter][4] ?>);
         <?php $counter++; ?> //counter starts off at 0 and is initialized someone else
       
    }
 
the $counter variable isn't getting incremented, it just stays at zero, any ideas
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: combining PHP and javascript, beginner's frustration

Post by php_east »

i'll have a go.

the counter is at the server(PHP) while your javascript is executed at the client, and when it loops ( which is a javascript loop ) , it takes on the $counter value passed and parsed by PHP, which is still at initial value of 0.

u need to redesign the loop such that it loops at the server producing incremental counter, to get the counter incemented, then pass it down to the javascript and down to the client.

hope this helps.
Post Reply