Jump to content



Recommended Posts

Hey guys its my first post ...So i have a suggestion box for countries and i need to get the countries from SQL database..It only shows the last one ...

<?php mysql_connect("", "root", "---") or die(mysql_error());  mysql_select_db("xxx") or die(mysql_error());  $data = mysql_query("SELECT * FROM countries")  or die(mysql_error());   while($info = mysql_fetch_array( $data, MYSQL_ASSOC))  { ?>
       $(document).ready(function() {            var jsonData = [];            var cities = '<?php echo $info['country'].","; }   ?>'.split(',');            for(var i=0;i<cities.length;i++) jsonData.push({id:i,name:cities[i],status:i%2?'':'Planned for visit',coolness:Math.floor(Math.random() * 10) + 1});            var ms1 = $('#ms1').magicSuggest({                data: jsonData,                sortOrder: 'name',                value: [0,1,2,3,4,5,6],                groupBy: 'status',                maxDropHeight: 2000,                name: 'ms1'

THank you.


Link to comment
Share on other sites

it looks like you're putting the entire document ready inside the mysql_fetch_array loop so every time its iterating over a country, its writing an extra document ready handler and writing a completely new var cities which is overwriting the previous (which is why you're only getting the last one). you could change the while loop to...

$cities = "";$data = mysql_query("SELECT country FROM countries");while($info = mysql_fetch_row($data)){  $cities .= $info[0].',';}?>$(document).ready(function(){  var cities = <?=$cities;?>.split(',');...continued code...

This should solve your problem where only the last country is appearing. Theres likely a better way to transfer arrays from php to javascript however...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...