I have a mobile app, the first page shows a list of categories, when click on one category go to second page passing the category id on the url and showing a list of business names under that category.
The data is on MySQL so I'm using jSON
Everything is working OK, but instead of showing a list of business names, it shows only the first business repeated 14 times (each business has 14 fields), so instead of looping on the 5 business under one category, looks like is looping inside the fields of the first business of the category and repeating the business name.
Here is the jquery:
$('#businessListPage').live('pageshow', function(event) {
var id = getUrlVars()["id"];
$.getJSON(serviceURL + 'getbusiness.php?id='+id, displayBusiness);
});
function displayBusiness(data) {
var business = data.item;
console.log(business);
$.each(business, function(index) {
$('#actionList').append('<li><a href="">' +
business.business + '</a></li>');
})
$('#actionList').listview('refresh');
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
This is the html
<div id="businessListPage" data-role="page" data-add-back-btn="true">
<div data-role="header">
<h1>Business List</h1>
</div>
<div data-role="content">
<ul id="actionList" data-role="listview" data-inset="true"></ul>
I tried a lot of stuff but I'm not good at jquery, I will appreciate any help. Thanks
**Add this:
This is the php if this helps... I have a normalized table that linke the categories with the business, I tested the Select part and is working well.
<?php
include 'config.php';
$sql = "select * " .
"from directory2 WHERE ID IN(SELECT dirID FROM cat2dir WHERE catID =:id)group by business order by business ";
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare($sql);
$stmt->bindParam("id", $_GET[id]);
$stmt->execute();
$businesses = $stmt->fetchObject();
$dbh = null;
echo '{"item":'. json_encode($businesses) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
?>