Remove the last comma of the JSON file and PHP

I have a JSON file with PHP and I want to remove the last comma because it is throwing an error. The code is as follows:

{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        },
<?php
}
?>
    ]
}

      

I tried many solutions but never worked in this code. I don't know when this is the last iteration of the loop and cannot delete it.

+3


source to share


4 answers


Get the query line count and create a counter to see which line you are executing in the loop, only add a comma if the counter is different from all lines:



  {
    "data": [
    <?php
        require_once("../../config.php");
        $qtodos = $mysqli->query("SELECT * FROM negocios");
        $TotalRcount = $qtodos->num_rows; // <<<< row count
        $counter = 0;

        while($todos = $qtodos -> fetch_assoc()) { $counter++ // <<<< add the counter
            ?>
            {
                "id": <?php echo $todos['idnegocios']; ?>,
                "category": "real_estate",
                "title": "<?php echo $todos['nombre']; ?>",
                "location": "<?php echo $todos['direccion']; ?>",
                "latitude": 51.541599,
                "longitude": -0.112588,
                "url": "item-detail.html",
                "type": "Apartment",
                "type_icon": "assets/icons/store/apparel/umbrella-2.png",
                "rating": 4,
                "gallery":
                    [
                        "assets/img/items/1.jpg",
                        "assets/img/items/5.jpg",
                        "assets/img/items/4.jpg"
                    ],
                "features":
                    [
                        "Free Parking",
                        "Cards Accepted",
                        "Wi-Fi",
                        "Air Condition",
                        "Reservations",
                        "Teambuildings",
                        "Places to seat"
                    ],
                "date_created": "2014-11-03",
                "price": "$2500",
                "featured": 0,
                "color": "",
                "person_id": 1,
                "year": 1980,
                "special_offer": 0,
                "item_specific":
                    {
                        "bedrooms": 4,
                        "bathrooms": 2,
                        "rooms": 4,
                        "garages": 1,
                        "area": 240
                    },
                "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
                "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
                "last_review_rating": 5
            }<?php
            if($TotalRcount != $counter)
            { echo ','};  // <<<< only add the comma when the counter is different than total rows
    }
    ?>
        ]
    }

      

+1


source


There are many possible ways. I just pick one ....
remove the trailing, literal comma and change the script like this

$first = true;
while($todos = $qtodos -> fetch_assoc()) {
    if ($first) {
        $first = false;
    }
    else {
        echo ',';
    }
?>
        {
            "id":

      



i.e. The script does not print a comma after the entry, but before the next entry.

+1


source


You can add a counter and match the score to full lines, so when it hits the last record it won't echo comma

{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    $count = 1;
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        }
<?php
    if ((int)$qtodos->num_rows != $count)
        echo ',';
    $count++;
}
?>
    ]
}

      

0


source


Hope, this will work 
`{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    $count = 0;
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        }
    if($count != mysql_num_rows($todos))
    {
        ,
    }
    $count += 1;
<?php
}
?>
    ]
}`

      

-2


source







All Articles