0

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"]

1 Answer 1

1

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'];
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I declared it. But its still the same.
Can you check your query? How many records are available for cid.
3 rows are available. The query is returning correct.
Check the edited answer. I have changed this line $results['projects'] = $row['project_name'];. I think this will work now.
that is working, But the thing is that I need not only the project_name but also the other coloumns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.