Danny620 Posted January 12, 2012 Share Posted January 12, 2012 how can i also output $pages number into the json as well so i can use it for jquery <?php // Start Buffer ob_start(); // Load Settings require '../../includes/config.inc.php'; // Require the database connection: require(MYSQL); // Number of records to show per page: $display = 6; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; }else{ // Need to determine. // Default Query $count_q = "SELECT COUNT(id) FROM deals"; // Count the number of records: $r = @mysqli_query ($dbc, $count_q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Make the query: $q = "SELECT id, url, views, deal_img, title, savings, DATE_FORMAT(expiry, '%a, %d %b %Y') AS expr FROM deals ORDER BY created_on DESC LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // Run the query. if (mysqli_num_rows($r) > 0) { // Available. /* create one master array of the records */ $offers = array(); while ($offer = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $offers[] = array("id" => $offer["id"], "url" => BASE_APP_URL.'offer/'.$offer["url"].'/', "views" => $offer["views"], "image" => BASE_URL.'niftyuk-deal/'.$offer["deal_img"], "title" => $offer["title"], "savings" => $offer["savings"], "expr" => $offer["expr"]); } // End of WHILE loop. } /* output in necessary format */ header('Content-type: application/json'); echo json_encode($offers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254911-json/ Share on other sites More sharing options...
Danny620 Posted January 12, 2012 Author Share Posted January 12, 2012 i dont want to include it in the loop just something like this [ { "pages": "2" }, { "id": "34", "url": "http://www.niftyuk.com/app/offer/bella-sante-beauty-salon-chadderton/", "views": "62", "image": "http://www.niftyuk.com/niftyuk-deal/1326314958.jpg", "title": "Bella Sante Of Chadderton Offer 60% Off Ulimited Flabelos....", "savings": "60", "expr": "Mon, 06 Feb 2012" }, Quote Link to comment https://forums.phpfreaks.com/topic/254911-json/#findComment-1307054 Share on other sites More sharing options...
requinix Posted January 12, 2012 Share Posted January 12, 2012 $offers is an array of offers. $object = array( "pages" => 2, "offers" => $offers ); JSON-encode that. Quote Link to comment https://forums.phpfreaks.com/topic/254911-json/#findComment-1307058 Share on other sites More sharing options...
Danny620 Posted January 12, 2012 Author Share Posted January 12, 2012 hi i cant seem to acess the json now using jquery $.ajax({ url: 'http://www.niftyuk.com/app/api/offers-json.php?s=' + start+'&p=' + currentPage+'', dataType: 'json', timeout: 5000, success: function(data, status){ output.empty(); //alert(data.pages); $.each(data, function(i,offer){ var offer = '<div class="post" id="' + offer.id + '"><a href="' + offer.url + '"><img src="' + offer.image + '" alt="" width="124" height="104" class="image"/></a>' + '<div class="blogtitle-div"><h2 class="blogtitle"><a href="' + offer.url + '">' + offer.title + '</a></h2></div>' + '<a href="' + offer.url + '" class="simplebutton">See Offer</a>' + '<div class="clear"></div>' + '<div class="foot"><div class="date">Nifty Savings: ' + offer.savings + '%</div><div class="yecomments"><span>Expires: ' + offer.expr + '</span></div><div class="clear"></div></div>' + '</div>'; output.append(offer); this is json { "pages": 2, "offers": [ { "id": "34", "url": "http://www.niftyuk.com/app/offer/bella-sante-beauty-salon-chadderton/", "views": "62", "image": "http://www.niftyuk.com/niftyuk-deal/1326314958.jpg", "title": "Bella Sante Of Chadderton Offer 60% Off Ulimited Flabelos....", "savings": "60", "expr": "Mon, 06 Feb 2012" }, { "id": "33", "url": "http://www.niftyuk.com/app/offer/teeth-whitening-manchester-stockport/", "views": "51", "image": "http://www.niftyuk.com/niftyuk-deal/1326217288.jpg", "title": "Teeth Whitening Manchester Stockport Poynton 80% Off.", "savings": "80", "expr": "Thu, 10 Jan 2013" }, { "id": "32", "url": "http://www.niftyuk.com/app/offer/auto-equipe-car-spares-discount/", "views": "37", "image": "http://www.niftyuk.com/niftyuk-deal/1326127986.jpg", "title": "Auto Equipe Offer 40% OFF Car Spares And Accessories", "savings": "40", "expr": "Wed, 09 Jan 2013" }, { "id": "31", "url": "http://www.niftyuk.com/app/offer/nifty-advertising/", "views": "161", "image": "http://www.niftyuk.com/niftyuk-deal/1325926551.jpg", "title": "Advertise With Nifty For Less Than A £1.00 A Day", "savings": "18", "expr": "Mon, 31 Dec 2012" } Quote Link to comment https://forums.phpfreaks.com/topic/254911-json/#findComment-1307063 Share on other sites More sharing options...
Alex Posted January 12, 2012 Share Posted January 12, 2012 Well now you need to loop over data.offers, not just data. So change: $.each(data, function(i,offer){ to: $.each(data.offers, function(i,offer){ Quote Link to comment https://forums.phpfreaks.com/topic/254911-json/#findComment-1307071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.