Jump to content

AJLX

Members
  • Posts

    37
  • Joined

  • Last visited

Posts posted by AJLX

  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:

     

    Array
    (
        [products] => Array
            (
                [0] => Array
                    (
                        [id] => 85
                        [name] => 100m Lighting Multicore (2 x 5pin dmx & 2 x 3pin dmx & 1 x 16a )
                        [type] => Product
                        [tag_list] => Array
                            (
                                [0] => cable
                                [1] => lighting
                                [2] => multicore
                                [3] => checkweight
                            )
    
                        [description] => 
                        [allowed_stock_type] => 1
                        [allowed_stock_type_name] => Rental
                        [stock_method] => 1
                        [stock_method_name] => Bulk
                        [buffer_percent] => 0.0
                        [post_rent_unavailability] => 0
                        [replacement_charge] => 0.0
                        [weight] => 25.0
                        [barcode] => 
                        [active] => 1
                        [accessory_only] => 
                        [system] => 
                        [discountable] => 1
    }
    

     

     

    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. 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

  6. 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!

     

  7. 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.

     

     

     

  8. 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 :confused:

     

    Any help or tips would be lovely!

     

    Thanks!

  9. 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!

  10. 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

  11. Hey Guys,

     

    I want to create a calendar. For this calendar I would like several features.

     

    A) In this calendar I want to be able to add events that last several days. I would like the calendar to show the event as a clickable coloured bar that spans the length of the event.

     

    B) When a new multi day event is added, I want it to appear underneath any other events that might already be occuring on these days.

     

    My project is currently here at: www.zeal.ajlx.co.uk.

     

    I'm not asking for you guys to write all the code for me, but some hints, tips, and ideas would be much appreciated!

     

    Many thanks!

     

    AJLX

  12. Hi there,

    It is slightly more complicated than that! The Calendar table is populated with a loop:

     
    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    $maxday = date("t",$timestamp);
    $thismonth = getdate ($timestamp);
    $startday = $thismonth['wday']-1;
    $new_month = ($monthNames[$cMonth-1]);
    for ($i=0; $i<($maxday+$startday); $i++) { 
        $day = ($i - $startday + 1);
        if(($i % 7) == 0 ) echo "<tr>";
        if($i < $startday) echo "<td></td>";
        
        else echo "<td align='center' valign='middle' height='20px'><a href=",'/index.php' . "?day1=" .$day. "&month1=". $new_month . "&year1=" . "$cYear>". ($i - $startday + 1) . "</a></td>";
        if(($i % 7) == 6 ) echo "</tr>";
    }
    
    

    The numbers that populate the table aren't dates. I guess I need to convert these numbers (and the month) back into a readable date format that I can then do an If statement on? Any ideas on the best way to do this?

     

    Regards,

     

    Andy Jones

  13. Hello Guys,

     

    I'm currently working on this swerv.ajlx.co.uk project. When some one clicks on a date on the calendar it brings up a table to allow bookings.

     

    However on weekends I need to be able to display a different table as the booking times are different.

     

    Can anyone think of a way I might be able to do this? I'm not asking for you to code it for me- but some hints, suggestions and ideas would be lovely! I'm currently working on the coding, and not the aesthetics- so ignore the obvious css issues!

     

    Let me know if you need any code snippets!

     

    login: chicken

    password: goat

     

    Regards,

     

    Andy Jones

  14. If my SQL statement returns a result I want to change the background color of a cell. If the user is logged in I want to then show one of two buttons. either book, or cancel, depending on whether the cell is available or not. So far I have this:

     

    $sql = "SELECT * FROM diary WHERE day = '$mon' AND studio_id ='mon11'";
    $query = mysql_query($sql) or die (mysql_error());
    if (mysql_num_rows($query) < 1)
    { 
    $bgcolour= '#5DFC0A';
    if(isSet($_SESSION['logged']))
    {
    $book = '<form action="../book.php" method="POST"><input type="hidden" name="day" value='.$mon.' type="text"/><input type="hidden" name="month" value='.$month.' type="text"/><input type="hidden" name="year" value='.$year.' type="text"/><input type="hidden" name="book_id" value="mon11" type="text"/><input type="submit" name="submit" value="Book!"></form>';
    }} else {
    $bgcolour= '#FF0000';
    }
    if(isSet($_SESSION['logged']))
    {
    $book = '<form action="../cancel.php" method="POST"><input type="hidden" name="day" value='.$mon.' type="text"/><input type="hidden" name="month" value='.$month.' type="text"/><input type="hidden" name="year" value='.$year.' type="text"/><input type="hidden" name="book_id" value="mon11" type="text"/><input type="submit" name="submit" value="Cancel!"></form>';
    }
    echo "<td rowspan='3' bgcolor=".$bgcolour.">";
    if (isset($book)) 
    {
        echo $book;
    }
    

     

    I want to use the $session(logged) variable to show the appropriate button, but I can't get it to work in the other if statement,

     

    Any ideas?

  15. replace Line 55 through 70 with this:

        else 
        {
    echo "<h1>Log In Successful!</h1>
    <form action='amendfile2.php' method='post'>
    <input type='submit' value='Amend to file'>
    </form>
    <form action='viewfile3.php' method='post'>
    <input type='submit' value='View Web Blog'>
    </form>
    <form action='loginform.php' method='post'>
    <input type='submit' value='Click here to go to the Log In Screen'>
    </form>";
    
            }
    	//Here, the PHP script is being ended
    ?>
    
    
    

    Any better?

     

  16. Hi guys,

    Sorry I can't seem to find an edit button? I have made a start on building a table to do what I want: www.swerv.ajlx.co.uk

    My current issue is trying to make the previous and next buttons work for the year, month, and week.

    I am using this as the code for the '>' year button.

     

    echo "<a href='{$_SERVER['PHP_SELF']}?nextyear=nextyear'> > </a>";

     

    I am then checking the url, and then trying to implement this code:

                    if($_GET['nextyear']=="nextyear")
    	$year = strtotime(date("Y", strtotime($date)) . " +1 year");
    	else($year = date('Y'));
    

     

    Can any one see what is obviously wrong with the above code? presumably once this bit is done it will be easy to copy it for months, and then, with a bit of tweeking the weeks.

     

    Regards

     

    Andy Jones

  17. Hello Guys,

    My goal is to make a Php and SQL calendar. I started learning php and sql a while ago, and basically got distracted. I'm now back, with a new project in the unhelpful position of having forgotten most of what I had learnt!

     

    I am making a project where I want to make a booking system. I have 3 rooms that are available to be booked Room 1, Room 2 and Room 3. I want to make a table that displays the current year in the top row, with the option to be able to click on a '<' or a '>' to change years. I want the same option for months and weeks. I think I am happy with how I would do this. The next bit is more tricky. I want a column for each of the days of the week. directly under this I want to display the correct date, under the correct day, starting with the current week we are on. for example it might look like this:

     

                  -------------------------------------------

                  |            <    2012      >                    |

                  -------------------------------------------

                  |            <      March    >                  |

                  -------------------------------------------

                  |            <      2    >                          |

                  -------------------------------------------

                  |mon | tues | wed| thur | fri  | sat | sun |

                  | 5th  |  6th |  7th|  8th |9th |10th| 11th |

                  ---------------------------------------------

    Room 1  | yes    |        |      |      |      |    |        |

                  ----------------------------------------------

    Room 2  |        |        | yes  |      |      |    |        |

                  ----------------------------------------------

    Room 3  |        |        |      |      |yes  |    |        |

                  ----------------------------------------------

     

    Although at the moment I'm struggling with the whole flippin' thing, the main issue is getting the correct dates to line up under the correct days of the week. I'm not asking for you to do this for me- but a bit of gentle help in this, and maybe some general advice in what I'm trying to achieve would be really useful!

     

    Regards,

     

    AJLX

  18. Perfect I had a couple of html issues.

    1) I had post[] instead of post. In my mind it made sense to be posting an array.

    2) I had $rows[iD] instead of $row[iD]

     

    All working now. Special thanks to ChemicalBliss.

     

    Regards,

     

    AJLX

×
×
  • 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.