'How can I loop through pages of 100 records until the last page is less than 100 using PHP?

I have this bit of PHP code below.


try {

    echo "Try for getting data";

    $starttime = ("1900-01-01");
    $endtime = $currDate;

    $day1 = strtotime($starttime);
    $day2 = strtotime($endtime);

    $parameters = array('startDate' => $day1, 'endDate' => $day2, 'page' => $pagenbr);
    $data = $client->GetInventoryItemsAddedBetween($parameters);
} catch (Exception $e) {
    printf("Soap Fault: %s\n", $e->Reason);
}

What I have is that $pagenbr is equal to the number of pages containing 100 records per page.

There are 10,000+ records that are retrieved in total.

What I want to do is loop through, adding pages into the JSON object $data where each page number will be another element in the array, $data[0] - $data[n] with each containing 100 records until we reach the final set of LESS than 100.

Here's the code that populates the current JSON from the retrieved records:

if (!empty($data->items->InventoryItem)) {
    // echo "Items added between " . str_replace('T', ' ', $starttime) . " and " . str_replace('T', ' ', $endtime) . "<p></p><ul>";
    foreach ($data->items->InventoryItem as $item) {
        // echo "<li><a href='https://www.murphysmagic.com/product.aspx?id=" . $item->InternalId . "' target='_blank'>$item->Title</a></li>";

        $id = $item->InternalId;
        $title = $item->Title;
        $qty = $item->QuantityAvailable;
        $price = $item->SuggestedRetailPrice;
        $artist_or_magician = $item->ArtistOrMagician;
        $html_description = $item->HTMLDescription;
        $non_html_description = $item->nonHTMLDescription;
        $alt_images = $item->AltImages;
        $dateAdded = $item->DataAdded;
        $wholesale_price = $item->WholesalePrice;
        $title = $item->Title;
        $manufacturer = $item->Manufacturer;
        $IsMurphysItem = $item->isMurphys;
        $productType = $item->ProductType;
        $suggestedRetailPrice = $item->SuggestedRetailPrice;
        $maintainMSRP = $item->MaintainMSRP;
        $imageFileName = $item->ImageFileName;
        $imageThumbnailFileName = $item->ImageThumbnaileFileName;
        $videos = $item->Videos;
        $categories = $item->Categories;
        $christmas_Themed = $item->ChristmasThemed;
        $halloween_Themed = $item->HalloweenThemed;
        $card_magic_and_trick_decks = $item->CardMagicTrickDecks;
        $decks_custom_standard = $item->DecksCustomStandard;
        $lectures_and_conventions = $item->LecturesConventions;
        $magazines = $item->Magazines;
        $money_magic = $item->MoneyMagic;
        $refills = $item->Refills;
        $silk_and_silk_magic = $item->Silks;
        $special_effects_fire_smoke_sound = $item->SPFXFireSmoke;
        $sponge_and_Sponge_magic = $item->Spongeballs;
        $tables_and_tases = $item->TablesCases;
        $theory_history_and_business = $item->TheoryHistoryBiz;
        $toy_magic_toy_kits_puzzles = $item->ToyMagicKitsPuzzles;
        $utility = $item->Utilities;
        $close_up_performer = $item->Closeup;
        $comedy_performer = $item->Comedy;
        $escape_performer = $item->Escape;
        $gambling_performer = $item->Gambling;
        $illusionist = $item->Illusionist;
        $juggling_performer = $item->Juggling;
        $kids_show_and_balloon_performer = $item->KidsBalloons;
        $mentalism_performer = $item->Metalism;
        $stage_parlor_performer = $item->StageParlor;
        $religious_performer = $item->Religious;
        $street_performer = $item->Street;
        $walk_around_performer = $item->Walkaround;
        $black_label = $item->BlackLabel;
        $limited_edition = $item->LimitedEdition;

        // get rid of \\" and then \\""

        $title = str_replace('5\\"x8\\"', '5x8', $title);
        $title = str_replace('\\\\""Six\\\\""', 'Six', $title);
        $title = str_replace('3\\\\"X5\\\\"', '3x5', $title);

        stripslashes($title);

        $return_arr[] = array(
            "id" => $id,
            "title" => $title,
            "qty" => $qty,
            "price" => $price,
            "artist_magician" => $artist_or_magician,
            "htmldesc" => $html_description,
            "textdesc" => $non_html_description,
            "altimages" => $alt_images,
            "dateadded" => $dateAdded,
            "wholesale" => $wholesale_price,
            "manufacturer" => $manufacturer,
            "ismurphys" => $IsMurphysItem,
            "producttype" => $productType,
            "msrp" => $suggestedRetailPrice,
            "maintainmsrp" => $maintainMSRP,
            "imgfilename" => $imageFileName,
            "imgthumbname" => $imageThumbnailFileName,
            "videos" => $videos,
            "categories" => $categories,
            "xmas" => $christmas_Themed,
            "halloween" => $halloween_Themed,
            "carddeckstricks" => $card_magic_and_trick_decks,
            "customstandarddecks" => $decks_custom_standard,
            "lecturesconventions" => $lectures_and_conventions,
            "magazines" => $magazines,
            "money" => $money_magic,
            "refills" => $refills,
            "silks" => $silk_and_silk_magic,
            "spfx" => $special_effects_fire_smoke_sound,
            "sponges" => $sponge_and_Sponge_magic,
            "tablescases" => $tables_and_tases,
            "theoryhistory" => $theory_history_and_business,
            "toyskitspuzzles" => $toy_magic_toy_kits_puzzles,
            "utility" => $utility,
            "closeup" => $close_up_performer,
            "comedy" => $comedy_performer,
            "escape" => $escape_performer,
            "gambling" => $gambling_performer,
            "illusionist" => $illusionist,
            "juggling" => $juggling_performer,
            "kidsballoons" => $kids_show_and_balloon_performer,
            "mentalism" => $mentalism_performer,
            "stageparlor" => $stage_parlor_performer,
            "religious" => $religious_performer,
            "street" => $street_performer,
            "walkaround" => $walk_around_performer,
            "blacklabel" => $black_label,
            "ltdedition" => $limited_edition

        );
    }

    $obj = (object) [
        "status" => 200,
        "statusText" => "Success",
        "data" => $return_arr
    ];

    // Save data to sessionStorage
    // sessionStorage . setItem('allmagictopresent', json_encode($obj, JSON_PRETTY_PRINT));

    echo json_encode($obj, JSON_PRETTY_PRINT);
} else {
    echo "No items found";
}

The line above:

$data->items->InventoryItem as $item

Would then need to have

$data[n]->items->InventoryItem as $item

to cover for all the SETS of 100 (pages).

The final result will look like this which works for only $pagenbr = 1 (100 records):

Magic Products List

That's pretty much it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source