Reputation: 643
In my application I have to load data based on category. What I am doing is when click on the category, click function is called where I take category id and is passed to $.post
method to a php page load_project.php
. I that page I select the projects based on the category id and I have to sent back the selected rows to success function. My problem is when I sent the data back In my array I'll get only the last row on alert of data. I dont know where I am going. Can anyone please help me.
Php page:
<script>
$(document).ready(function() {
$("#categorylink").click(function() {
cid = $(this).attr("data-cid");
$.post('load_project.php', { cid: "" + cid + "" }, function(data) {
alert(data); //here I get only one row, but there is actually 3 rows
});
});
});
</script>
<ul>
<a><li data-filter="all">>All</li></a>
<?php $get=$config->get_cat();
while($row = $get->fetch(PDO::FETCH_ASSOC)) {
?>
<a id="categorylink" data-cid="<?php echo $row['category_id'];?>">
<li>
<?php echo $row['category_name'];?>
</li>
</a> //this is my categories
<?php } ?>
</ul>
load_project.php
<?php
require_once 'config.php';
$config = new DBConfig;
if(isset($_POST['cid']))
{
$cid=$_POST['cid'];
$get=$config->getprojectbycategory($cid);
while($row=$get->fetch(PDO::FETCH_ASSOC)) {
$results['projects'] = $row;
}
echo json_encode($results);
}
?>
From the success function I have to populate the rows to a grid. I am unable to do this. So currently I am passing the category id to the php page itself through href and populating the data after a page reload. Since its not that good way to load the data, I want to replace it by jquery ajax. Please help me.
Edit 1
When I try like this:
$results[] .= $row['project_name'];
I am getting the three project names in alert: [[],"Private Palace","Private Villa F","23rd Floor Addax Tower"]
Upvotes: 0
Views: 147
Reputation: 1135
This issue is because the values you are storing in array get overwrites with previous values.
Solution for this is declare $results['projects'] = array();
before while loop.
$results['projects'] = array();
while($row=$get->fetch(PDO::FETCH_ASSOC)){
$results['projects'] = $row['project_name'];
}
Upvotes: 1