1

Here i want compare two array.

I have two tables one is CATEGORY and second is USER(in which selected multiple category is stored in this format , , , ).

but when the USER want to update their category at that time the earlier selected category should return checked 1 and which is not selected should return checked 0 and here is my code.

case 'cat':
            $sql="SELECT category from nesbaty_user where user_id='".$user_id."'";
            $qry_res=mysqli_query($con,$sql);
            $response1 = array();
            while ($array = mysqli_fetch_array($qry_res)) 
            {
                foreach (explode(',', $array['category']) as $cat) 
                {
                    $response1[]=array('category' => $cat);
                    $response['Selected_category'] = $response1;
                }

            }
            $qry="SELECT cat_id,category,image_url FROM nesbaty_category";

            $qry_res=mysqli_query($con,$qry);
            $jsonData = array();
            while ($array = mysqli_fetch_assoc($qry_res)) 
            {
                $jsonData[] = $array;           
                $response['category'] = $jsonData;
            }
            echo json_encode($response);
            //echo json_encode(array('data1' =>$response1));
            //
            mysqli_close($con);
        break;

and here is my fetched array from the database.

{
"Selected_category": [
    {
        "category": "5"
    },
    {
        "category": "6"
    },
    {
        "category": "9"
    }
],
"category": [
    {
        "cat_id": "1",
        "category": "Drug",
        "image_url": "1.jpg"
    },
    {
        "cat_id": "2",
        "category": "Bars",
        "image_url": "2.jpg"
    },
    {
        "cat_id": "3",
        "category": "Bars",
        "image_url": "2.jpg"
    },
    {
        "cat_id": "4",
        "category": "Hair Saloon",
        "image_url": "2.jpg"
    }
]

}

5
  • What does it meach checked? show expected output. Commented May 29, 2018 at 9:20
  • its just string { "cat_id": 1, "category": "Drug", "image_url": "1.jpg", "checked": "0" }, Commented May 29, 2018 at 9:24
  • or checked 1 if earlier selected Commented May 29, 2018 at 9:25
  • Do you need it with this same structure as you provided? Commented May 29, 2018 at 9:37
  • structure doesent matter output matters Commented May 29, 2018 at 10:06

1 Answer 1

1

This is for handle this same structure as You provided.

<?php

$response = [];

$sql = "SELECT category from nesbaty_user where user_id='".$user_id."'";
$qry_res = mysqli_query($con, $sql);

$selectedCategories = mysqli_fetch_array($qry_res);
$selectedCategories = explode(',', $selectedCategories['category']);

$qry = "SELECT cat_id,category,image_url FROM nesbaty_category";
$qry_res = mysqli_query($con, $qry);

while ($categories = mysqli_fetch_assoc($qry_res)) {
    $categories['checked'] = (int)\in_array($categories['cat_id'], $selectedCategories);
    $response[] = $categories;
}
echo json_encode($response);

mysqli_close($con);

If any error writes a comment, I'll fix it.

Sign up to request clarification or add additional context in comments.

4 Comments

actually i want output like this list of all categories from category table and which category is earlier selected in that array it shouldreturn { "cat_id": 1, "category": "Drug", "image_url": "1.jpg", "checked": "1" } otherwise { "cat_id": 1, "category": "Drug", "image_url": "1.jpg", "checked": "o" }
and your code returns all checked is 0 where id of category may match it should return 1
it shows undefined variable($categories) in this line $selectedCategories = explode(',', $categories['category']);
Sorry my bad. Fixed.

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.