Multiple Google map with php loop - api

I am trying to dispaly multiple maps with a php loop but i cant get it to work here is the code stripped down...
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=ABQIAAAA5iXFaQoABBiRl7JNx5HFuxSa5RD4FXABRIVtLveK1-E6gmai7BR1Y63hhzwAO9ZPoNDgLDBQ_Z6B4Q" type="text/javascript"></script>
</head>
<body onload="load()" onunload="GUnload()">
<?php
if($_POST['select'] == "place"){
$posts = $facebook->api("/search?q=".urlencode($_POST['search'])."&type=".urlencode($_POST['select'])."&center=".urlencode($_POST['center'])."&distance=".urlencode($_POST['distance'])."");
//print_r($posts);
$num = "1";
$num2 = "1";
foreach($posts as $v) {
foreach($v as $v2) { ?>
<div>
<script type="text/javascript">
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
}
}
//]]>
<div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
<p><?php echo $v2['location']['latitude']; ?></p>
<p><?php echo $v2['location']['longitude']; ?></p>
</div>
<?php
}
}
}
}
?>
</body>
</html>
I have tried to put the javascript within the loop but this doesnt work.
Has anyone got any suggestions at the moment it will only show the last map within the loop???
Thanks

I can tell you why it doesn't work. You're redefining the same load() function each time through the loop. So when document loads, and load() gets called, it just displays the last map. By the way, you should also be using google map V3 api instead of V2, but that's a different topic.
Something like this should fix this. First, put this in the <head> section:
<script type="text/javascript">
var loaders = [];
function load() {
for (var i = 1; i < loaders.length; i++) {
loaders[i]();
}
}
</script>
Then change your loop to this:
foreach($posts as $v) {
foreach($v as $v2) { ?>
<div>
<script type="text/javascript">
//<![CDATA[
loaders[<?php echo $num; ?>] = function() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
}
};
//]]>
<div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
<p><?php echo $v2['location']['latitude']; ?></p>
<p><?php echo $v2['location']['longitude']; ?></p>
</div>
<?php
}
}

Related

PHP variable auto update failure

I have this code that I want that $X to be updated in the form, so when I press the +1 button it just keep going adding 1 to the form
<html>
<head>
<title>7mada</title>
</head>
<body>
<?php $X = 7 ;?>
<form method="POST" >
<table border="0" width="60%">
<tr> <td width ="30%"> Number: </td><td><input type="test" name="newname" value="<?php echo $X ;?>" readonly></td></tr>
</table>
<input name="save" type="submit" value="+1"/>
</form>
<?php
if($_POST){
if($_POST['save'] == "+1"){
$_POST['newname']++;
echo $_POST['newname'];
$X = $_POST['newname'];
}
}else{echo "say smth";}
?>
</body>
</html>
thanks.
Hey Samy this is the answer for your question :D :D
It completely ignores HTML output, therefore PHP can't interact with events that should be javascript.
you have two options:
1-Do it in Javascript, Live
2-Do it in PHP, requires refresh
If you want to do it in JavaScript, you'd do this:
<html>
<body>
<button onclick="add()">Add</button>
<div id="showIt"><script type="text/javascript">document.write(x);</script></div>
</body>
<head>
<title></title>
<script type="text/javascript">
var x = 0;
function add() {
x++;
document.getElementById('showIt').innerHTML = x;
}
</script>
</head>
</html>
or in PHP:
<?php
session_start();
$_SESSION['x'] = ((isset($_SESSION['x'])) ? $_SESSION['x'] : 0);
if(isset($_GET['add'])){
$_SESSION['x']++;
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="submit" name="add" value="add" />
</form>
<?php
echo $_SESSION['x'];
?>
</body>
</html>
I hope this helps you,
Mahmoud Elsaadany

dojo datagrid autoheight not working when programmatically defined

The following code builds 3 datagrids, 2 via markup and one via code.
When you press the "autoheight!" button only the markup datagrids resize.
I don't understand why the code datagrid does not work. As far as i can see, the same attributes are being initialized.
Thanks
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://js.arcgis.com/3.13/dijit/themes /claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.13/dojox/grid/resources/claroGrid.css">
<script>dojoConfig = {parseOnLoad: true}</script>
<script src="http://js.arcgis.com/3.13/"></script>
<script>require(["dojo/parser", "dijit/layout/TabContainer", "dijit/layout/ContentPane"]);</script>
<meta charset="UTF-8">
<title>alubia</title>
<script type="text/javascript">
var test_store;
var layout = [
{name:"id", field:"id", width: '165px', noresize: 'true'},
{name:"data", field:"data", width: '125px', noresize: 'true'}
];
function loadData(gridId, divId) {
require(['dojo/data/ItemFileWriteStore', 'dojox/grid/DataGrid'], function(ItemFileWriteStore, DataGrid) {
var mi_data = {
items : [],
identifier:"id"
};
for (var i = 0; i < 22; i++) {
mi_data.items.push({id: ""+i, data:"111 ! "+ i});
}
test_store = new ItemFileWriteStore({data: mi_data});
if (divId != null) {
var grid = new DataGrid({
id : gridId,
store : test_store,
structure : layout,
rowSelector : '0px',
autoHeight : false
});
grid.placeAt(divId);
grid.startup();
}
});
}
function fitHeight(gridId) {
var grid = dijit.byId(gridId);
grid.set('autoHeight', true);
grid.set('autoWidth', false);
grid.update();
}
loadData("grid", null);
loadData("grid2", "grid2Div");
loadData("grid3", null);
</script>
</head>
<body class="claro" style="font-family:sans-serif; font-size:12px;">
<button onclick="fitHeight('grid'); fitHeight('grid2'); fitHeight('grid3');">autoheight!</button>
<div id="grid2Div" style="height: 7em;" ></div>
<div id="grid" style="height: 7em;" data-dojo-id="grid" dojoType="dojox.grid.DataGrid" autoHeight="false" store="test_store" structure="layout" ></div>
<div id="grid3" style="height: 7em;" data-dojo-id="grid3" dojoType="dojox.grid.DataGrid" autoHeight="false" store="test_store" structure="layout" ></div>
</body>
</html>
again, dojo is a horrible tool. going to datatables (jquery plugin). the sun shines again.

Yii, new controller render does not have the same layout like default

i dont get why the new controller im making which is rendering a file in an action does not seem to have the same layout in content like yii generates automatically, the site/index, just like contact and about have their content a bit centered in the content, like some padding or margin, but when i make a new controller, it does look the same, looks a bit ugly being stick to the line where the content begins.
heres the new controller im making:
<?php
class ToolsController extends CController
{
// -----------------------------------------------------------
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
public function actionIndex()
{
$this->render('index');
}
}
and its just rendering this:
<h1>hello</h1>
im missing something?
sorry heres the layout:
<?php /* #var $this Controller */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/dist/css/bootstrap.css">
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'htmlOptions'=>array('class'=>'nav nav-pills'),
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Tools', 'url'=>array('/tools/index')),
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
<?php echo $content; ?>
<div class="clear"></div>
<div id="footer">
Copyright © <?php echo date('Y'); ?> by My Company.<br/>
All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->
</div><!-- page -->
</body>
</ht
ml>
Quick fix is:
public $layout='//layouts/myLayout';
Pretty sure you can also set it in the Base Controller also

App works only for but not for other users

I am editing the default app provided by the heroku. This works with my login. But other users doesn't see app working. My app requires permission - user_relationships, friends_relationships.
When I debug the access_token seen by the user, it correctly specifies the above permissions.
Somebody can please point me what is the issue?? Thanks in advance.
<?php
// Provides access to app specific values such as your app id and app secret.
// Defined in 'AppInfo.php'
require_once('AppInfo.php');
// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}
// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');
/****************************************************************************
*
* The content below provides examples of how to fetch Facebook data using the
* Graph API and FQL. It uses the helper functions defined in 'utils.php' to
* do so. You should change this section so that it prepares all of the
* information that you want to display to the user.
*
****************************************************************************/
require_once('sdk/src/facebook.php');
// Instantiating Facebook object to access SDK
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
));
// Returns facebook user id of the current user
$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
$access_token = $facebook->getAccessToken();
echo $access_token;
// Here is an example of a FQL call that fetches all of your friends that are
// using this app
$app_using_friends = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT uid, name, relationship_status FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me())'
));
}
// Fetch the basic info of the app that they are using
$app_info = $facebook->api('/'. AppInfo::appID());
$app_name = idx($app_info, 'name', '');
?>
<!DOCTYPE html>
<html xmlns:fb="http://ogp.me/ns/fb#" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
<title><?php echo he($app_name); ?></title>
<link rel="stylesheet" href="stylesheets/screen.css" media="Screen" type="text/css" />
<link rel="stylesheet" href="stylesheets/mobile.css" media="handheld, only screen and (max-width: 480px), only screen and (max-device-width: 480px)" type="text/css" />
<!--[if IEMobile]>
<link rel="stylesheet" href="mobile.css" media="screen" type="text/css" />
<![endif]-->
<!-- These are Open Graph tags. They add meta data to your -->
<!-- site that facebook uses when your content is shared -->
<!-- over facebook. You should fill these tags in with -->
<!-- your data. To learn more about Open Graph, visit -->
<!-- 'https://developers.facebook.com/docs/opengraph/' -->
<meta property="og:title" content="<?php echo he($app_name); ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo AppInfo::getUrl(); ?>" />
<meta property="og:image" content="<?php echo AppInfo::getUrl('/logo.png'); ?>" />
<meta property="og:site_name" content="<?php echo he($app_name); ?>" />
<meta property="og:description" content="My first app" />
<meta property="fb:app_id" content="<?php echo AppInfo::appID(); ?>" />
<script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function logResponse(response) {
if (console && console.log) {
console.log('The response was', response);
}
}
$(function(){
// Set up so we handle click on the buttons
$('#postToWall').click(function() {
FB.ui(
{
method : 'feed',
link : $(this).attr('data-url')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
$('#sendToFriends').click(function() {
FB.ui(
{
method : 'send',
link : $(this).attr('data-url')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
$('#sendRequest').click(function() {
FB.ui(
{
method : 'apprequests',
message : $(this).attr('data-message')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
});
</script>
<!--[if IE]>
<script type="text/javascript">
var tags = ['header', 'section'];
while(tags.length)
document.createElement(tags.pop());
</script>
<![endif]-->
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo AppInfo::appID(); ?>', // App ID
channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
//oauth : true,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
//FB.login({
// scope : 'user_relationships,friends_relationships'
// });
// Listen to the auth.login which will be called when the user logs in
// using the Login button
FB.Event.subscribe('auth.login', function(response) {
// We want to reload the page now so PHP can read the cookie that the
// Javascript SDK sat. But we don't want to use
// window.location.reload() because if this is in a canvas there was a
// post made to this page and a reload will trigger a message to the
// user asking if they want to send data again.
window.location = window.location;
});
FB.Canvas.setAutoGrow();
};
// Load the SDK Asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<header class="clearfix">
<?php if (isset($basic)) { ?>
<p id="picture" style="background-image: url(https://graph.facebook.com/<?php echo he($user_id); ?>/picture?type=normal)"></p>
<div>
<h1>Welcome, <strong><?php echo he(idx($basic, 'name')); ?></strong></h1>
<p class="tagline">
This is my app. My First Facebook app.
<?php echo he($app_name); ?>
</p>
<div id="share-app">
</div>
<?php } else { ?>
<div>
<h1>Welcome</h1>
<div class="fb-login-button" data-scope="user_likes,user_photos,user_relationships,friends_relationships"></div>
</div>
<?php } ?>
</header>
<?php
if ($user_id) {
?>
var $shaheeds = 0;
var $aazaads = 0;
var $confused = 0;
var $in_trap = 0;
var $total = 0;
<section id="samples" class="clearfix">
<h1>Examples of the Facebook Graph API</h1>
<div class="list">
<h3>Shaheed Friends</h3>
<ul class="shaheed">
<?php
foreach ($app_using_friends as $friend) {
// Extract the pieces of info we need from the requests above
$id = idx($friend, 'uid');
$name = idx($friend, 'name');
$r_status = idx($friend, 'relationship_status');
if($r_status == "Married"){
$shaheeds++;
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
<img src="https://graph.facebook.com/<?php echo he($id) ?>/picture?type=square" alt="<?php echo he($id); ?>">
<?php echo he($name); ?>
</a>
</li>
<?php
}
}
?>
</ul>
</div>
<div class="list inline">
<h3>Recent photos</h3>
<ul class="photos">
<?php
$i = 0;
foreach ($photos as $photo) {
// Extract the pieces of info we need from the requests above
$id = idx($photo, 'id');
$picture = idx($photo, 'picture');
$link = idx($photo, 'link');
$class = ($i++ % 4 === 0) ? 'first-column' : '';
?>
<li style="background-image: url(<?php echo he($picture); ?>);" class="<?php echo $class; ?>">
</li>
<?php
}
?>
</ul>
</div>
<div class="list">
<h3>Friends In Trap</h3>
<ul class="in-trap">
<?php
foreach ($app_using_friends as $friend) {
// Extract the pieces of info we need from the requests above
$id = idx($friend, 'uid');
$name = idx($friend, 'name');
$r_status = idx($friend, 'relationship_status');
if($r_status == "Engaged" || $r_status == "In a relationship"){
// This display's the object that the user liked as a link to
// that object's page.
$in_trap++;
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
<img src="https://graph.facebook.com/<?php echo he($id) ?>/picture?type=square" alt="<?php echo he($name); ?>">
<?php echo he($name); ?>
</a>
</li>
<?php
}
}
?>
</ul>
</div>
<div class="list">
<h3>Azaad Friends</h3>
<ul class="friends">
<?php
foreach ($app_using_friends as $auf) {
// Extract the pieces of info we need from the requests above
$id = idx($auf, 'uid');
$name = idx($auf, 'name');
$r_status = idx($auf, 'relationship_status');
if($r_status == "Single"){
$aazaads++;
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
<img src="https://graph.facebook.com/<?php echo he($id) ?>/picture?type=square" alt="<?php echo he($name); ?>">
<?php echo he($name); ?>
</a>
</li>
<?php
}
$total++;
}
$confused = $total - $aazaads - $shaheeds - $in_trap;
?>
</ul>
</div>
</section>
<section id="graphs" class="clearfix">
<h1>"Graph"</h1>
<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:<?php echo he($shaheeds)?>,<?php echo he($aazaads)?>,<?php echo he($in_trap)?>,<?php echo he($confused)?>&chs=250x100&chl=Shaheed|Aazaad|In Trap|Unknown" />
</section>
<?php
}
?>
</body>
</html>
I am experiencing the same problem. for this one thing is sure we have to disable sandbox mode. for that you must go to facebook developers app. And in that advanced. But i am facing the problem. because to disable sandbox mode we have to provide privacy policy url and secure canvas page url.

SQL/PHP make an array with column name / value for specific unique id

I have an sql table named clients where each client is stored in a row, with a unique (auto increment) id and the column names are: clientname, clientlastname, clientmobile and so on.
I want to make an array that will retrieve the contents of a specific client (row) and will be like : clientname => lalala, clientlasthname=>lalalala... and so on.
Then I want to display the results in an html form (non submitable, just display)
each cell in the form has a unique id and name same as the column names of the sql db.
I just new at it and cant seem to get it.
Here's what I've done so far:
<?php
header("Content-Type:text/html; charset=utf-8");
if ($_POST) {
$link = mysql_connect("localhost","userid","pw");
if (!$link) { die("Database Connection Failed". mysql_error()); }
mysql_set_charset('utf8',$link);
$db_select = mysql_select_db("form2",$link);
if(!$db_select){ die("Database Selection Failed " . mysql_error()); }
} else { echo "Search:"; }
?>
<html>
<head> </head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/dsearch.css" />
<link rel="stylesheet" href="css/base.css" />
<link type="text/css" href="css/selected/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery-ui.min.js"></script>
<script language="javascript" type="text/javascript" src="js/dropdowndata.sql/dropdowndata.clients.js"></script>
<script language="javascript" type="text/javascript" src="js/buttons.js"></script>
<body>
<form class="form" id="dsearch" name="dsearch" action="dsearch.php" method="POST" enctype="application/x-www-form-urlencoded">
<div class="dsearch">
<li class="ui-widget"><label for="clients">client id: </label><input class="client" name="searchcid" style="width:100px" /></li>
<li class="cell3"><input type="submit" value="Αναζήτηση" style="color:green; width:210px; " /></li>
</div>
</form>
<div class="results">
<?php
$result1 = mysql_query("SELECT * FROM clients WHERE id='$_POST[searchcid]'", $link);
if(!$db_select) { die("No Data found in db " . mysql_error()); };
$items=array();
while($row = mysql_fetch_array($result1)){
$items[$row['id']] = $row['dlastname'];
/* here is where I need the help I guess,
I need to fill the $items array with the data formatted as mentioned above.
And after that I want to populate a form that I haven't already written with the
values ,any help or maybe any suggestions on some tutorials I could go study
would be appreciated.
It goes on like this: */
}
print_r ($items);
?>
</div>
</body>
</html>
<?php
if ($_POST) {
mysql_close($link);
}
?>
Edit: figured out how to get the array
$clinfo=array();
$clinfo=mysql_fetch_assoc($result1);
print_r ($clinfo);
Now I need a way to go put the data where the field id equals the id of the docinfo array
figured out how to do it with a little php and jquery,
here it is:
$(document).ready(function(){
<?php
if($_GET){
foreach($docinfo as $key=>$val) {
echo "$('#" . $key . "').val('" . htmlspecialchars($val) ."');";
}
}
?>
});
This creates a jquery line for each cell id and fills it with the appropriate value
Any comments or improvements are extremely appreciated