Jump to content

Barand

Moderators
  • Posts

    24,450
  • Joined

  • Last visited

  • Days Won

    809

Community Answers

  1. Barand's post in Trying to add a where clause to my pdo select was marked as the answer   
    Sorry, I forgot to take the "->fetchAll()" from the end. Let's start again
    $stmt = $db->prepare("SELECT * FROM posts WHERE username = ? LIMIT $paginationStart, $limit"); $stmt->execute([$row2['username']]); $authors = $stmt->fetchAll();  
  2. Barand's post in Why I Get This Unnecssary FATAL Error ? was marked as the answer   
    Since you found that this...
    $query->bind_param($CharTypes, ...$SearchValues); works, why are you using
    without the "..." to expand the array into a comma separated list?
  3. Barand's post in how to Run a line of code after 10 minutes of running another line of code in PHP was marked as the answer   
    Here's one way.
    When you send a payment confirmation write a record (forename, surname, email) to "confirmation" table
    CREATE TABLE `confirmation` ( `confirm_id` int(11) NOT NULL AUTO_INCREMENT, `forename` varchar(45) DEFAULT NULL, `surname` varchar(45) DEFAULT NULL, `email` varchar(45) DEFAULT NULL, `time_confirmed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `tips_sent` datetime DEFAULT NULL, PRIMARY KEY (`confirm_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Set up a cron job to run every 5 minutes. This would
    SELECT confirm_id, forename, surname, email FROM confirmation WHERE tips_sent IS NULL AND NOW() - INTERVAL 10 MINUTE > time_confirmed; foreach record returned
    Send welcome tips email UPDATE confirmation SET tips_sent = NOW() WHERE confirm_id = ?
  4. Barand's post in simple string operation not working.. was marked as the answer   
    Looking up strpos() in the manual would have explained exactly what your problem is, and the first example gives you the cure.

  5. Barand's post in there seems to be no difference between CHAR and VARCHAR was marked as the answer   
    Use varchar for columns which can have values of variable lengths (names, address lines, descriptions, comments etc)
    Use char when you have values of fixed length (EG US state abbreviations could be CHAR(2), or if you have a product table that has a 4-character product code then CHAR(4)).
    If in doubt use varchar, but don't go mad and make everything VARCHAR(255) as some do. Keep them as sensible size for the expected content. You don't want people entering a phone number with 255 digits just because your DB allows it.
  6. Barand's post in PHP and wildlife was marked as the answer   
    or
    $ark = array( '3 gerbils', '7 fish', '3 gerbils', '1 cat' ); $received = array( '3 gerbils', '7 fish' ); foreach($ark as $j => $expected) { echo $expected; foreach($received as $k => $line) { if($expected == $line) { echo " ARRIVED"; unset ($received[$k], $ark[$j]); // cross them off the lists } } echo "<hr>"; } giving...

     
  7. Barand's post in numbers and math driven websites was marked as the answer   
    What about the Dewey Decimal System used by libraries?
  8. Barand's post in CRUD read. An overview of all the members was marked as the answer   
    If you are happy to display the multiple numbers and emails as, say, comma-separated lists, then you can use GROUP_CONCAT on those columns and GROUP BY Lidnummer.
    SELECT lid.Lidnummer , lid.Naam , lid.Voornaam , lid.Huisnummer , lid.Postcode , postcode.Adres , postcode.Woonplaats , GROUP_CONCAT(telefoonnummers.Telefoonnummer SEPARATOR ', ') as Telefoonnummer , GROUP_CONCAT(email.Emailadres SEPARATOR ', ') as Emailadres FROM lid INNER JOIN postcode ON lid.Postcode = postcode.Postcode INNER JOIN telefoonnummers lid.Lidnummer = telefoonnummers.Lidnummer INNER JOIN email ON lid.Lidnummer = email.Lidnummer GROUP BY lid.Lidnummer; Use explicit joins and not "FROM A, B, C WHERE..."
    When posting code in the forum, use the <> button to create a code block and specify the code type.
  9. Barand's post in Syntax issues, still a newbie but getting there! was marked as the answer   
    HTML code wil not be output when it is inside <?php .. ?> tags. It need to be output using echo command.
    <?php if ($ticket['status'] == 'Fermé') { echo "<div class=\"btns\"> <a href='viewtest.php?id={$_GET['id']}&status=Réouvert' class='btn blue' style='width:400px'>Réouvrir ce billet</a> </div> "; echo 'billet fermé'; } ?> The alternative is exit php mode, output the html then re-enter php mode, which gets very messy...
    <?php if ($ticket['status'] == 'Fermé') { echo "<div class=\"btns\">"; // I would need this part to echo the button correctly if above status is Fermé ?> <a href="viewtest.php?id=<?=$_GET['id']?>&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a> <?php //End of part echo "</div>"; echo 'billet fermé'; } ?>  
  10. Barand's post in I can't push my record to mysql database was marked as the answer   
    Remove the single quotes from around the tablename and column name identifiers. Quotes are for string literals.
    $sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')"; ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ REMOVE Also, you should use a prepared statement instead of putting user-provided variables into your query.
  11. Barand's post in I just have a question about a database table. was marked as the answer   
    It won't let you INSERT a new record with the same primary key as an existing record. The only way you can overwrite them is if you UPDATE them (or INSERT with an ON DUPLICATE KEY UPDATE option)
  12. Barand's post in PHP Form just errors every time - Can't figure out why! was marked as the answer   
    For $_POST['submit'] to exist there must be an input form element with the name 'submit'.
    A better way to check is
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { // data was posted - process it }  
  13. Barand's post in Sum and join 2 tables was marked as the answer   
    Your query joins each record from 'a' to many records from 'b' thus multiplying your totals.
    Try
    SELECT a.prod_id, a.prod_name , a.qty_received , b.qty_bot , a.qty_received - b.qty_bot as qty_remain FROM ( SELECT prod_id , prod_name , sum(prod_qty) as qty_received FROM tbl_distribution WHERE staff_id = 2962 GROUP BY prod_id ) a LEFT JOIN ( SELECT prod_id , sum(qty_bought) as qty_bot FROM tbl_sales_bar WHERE staff_id = 2962 GROUP BY prod_id ) b USING (prod_id);
    Your tables are in dire need of normalization. For example, "prod_name" should occur once in your database (in a product table) and not be repeated in several tables.
    PS
    In future, don't post pictures of your data, post the data. It was not a great deal of fun building the test tables from those images so that I could test your  query.
  14. Barand's post in Find no.of position in column was marked as the answer   
    What do get if you run the subquery on its own?
    SELECT GROUP_CONCAT(DISTINCT Average ORDER BY Average DESC ) FROM mark; I suspect that your "average" column is varchar instead of a numeric type. (in my table it's DECIMAL(8,2) so that it sorts correctly.
  15. Barand's post in Can't find error in php mysql jquery was marked as the answer   
    Use your browser's developer tools network tab to check what is being sent and received by the ajax request
  16. Barand's post in Matrix combination was marked as the answer   
    Does this look right?
    A1 A2 A3 A4 A5 A1 B2 A3 A4 A5 A1 C2 A3 A4 A5 B1 A2 A3 A4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 B4 C5 A1 C2 C3 B4 C5 B1 A2 C3 B4 C5 B1 B2 C3 B4 C5 B1 C2 C3 B4 C5 C1 A2 C3 B4 C5 C1 B2 C3 B4 C5 C1 C2 C3 B4 C5 A1 A2 C3 C4 A5 A1 B2 C3 C4 A5 A1 C2 C3 C4 A5 B1 A2 C3 C4 A5 B1 B2 C3 C4 A5 B1 C2 C3 C4 A5 C1 A2 C3 C4 A5 C1 B2 C3 C4 A5 C1 C2 C3 C4 A5 A1 A2 C3 C4 B5 A1 B2 C3 C4 B5 A1 C2 C3 C4 B5 B1 A2 C3 C4 B5 B1 B2 C3 C4 B5 B1 C2 C3 C4 B5 C1 A2 C3 C4 B5 C1 B2 C3 C4 B5 C1 C2 C3 C4 B5 A1 A2 C3 C4 C5 A1 B2 C3 C4 C5 A1 C2 C3 C4 C5 B1 A2 C3 C4 C5 B1 B2 C3 C4 C5 B1 C2 C3 C4 C5 C1 A2 C3 C4 C5 C1 B2 C3 C4 C5 C1 C2 C3 C4 C5  
  17. Barand's post in PHP script to export for Facebook data feed CSV was marked as the answer   
    These are the differences I can see
    Your "after" file has a comma at the end of line 1, the "before" one doesn't.

      Your "after" file has a newline character at the end of the final line (extra blank line at end of file); "before" doesn't.

      "After" prices not rounded to 2 dec; "before" ones are. (123.50 becomes 123.5 etc)
  18. Barand's post in error_reporting: hide the warnings was marked as the answer   
    The point is that that your code to set the error reporting level is not being executed.
    When you run a PHP script is is first parsed for errors (startup errors). If none are found the script is then executed (and your error reporting code will be executed) but, as in this case, if errors are found your code is not executed.
    This was pointed out to you by requinix earlier...
     
  19. Barand's post in What does the x and y value mean here? Can you show in a figure? was marked as the answer   
    The closest x or y can be to the edge is the radius of the ball. When either of them reach that position, change direction.

  20. Barand's post in Insert into .. select was marked as the answer   
    That will only select one record (g1/s1 or g1/s2) so you will only insert one new record
  21. Barand's post in school database was marked as the answer   
    The SQL tutorial link in my signature may help to get you started.
  22. Barand's post in Converting time from format in database was marked as the answer   
    You can do it in your query with
    SELECT date_format(created_at, '%b %e %Y %h:%i%p'), ... or you can do it in php with
    date('M j Y g:ia', strtotime($row['created_at']))  
  23. Barand's post in round to 1 decimal was marked as the answer   
    There are a couple of solutions that you can apply on the MySql side.
    1 ) Use mysql's ROUND() function i your query
    mysql> create temporary table test1 (amount float); mysql> insert into test1 (amount) Values (PI()); mysql> select amount, round(amount, 1) as rounded from test1; +---------+---------+ | amount | rounded | +---------+---------+ | 3.14159 | 3.1 | +---------+---------+ 1 row in set (0.03 sec) 2 ) Define the column as DECIMAL instead of FLOAT
    mysql> create temporary table test2 (amount DECIMAL(8,1)); mysql> insert into test2 (amount) Values (PI()); mysql> select amount from test2; +--------+ | amount | +--------+ | 3.1 | +--------+ 1 row in set (0.00 sec)  
  24. Barand's post in JSON Api results, loop through array and extract records to html table was marked as the answer   
    It's been  a particularly miserable , grey, rainy sort of a day so I passed some time on this
    <?php $json = '{"meta":{"count":5,"links":{"self":"https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=CPT&destinationLocationCode=LGW&departureDate=2023-03-01&returnDate=2023-03-08&adults=1&travelClass=ECONOMY¤cyCode=ZAR&max=5"}},"data":[{"type":"flight-offer","id":"1","source":"GDS","instantTicketingRequired":false,"nonHomogeneous":false,"oneWay":false,"lastTicketingDate":"2023-03-01","numberOfBookableSeats":7,"itineraries":[{"duration":"PT18H50M","segments":[{"departure":{"iataCode":"CPT","at":"2023-03-01T12:55:00"},"arrival":{"iataCode":"DOH","at":"2023-03-01T23:40:00"},"carrierCode":"QR","number":"1372","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT9H45M","id":"5","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-02T01:15:00"},"arrival":{"iataCode":"LGW","terminal":"S","at":"2023-03-02T05:45:00"},"carrierCode":"QR","number":"5943","aircraft":{"code":"777"},"operating":{"carrierCode":"BA"},"duration":"PT7H30M","id":"6","numberOfStops":0,"blacklistedInEU":false}]},{"duration":"PT19H10M","segments":[{"departure":{"iataCode":"LGW","terminal":"S","at":"2023-03-08T14:05:00"},"arrival":{"iataCode":"DOH","at":"2023-03-08T23:45:00"},"carrierCode":"QR","number":"5942","aircraft":{"code":"777"},"operating":{"carrierCode":"BA"},"duration":"PT6H40M","id":"7","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-09T02:15:00"},"arrival":{"iataCode":"CPT","at":"2023-03-09T11:15:00"},"carrierCode":"QR","number":"1369","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT10H","id":"8","numberOfStops":0,"blacklistedInEU":false}]}],"price":{"currency":"ZAR","total":"12632.45","base":"6370.00","fees":[{"amount":"0.00","type":"SUPPLIER"},{"amount":"0.00","type":"TICKETING"}],"grandTotal":"12632.45"},"pricingOptions":{"fareType":["PUBLISHED"],"includedCheckedBagsOnly":true},"validatingAirlineCodes":["QR"],"travelerPricings":[{"travelerId":"1","fareOption":"STANDARD","travelerType":"ADULT","price":{"currency":"ZAR","total":"12632.45","base":"6370.00"},"fareDetailsBySegment":[{"segmentId":"5","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"6","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"7","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"8","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}}]}]},{"type":"flight-offer","id":"2","source":"GDS","instantTicketingRequired":false,"nonHomogeneous":false,"oneWay":false,"lastTicketingDate":"2023-03-01","numberOfBookableSeats":7,"itineraries":[{"duration":"PT19H15M","segments":[{"departure":{"iataCode":"CPT","at":"2023-03-01T19:10:00"},"arrival":{"iataCode":"DOH","at":"2023-03-02T05:55:00"},"carrierCode":"QR","number":"1370","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT9H45M","id":"1","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-02T08:05:00"},"arrival":{"iataCode":"LGW","terminal":"N","at":"2023-03-02T12:25:00"},"carrierCode":"QR","number":"327","aircraft":{"code":"788"},"operating":{"carrierCode":"QR"},"duration":"PT7H20M","id":"2","numberOfStops":0,"blacklistedInEU":false}]},{"duration":"PT19H10M","segments":[{"departure":{"iataCode":"LGW","terminal":"S","at":"2023-03-08T14:05:00"},"arrival":{"iataCode":"DOH","at":"2023-03-08T23:45:00"},"carrierCode":"QR","number":"5942","aircraft":{"code":"777"},"operating":{"carrierCode":"BA"},"duration":"PT6H40M","id":"7","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-09T02:15:00"},"arrival":{"iataCode":"CPT","at":"2023-03-09T11:15:00"},"carrierCode":"QR","number":"1369","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT10H","id":"8","numberOfStops":0,"blacklistedInEU":false}]}],"price":{"currency":"ZAR","total":"12632.45","base":"6370.00","fees":[{"amount":"0.00","type":"SUPPLIER"},{"amount":"0.00","type":"TICKETING"}],"grandTotal":"12632.45"},"pricingOptions":{"fareType":["PUBLISHED"],"includedCheckedBagsOnly":true},"validatingAirlineCodes":["QR"],"travelerPricings":[{"travelerId":"1","fareOption":"STANDARD","travelerType":"ADULT","price":{"currency":"ZAR","total":"12632.45","base":"6370.00"},"fareDetailsBySegment":[{"segmentId":"1","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"2","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"7","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"8","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}}]}]},{"type":"flight-offer","id":"3","source":"GDS","instantTicketingRequired":false,"nonHomogeneous":false,"oneWay":false,"lastTicketingDate":"2023-03-01","numberOfBookableSeats":7,"itineraries":[{"duration":"PT25H30M","segments":[{"departure":{"iataCode":"CPT","at":"2023-03-01T12:55:00"},"arrival":{"iataCode":"DOH","at":"2023-03-01T23:40:00"},"carrierCode":"QR","number":"1372","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT9H45M","id":"3","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-02T08:05:00"},"arrival":{"iataCode":"LGW","terminal":"N","at":"2023-03-02T12:25:00"},"carrierCode":"QR","number":"327","aircraft":{"code":"788"},"operating":{"carrierCode":"QR"},"duration":"PT7H20M","id":"4","numberOfStops":0,"blacklistedInEU":false}]},{"duration":"PT19H10M","segments":[{"departure":{"iataCode":"LGW","terminal":"S","at":"2023-03-08T14:05:00"},"arrival":{"iataCode":"DOH","at":"2023-03-08T23:45:00"},"carrierCode":"QR","number":"5942","aircraft":{"code":"777"},"operating":{"carrierCode":"BA"},"duration":"PT6H40M","id":"7","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-09T02:15:00"},"arrival":{"iataCode":"CPT","at":"2023-03-09T11:15:00"},"carrierCode":"QR","number":"1369","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT10H","id":"8","numberOfStops":0,"blacklistedInEU":false}]}],"price":{"currency":"ZAR","total":"12632.45","base":"6370.00","fees":[{"amount":"0.00","type":"SUPPLIER"},{"amount":"0.00","type":"TICKETING"}],"grandTotal":"12632.45"},"pricingOptions":{"fareType":["PUBLISHED"],"includedCheckedBagsOnly":true},"validatingAirlineCodes":["QR"],"travelerPricings":[{"travelerId":"1","fareOption":"STANDARD","travelerType":"ADULT","price":{"currency":"ZAR","total":"12632.45","base":"6370.00"},"fareDetailsBySegment":[{"segmentId":"3","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"4","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"7","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"8","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}}]}]},{"type":"flight-offer","id":"4","source":"GDS","instantTicketingRequired":false,"nonHomogeneous":false,"oneWay":false,"lastTicketingDate":"2023-03-01","numberOfBookableSeats":7,"itineraries":[{"duration":"PT18H50M","segments":[{"departure":{"iataCode":"CPT","at":"2023-03-01T12:55:00"},"arrival":{"iataCode":"DOH","at":"2023-03-01T23:40:00"},"carrierCode":"QR","number":"1372","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT9H45M","id":"5","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-02T01:15:00"},"arrival":{"iataCode":"LGW","terminal":"S","at":"2023-03-02T05:45:00"},"carrierCode":"QR","number":"5943","aircraft":{"code":"777"},"operating":{"carrierCode":"BA"},"duration":"PT7H30M","id":"6","numberOfStops":0,"blacklistedInEU":false}]},{"duration":"PT19H10M","segments":[{"departure":{"iataCode":"LGW","terminal":"N","at":"2023-03-08T14:05:00"},"arrival":{"iataCode":"DOH","at":"2023-03-08T23:50:00"},"carrierCode":"QR","number":"328","aircraft":{"code":"788"},"operating":{"carrierCode":"QR"},"duration":"PT6H45M","id":"9","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-09T02:15:00"},"arrival":{"iataCode":"CPT","at":"2023-03-09T11:15:00"},"carrierCode":"QR","number":"1369","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT10H","id":"10","numberOfStops":0,"blacklistedInEU":false}]}],"price":{"currency":"ZAR","total":"12670.45","base":"6370.00","fees":[{"amount":"0.00","type":"SUPPLIER"},{"amount":"0.00","type":"TICKETING"}],"grandTotal":"12670.45"},"pricingOptions":{"fareType":["PUBLISHED"],"includedCheckedBagsOnly":true},"validatingAirlineCodes":["QR"],"travelerPricings":[{"travelerId":"1","fareOption":"STANDARD","travelerType":"ADULT","price":{"currency":"ZAR","total":"12670.45","base":"6370.00"},"fareDetailsBySegment":[{"segmentId":"5","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"6","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"9","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"10","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}}]}]},{"type":"flight-offer","id":"5","source":"GDS","instantTicketingRequired":false,"nonHomogeneous":false,"oneWay":false,"lastTicketingDate":"2023-03-01","numberOfBookableSeats":9,"itineraries":[{"duration":"PT19H15M","segments":[{"departure":{"iataCode":"CPT","at":"2023-03-01T19:10:00"},"arrival":{"iataCode":"DOH","at":"2023-03-02T05:55:00"},"carrierCode":"QR","number":"1370","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT9H45M","id":"1","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-02T08:05:00"},"arrival":{"iataCode":"LGW","terminal":"N","at":"2023-03-02T12:25:00"},"carrierCode":"QR","number":"327","aircraft":{"code":"788"},"operating":{"carrierCode":"QR"},"duration":"PT7H20M","id":"2","numberOfStops":0,"blacklistedInEU":false}]},{"duration":"PT19H10M","segments":[{"departure":{"iataCode":"LGW","terminal":"N","at":"2023-03-08T14:05:00"},"arrival":{"iataCode":"DOH","at":"2023-03-08T23:50:00"},"carrierCode":"QR","number":"328","aircraft":{"code":"788"},"operating":{"carrierCode":"QR"},"duration":"PT6H45M","id":"9","numberOfStops":0,"blacklistedInEU":false},{"departure":{"iataCode":"DOH","at":"2023-03-09T02:15:00"},"arrival":{"iataCode":"CPT","at":"2023-03-09T11:15:00"},"carrierCode":"QR","number":"1369","aircraft":{"code":"77W"},"operating":{"carrierCode":"QR"},"duration":"PT10H","id":"10","numberOfStops":0,"blacklistedInEU":false}]}],"price":{"currency":"ZAR","total":"12670.45","base":"6370.00","fees":[{"amount":"0.00","type":"SUPPLIER"},{"amount":"0.00","type":"TICKETING"}],"grandTotal":"12670.45"},"pricingOptions":{"fareType":["PUBLISHED"],"includedCheckedBagsOnly":true},"validatingAirlineCodes":["QR"],"travelerPricings":[{"travelerId":"1","fareOption":"STANDARD","travelerType":"ADULT","price":{"currency":"ZAR","total":"12670.45","base":"6370.00"},"fareDetailsBySegment":[{"segmentId":"1","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"2","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"9","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}},{"segmentId":"10","cabin":"ECONOMY","fareBasis":"NLR1R1RX","brandedFare":"ECLASSIC","class":"N","includedCheckedBags":{"quantity":2}}]}]}],"dictionaries":{"locations":{"CPT":{"cityCode":"CPT","countryCode":"ZA"},"DOH":{"cityCode":"DOH","countryCode":"QA"},"LGW":{"cityCode":"LON","countryCode":"GB"}},"aircraft":{"77W":"BOEING 777-300ER","777":"BOEING 777-200/300","788":"BOEING 787-8"},"currencies":{"ZAR":"S.AFRICAN RAND"},"carriers":{"QR":"QATAR AIRWAYS","BA":"BRITISH AIRWAYS"}}}'; $array = json_decode($json, true); $html = ''; foreach ($array['data'] as $offer) { $html .= buildOfferTable($offer); } function buildOfferTable($offer) { $html = "<div class='w3-row w3-padding w3-topbar'> <div class='w3-col w3-quarter'> Offer ID : {$offer['id']} </div> <div class='w3-col w3-quarter'> Source : {$offer['source']} </div> <div class='w3-col w3-half'> <b>Total Price : {$offer['price']['total']} {$offer['price']['currency']}</b> </div> </div> <table class='w3-table w3-bordered'> <tr class='w3-indigo'> <th>Segment</th> <th>Flight No</th> <th>Departure Time</th> <th>Arrival Time</th> </tr> "; foreach ($offer['itineraries']as $itins) { foreach ($itins['segments'] as $segment) { $html .= segmentRow($segment); } } $html .= "</table>\n<br>"; foreach ($offer['travelerPricings'] as $k => $tp) { $n = $k + 1; $html .= "Traveller $n : {$tp['travelerType']}<br> Amount : {$tp['price']['total']} {$tp['price']['currency']}<br><br> "; } return $html; } function segmentRow($segment) { $dt = (new DateTime($segment['departure']['at']))->format('Y-m-d H:i'); $at = (new DateTime($segment['arrival']['at']))->format('Y-m-d H:i'); $row = "<tr> <td>{$segment['departure']['iataCode']}-{$segment['arrival']['iataCode']}</td> <td>{$segment['carrierCode']} {$segment['number']}</td> <td>$dt</td> <td>$at</td> </tr> "; return $row; } ?> <!DOCTYPE=html> <html lang="en"> <head> <meta charset='utf-8'> <title>Flight Offers</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> </head> <body> <h1>Flight Offers</h1> <div class='w3-content'> <?= $html ?> </div> </body> </html> producing 5 offers like this ...

  25. Barand's post in Php variable not accessible was marked as the answer   
    $data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; $characters = json_decode($data, 1); // decode the JSON feed as array echo $characters['location']['name'] . '<br>'; echo $characters['current']['condition']['text'] . '<br>'; echo $characters['current']['condition']['code'] . '<br>'; If you want to stay with objects...
    $data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; $obj = json_decode($data); // decode the JSON feed as object echo $obj->location->name . '<br>'; echo $obj->current->condition->text . '<br>'; echo "<img src='{$obj->current->condition->icon}'>" . '<br>'; echo $obj->current->condition->code . '<br>';
×
×
  • 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.