Jump to content

AJLX

Members
  • Posts

    37
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

AJLX's Achievements

Member

Member (2/5)

0

Reputation

  1. Thanks Jacques, The nested loop was because I didn't know that I could do it your way. I'm not sure how I could run it with a cron job, I'm using it to suck the info out of an API, and store it in my own DB, so that I can then add my own data to it. The script only needs to run once, I'll then work out a way where I can make a refresh script which only imports the information that has changed. Regards, AJLX
  2. Hey Guys, I'm just starting to learn Curl so that I connect to an API. At the moment I have this php code: <?php //******************************************************************************// // // //THIS FILE TAKES A CURRENT JOB AND ADDS IT TO A PAL LABEL PROJECT // // // //******************************************************************************// // ******* TURN ON ERROR REPORTING *****// error_reporting(E_ALL); ini_set('display_errors', '1'); //**** SET THE HEADERS UP FOR AUTHENTICATION *****// $headers = array(); $headers[] = "X-SUBDOMAIN:XXXXXXXXXXX"; // Set the sub domain $headers[] = "X-AUTH-TOKEN:XXXXXXXXXX"; // Set the API Code $headers[] = "application/json"; $total = 50; // this is the max number of pages we will search. with 100 items on a page, and 100 items that means we can return 10,000 items $curl = curl_init(); //**** WE GET ALL OF THE ITEMS CURRENTLY IN THE CURRENT PROJECT AND PUT THEIR UNIQUE ID INTO THE ASSETS ARRAY ***// curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); curl_setopt($curl, CURLOPT_URL, 'https://api.current-rms.com/api/v1/products?page=1&per_page=100'); // allows up to 1 million products curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // Send the request & save response to $resp $resp = curl_exec($curl); // Convert the response to an array $obj = json_decode($resp, true); // The Obj array currently contains every item in the DB and loads of useless info. We need to extract just the ID and the Name! If I print_r the $obj variable it returns this: I have cut the output down a bit, each item in the products array is about 50 lines long, The only information I actually need to recall is the first two lines which is [id] and the [name]. Is there a simple way that I can do this? So far I have tried to do a Foreach loop and create a new array, but this take a very long time (there are about 600 records!) there must be a much simpler way, where I can unset everything that I don't need, or only call the information that I need from the API? Here is the foreach loop that I used: foreach ($obj as $key) { foreach ($key as $id => $value) { if (!empty($value['name'])) { $products[] = array('Name'=>$value['name'], 'ID' => $value['id'] ); } } } **EDIT ** Once I have my array of ID's (which will be around 600 values long) I then need to get a list of serial numbers that are linked to these id's (there could be up to 100 per id) which will leave me with an array that will be huge! So far I have been getting gateway 504 error's, which I'm hoping is just due to the fact that I'm currently pulling out so much more information than I actually need, so I'm trying to make this more efficient and slim lined. I'm fairly new to PHP, and just do it as a hobby, so go easy on me please! Thanks AJLX
  3. Hey guys: I have a database that is full of items. Each item lives in a box, with various accessories. I'm trying to loop through this DB to print out a label for each case that shows the amount of items that are in the case, along with the accessories that belong to it. I currently have a rather convoluted bit of code, that kind of works, but doesn't have the order that I want. My result is currently this: 4 X Generic lighting hanging clamp 4 X Generic lighting hanging clamp 4 X Generic lighting hanging clamp 4 X Generic lighting hanging clamp 4 X Generic lighting hanging clamp 2 X 25kg Safety Bond 2 X 25kg Safety Bond 2 X 25kg Safety Bond 2 X 25kg Safety Bond 2 X 25kg Safety Bond 2 X 16a to Powercon 2 X 16a to Powercon 2 X 16a to Powercon 2 X 16a to Powercon 2 X 16a to Powercon ********** 12 X Generic lighting hanging clamp 6 X 25kg Safety Bond ********** All the information is there, but due to the way that it loops through it's in the wrong order. I'd like it to be grouped so it looks like this so that it is grouped by box: 4 X Generic lighting hanging clamp 2 X 25kg Safety Bond 2 X 16a to Powercon 4 X Generic lighting hanging clamp 2 X 25kg Safety Bond 4 X Generic lighting hanging clamp 2 X 16a to Powercon 4 X Generic lighting hanging clamp 2 X 25kg Safety Bond 2 X 16a to Powercon 4 X Generic lighting hanging clamp 2 X 25kg Safety Bond 2 X 16a to Powercon ********** 12 X Generic lighting hanging clamp 6 X 25kg Safety Bond ********** Here is the code that I'm using. At the moment I'm just concentrating on the if ($item_type == '2') part, but have shown the whole lot so you can see what I'm doing. foreach ($distinct_path as $path) { $sql = "SELECT * FROM current_items WHERE path = '$path'"; $result = $con->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $item_name = $row['item_name']; $item_type = $row['item_type']; $item_qty = $row['qty']; $case_qty = $row['case_qty']; if ($item_type == '1') // THESE ARE MAIN ITEMS { $total_main_qty = $row['qty']; //main item QTY; $main_item_name = $item_name; } // This code will tell us number of full boxes, and number of extra lights left over $amtoffull = $total_main_qty/$case_qty; $amtoffull = floor($amtoffull); // we round down to the nearest whole number $total_left = $total_main_qty - ($case_qty * $amtoffull); if ($item_type == '2') // THESE ARE ACCESSORIES { $asc_qty = $row['qty']; //main item QTY; $asc_qty = $asc_qty/$total_main_qty; // THIS GIVES US THE TOTAL OF ONE Item $asc_qty = $asc_qty * $case_qty; // This is a full case amount //echo $asc_qty ." x ". $item_name ."</p>"; while ($i < $amtoffull) { echo $asc_qty ." X ". $item_name ."</p>"; ++$i; } } $i = 0; } echo "**********"; echo "<p>"; } I think I need to either shift the loops around so they work in a different order, or store everything into an array and then loop through that. Can any one help?
  4. Ah yes! The code I used above with print_r was just to prove that the result was correct. The code I used to actually build the array looked like this: $assets = array(); foreach ($obj as $key) { foreach ($key as $id) { $assets[] = ($id['id']); } } Thanks
  5. Out of interest what would your solution be? This is for my own personal use, you'll be pleased to know I don't do this as a day job. Any opportunity to learn new things is appreciated...
  6. I finally got there! foreach ($obj as $key) { foreach ($key as $id) { print_r($id['id']); } }
  7. Thanks for your reply. I'm still on PHP 5.4, so this hasn't been implimented yet, and upgrading isn't an option.
  8. Hey guys, I have a php array that looks like this: Array ( [opportunity_items] => Array ( [0] => Array ( [id] => 2783 [opportunity_id] => 92 [item_id] => [item_type] => [opportunity_item_type] => 0 [opportunity_item_type_name] => Group [name] => Strobes [transaction_type] => [transaction_type_name] => [accessory_inclusion_type] => [accessory_inclusion_type_name] => [accessory_mode] => The array goes on for ages, but I have only shown the bits I need. I want to create a new array that just contains the [id] key. I can get the first value by doing this: echo $array["opportunity_items"]["0"]["id"]; But I somehow I need to loop through this to get all of the rest of the values. Thanks, Andy
  9. Hello, When I first load the page, for some reason the Session data is never stored. It's only when I manually refresh the page that it gets stored and that the rest of the pages then work as expected...
  10. Hey guys, I'm opening up a new tab, that includes a project_id code. I am opening with a link like this: window.open(\'project_details.php?Project_id='.$row['Project_id'].'\', \'newwindow\', \'width=1000, height=800\') This works fine. In the project_details.php page I have the following bit of code to try and convert the project _id into a session variable. (I open with the Start_sesssion() as normal). $get_id = $_GET['Project_id']; if (isset($get_id)) { $_SESSION['PROJECT'] = $get_id; } The idea is that I can use this project_id to show the correct project when I navigate to other pages. The problem is, that the only way to get the sesion to start is to refresh the page, before I navigate away. Can anyone think why this is happening, and how I can get around it? the very lazy way is simply to force a refresh, but it seems a very messy way to get around this! Thanks!
  11. Hello guys. I have a DB that has a Project Id, Start_date, End_date in it. I want to be able to put an array that gives me the date range, and then have the project id associtated with it. I currently get the date range, by using this function: function getDateRange($startDate, $endDate, $format="Y-m-d") { //Create output variable $datesArray = array(); //Calculate number of days in the range $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1; if($days<0) { return false; } //Populate array of weekdays and counts for($day=0; $day<$total_days; $day++) { $datesArray[] = date($format, strtotime("{$startDate} + {$day} days")); } //Return results array return $datesArray; } $dateRange = array(); $result = mysql_query("SELECT * FROM Project_details WHERE Project_id ='$value'"); while ($row = mysql_fetch_array($result)) { $project_id = ($row['Project_id']); $startDate = date("d-M-y", strtotime($row['Start_date'])); // get start date for current project and format it to look correct $endDate = date("d-M-y", strtotime($row['End_date'])); // get start date for current project and format it to look correct $dateRange[] = getDateRange($startDate,$endDate); } The start This gives me a the date range. I would like to now add the project id to each entry so that the array would look something like this: date => 17.02.14 project_id => 15 date => 18.02.14 project_id => 15 date => 19.02.14 project_id => 15 date => 20.02.14 project_id => 15 date => 21.02.14 project_id => 15 I have had a tweak, but the project number always seems to increment instead.
  12. Hey guys, I'm trying to work out some logic for my hire system. I'm trying to work out how much to charge for an item. At the moment I'm taking the start date, the end date, and putting the range into an array. I'm then counting that array to find out the total amount of time that the hire lasts for. If a hire lasts for a day, then I charge a day rate. If the hire lasts more than 1.5 days, but less than 7, then a week price is charged. If a hire lasts 8 days, then I would charge a week rate, and a day rate. Anymore then It would become a second week. I don't really know where to start with this? I've never been good at maths Any help or tips would be lovely! Thanks!
  13. Thanks guys, Plenty to be getting on with!
  14. Hello Guys, I have a database that is set up something like this: Name Type_a Type_b 3 series Car BMW 1 series Car BMW A class Car Merc C class Car Merc 500 Motorbike XYZ 600 Motorbike XYZ I want to pull this info out of my database and display it like this: Cars: BMW: 1 Series 3 Series Merc A Class Merc C Class Motorbikes: XYZ: 500 600 So far I have this: $result = mysql_query("SELECT * FROM Cars")or die(mysql_error()); while ($row = mysql_fetch_array($result)) { // we get all of the items in this project and put them into a 3 part Array.... $assets= array('Name' => $row['Name], type_a' => $row['Type_A'], 'Type_B' => $row['Type_B']); } print_r($assets); Now this works fine, I end up with a 3 part array containing all of the information I need. The part I am struggling with, is how to get the information back out of the array in a way I can deal with, to produce the above example. Any help? Thanks guys!
  15. Hello Guys, I have a system that generate project numbers. The system is: YEAR - PROJECT NUMBER . Sub ID 2014.1065.02 I want to be able to search my database for the sub ID's belonging to a project. I have tried using wildcards, but they don't seem to do what I want. Could you please tell me where I'm going wrong? $project_id = $_SESSION['PROJECT']; $project_id_trim = substr($project_id, 0,-3); $trim = mysql_real_escape_string($project_id_trim); echo $trim; $result = mysql_query("SELECT * FROM `Project_subhire` WHERE `Subhire_id` LIKE '%$trim%'"); while ($row = mysql_fetch_array($result)) { $project_results = $row['Project_id']; } Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.