Ok, this is my solution and it works flawlessly:
PHP:
<?php
ini_set('magic_quotes_gpc', false);
header('Content-type: text/plain');
$dbhost = 'hostname';
$dbuser = 'database_username';
$dbpass = 'database_password';
$dbname = 'database_name';
header('Access-Control-Allow-Origin: *');
$sql = "do your DB query SELECT what ever here";
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->query("set names utf8");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query($sql);
$json_export = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
echo '{"items":'. json_encode($json_export) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
?>
HTML LISTVIEW:
<div data-role="page" id="myPage"><div data-role="content" id="myContent">
//my HTML list tag
<ul data-role="listview" id="myList"></ul>
</div></div>
JAVASCRIPT
$('#myPage').live('pageshow',function (event) {
$.getJSON('path_to_your_php_json_generator_file_declared_upper',function(data){
var json_entries = data.items;
$.each(json_entries,function(index,entry){
$('#myList').append('<li><a href="#">' + entry.title + '</a></li>');
});
$('#myList').listview('refresh');
});
});
And here is how you populate the list on jQuery Mobile with the JSON data generated by the php file. You can adapt this type of script to each JSON interpreter in your jQuery code even with parameters (id, category, etc.)!
Hope this helps anyway!
source
share