Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4050920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:07:47+00:00 2026-05-20T14:07:47+00:00

Im a newbie in PHP (also JSON for that matter) but I am not

  • 0

Im a newbie in PHP (also JSON for that matter) but I am not sure/confident of the format of the json data i have. I am converting a mysql into json for use in another application, and the format i am getting is as follows:

    [
     {
        "category":
        {
            "catId":"1",
            "categoryName":"BABY FOOD",
            "categoryNotes":"ONLY baby food varieties which have been listed below should be used. However if your doctor or pediatrician requires a specific diet please contact your Rabbi for further advice."
        }
    },
    {
        "category":
        {
            "catId":"2",
            "categoryName":"BAKING INGREDIENTS",
            "categoryNotes":""
        }
    },
    {
        "category":
        {
            "catId":"131",
            "categoryName":"BEER",
            "categoryNotes":"See Alcoholic Drinks"
        }
    },
    {
        "category":
        {
            "catId":"4",
            "categoryName":"BEVERAGES - Powdered",
            "categoryNotes":""
        }
    },
    {
        "category":
        {
            "catId":"5",
            "categoryName":"BISCUITS",
            "categoryNotes":"Locally produced biscuits usually contain fats, oils or improvers of non-kosher origin.  There is a very large range of Israeli, South African and American kosher biscuits available locally. Even these imported biscuits and crackers (including Israeli produced goods) should only be used if marked with a reliable Rabbinic Hechsher. A 'K' on the packet alone is insufficient. Please note which products are dairy or pareve."
        }
    },
    {
        "category":
        {
            "catId":"6",
            "categoryName":"BREAD AND BAKERY GOODS",
            "categoryNotes":"Bread usually contains or comes in contact with Non-Kosher oils, fats or improvers and cannot be accepted as kosher without thorough investigation and subsequent authorisation"
        }
    },
    {
        "category":
        {
            "catId":"7",
            "categoryName":"BREAD\/CORN\/RICE CRUMBS",
            "categoryNotes":""
        }
    },
    {
        "category":
        {
            "catId":"8",
            "categoryName":"BUTTER AND DAIRY BLENDS",
            "categoryNotes":"Soft butters, butter spreads & dairy blends are NOT ACCEPTABLE unless specifically listed. Butter made from whey cream is NOT ACCEPTABLE unless produced under Rabbinic supervision"
        }
    },
    {
        "category":
        {
            "catId":"9",
            "categoryName":"BUTTERMILK and LEBEN ",
            "categoryNotes":""
        }
    },
    {
        "category":
        {
            "catId":"10",
            "categoryName":"CAKES & CAKE SHOPS",
            "categoryNotes":"Cakes and cake mixes usually contain Non-Kosher ingredients, and must only be used if they are produced under supervision <br\/> Cakes bought in a Non-Kosher cake shop, even if containing only kosher ingredients are NOT ACCEPTABLE because of the Non-Kosher utensils used in their preparation"
        }
    },
    {
        "category":
        {
            "catId":"11",
            "categoryName":"CEREALS ",
            "categoryNotes":""
        }
    },
    ...
]

and so forth.

Now I am not sure if this is the right format, but my code to generate this is as follows:

            <?php


            /* soak in the passed variable or set our own */
            // $approved_prod_date = $_GET['date'];



            $link = mysql_connect('localhost','root','broncos') or die('Cannot connect to the DB');
            mysql_select_db('iKosher',$link) or die('Cannot select the DB');

            /* grab the categories from the db */
            $query=  "SELECT c.id as catId, c.name as categoryName,     c.notes as categoryNotes
                FROM        cat c
                WHERE       1=1\n";


            if(isset($_GET['catid']) && intval($_GET['catid'])) {
                $query.="AND c.id = ";
                $query.= $_GET['catid'];
            }   


            if(isset($_GET['startIndex']) && intval($_GET['startIndex'])) {
                if(isset($_GET['numItems']) && intval($_GET['numItems'])) {
                    $query .= "\nLIMIT ";
                    $query .= $_GET['startIndex'];
                    $query .= ","; 
                    $query .= $_GET['numItems'];
                }
            }




             // $query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
            $result = mysql_query($query,$link) or die('Errant query:  '.$query);
            $num=mysql_numrows($result);


            /* create one master array of the records */
              $categories = array();
              if($num) {
                while($category = mysql_fetch_assoc($result)) {
                  $categories[] = array('category'=>$category);
                }
              }
                header('Content-type: application/json');
                echo json_encode($categories);
            ?>

Based on my code, am i doing this right? Is the array converted correctly into JSON?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:07:48+00:00Added an answer on May 20, 2026 at 2:07 pm

    json_encode() will produce valid JSON. If necessary, you can verify this for yourself by copy-and-pasting the JSON output into an online validator like the one at http://www.jsonlint.com/

    If you’re not sure if your data is being placing into the array with the correct organization, that’s another matter. But given a valid PHP array/object/pretty much whatever, json_encode() will produce it encoded properly in the JSON format.

    EDIT: Inside your while loop, this code:

            while($category = mysql_fetch_assoc($result)) {
              $categories[] = array('category'=>$category);
            }
    

    Builds an array that looks like this: (“category” => [$catId, $categoryName, etc.], “category” => [$catId, $categoryName, etc], and so on) I think you’d be better off (and happier with the JSON produced) if you changed it to:

            while($category = mysql_fetch_assoc($result)) {
              $categories[] = $category;
            }
    

    The original code is adding each and every category to the $categories array with the same “category” key. But since this key is duplicated for each category, it serves no real purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP and have installed on Linux to boot (also a newbie).
I am a newbie in php, mysql. I have written a hello.php script, which
I'm not a newbie to css and html, but totally not smart with php.
AM a newbie in php, i have seen some web applications that have only
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie to php and have been trying to use Eclipse PDT
Is is that I'm a newbie learning Ruby, or does it really have more
php newbie here..I need some PHP help ideas/examples on how to import data from
I'm a php newbie (but long time developer in other languages) and I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.