Page 1 of 1

combining PHP and javascript, beginner's frustration

Posted: Sun Feb 22, 2009 1:05 pm
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

Re: combining PHP and javascript, beginner's frustration

Posted: Sun Feb 22, 2009 6:03 pm
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.