//HERE ANSWER COMES...
//THIS FUNCTION IS IN A PHP FILE BUT FULL OF JS CODE
//THE LAST LINE PASSES THE DATA TO ANOTHER PHP FILE WHICH SAVES THE DATA INTO A DB
function saveProfile (){
var _profileId = 0;
var _profileName = document.getElementById('nameProfile').value;
var queryArr=[];
$(markersArray).each(function (index){
//alert(markersArray[index].name);
var _locationId = index;
var _locName = markersArray[index].name;
var _markerLat = markersArray[index].marker.getLatLng().lat();
var _markerLng = markersArray[index].marker.getLatLng().lng();
var locations = {
profileName: _profileName,
locationId:_locationId,
locationName:_locName,
lat:_markerLat,
lng:_markerLng }
queryStr = { "locations": locations}
queryArr.push(queryStr);
});
/*for ( var i=0; i<markersArray.length; i++){
alert(queryArr[i].locations.locationId+"--"+queryArr[i].locations.locationName +"--"+queryArr[i].locations.lat);
}*/
$.post('dbConn.php', { opType:"saveAsProfile" , data: queryArr}, showResult, "text");
}
//THIS IS dbConn.php WHICH IS CALLED BY saveProfile METHOD. THE DATA IS HANDLED AS FOLLOWS:
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'google_map_db';
$opType = $_POST['opType'];
//SAVE PROFILES WITH A PROFILE NAME
if(!strcmp($opType, "saveAsProfile") ){
$res = $_POST['data'];
$connect = mysql_connect( $db_host, $db_user, $db_pass ) or die( mysql_error() );
mysql_select_db( $db_name ) or die( mysql_error() );
$queryString = "";
for($i = 0; $i < sizeof($res); $i++){
$profileName = $res[$i]['locations']['profileName'];
$locationId = $res[$i]['locations']['locationId'];
$locationName = $res[$i]['locations']['locationName'];
$lat = $res[$i]['locations']['lat'];
$lng = $res[$i]['locations']['lng'];
$sp = " ";
$queryString = $queryString . "(0 ".",'".$profileName."',".$locationId.",'".$locationName."',".$lat.",".$lng.") ";
if($i<sizeof($res)-1)
$queryString = $queryString . ", ";
}
$qInsertUser = mysql_query(" INSERT INTO `map_locations` (`profileId`, `profileName`, `locationId`, `locationName`, `lat`, `lng`)
VALUES ".$queryString." ");
if ($qInsertUser){
echo "successfully added!!!";
} else {
echo "Error";
}
}
//I HOPE THIS HELPS TO OTHER PEOPLE...
//CHEERS.. Ozlem.