Link to home
Start Free TrialLog in
Avatar of Ray Zuchowski
Ray Zuchowski

asked on

PHP Script (Make Page Variable Change and Script Loop)

Here is my current Code.  To be clear this script works perfect from the help of Experts on this forum. The remaining thing I need this script to do is change the page number  in $params.   For Example here is the string

$params = array('p'=>27,'withBreweries'=>'Y');        Where p=27 that represent a page number. There are 1154 pages. Each page has an array of 50 values in it. This script right now will copy everything off of that page. However I need it to copy pages 1 -1154 without having to go into this file and change P=1 to p=2 .  So at the end of this script it loops back and changes that variable to 2,3,4 and so fourth. Id appreciate any help or point me in a direction to get this to work. Thanks



 <?php
include ("Brewerydb.php");
echo "
Step 2 GET beers from Brewery DB";

#API*************************************INFO
$apikey = '';

$bdb = new Pintlabs_Service_Brewerydb($apikey);
$bdb->setFormat('php'); 
$params = array('p'=>27,'withBreweries'=>'Y');
$results = array();

#RUNS************************************API
try {
    
// The first argument to request() is the endpoint you want to call
// 'brewery/BrvKTz', 'beers', etc.
// The third parameter is the HTTP method to use (GET, PUT, POST, or DELETE)
    $results = $bdb->request('beers', $params, 'GET'); // where $params is a keyed array of parameters to send with the API call.
} catch (Exception $e) {
    $results = array('error' => $e->getMessage());
}

#DUMPS API******************************RESULTS
var_dump($results);


#MYSQL CONNECTION***********************INFO
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";

// Create MYSQL connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



#SQL QUERY******************************SETTINGS
foreach($results['data'] as $result){
	
if($result['abv'] == null){
continue;
}
if($result['description'] == null){
continue;
}

  $sql = "INSERT INTO masterbrewlist (BreweryDbId, BeerName,Description,Organic,ABV)
VALUES (
'{$result['id']}',
   '".$conn->real_escape_string ($result['name'])."',
   '".$conn->real_escape_string ($result['description'])."',
   '".$conn->real_escape_string ($result['isOrganic'])."',
   '".$conn->real_escape_string ($result['abv'])."') ON DUPLICATE KEY 
  UPDATE BreweryDbId=BreweryDbId";
   echo 'Processing ' . $result['name'] . '<br />'; 
	  


if ($conn->query($sql) === TRUE) {
echo "New record created successfully <br>";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
}
$conn->close();
?>

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

To start with, would you rather try working towards putting the repeated code into a function, or would you rather have just one really long loop around the whole lot? A function is neater, but a little more effort.
Avatar of Ray Zuchowski
Ray Zuchowski

ASKER

Hey Terry : )

Well function would be nice but it was messing up the API on this line earlier.

 $results = $bdb->request('beers', $params, 'GET'); // where $params is a keyed array of parameters to send with the API call.

I think in the end if its possible to just do a longer loop if its easier, id prefer that.
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It would be worth you fixing up the indentation to help show where loops start and end... some text editors do that automatically, so it usually isn't difficult. You just need to watch for multiline strings that might be actually part of data, in which case indentation can sometimes be added to the data by mistake.
Terry you are coding genious. Thanks brother. Where can I put the pause command so I don't flood there server ?
You mean like a sleep(5) to slow down each iteration of the loop? Just after the try-catch block would be suitable.
sweet thanks dude. You are the man.
Awesome