Jump to content

Barand

Moderators
  • Content Count

    18,927
  • Joined

  • Last visited

  • Days Won

    350

Everything posted by Barand

  1. Barand

    Create table fields with types

    It's still doable. I'd fetch the results into an array (key = category) then loop throught array to output $data = [ 'starter' => ['Soup', 'Pate'], 'main' => ['Cod', 'Steak', 'Quail'], 'dessert' => ['Kippers & custard', 'Sago pudding'] ]; then foreach data as course => items output course foreach items as i output i end end
  2. Barand

    Create table fields with types

    I'd move the form and table opening tags above the while() loop then close them after the loop (at present you don't close the form)
  3. Barand

    maybe I'm searching incorrectly?

    Insert customer record Get its ID using last_insert_id() Insert contactinfo with that id as a foreign key.
  4. Barand

    Create table fields with types

    Have you checked the first item in your menu is there?
  5. Barand

    Create table fields with types

    Why would you want to do that? Now, if you want "Entree" to appear on the menu instead of "Starter" you now have to change it in every starter item. With my normalised model you change it in on place only. Database tables are not spreadsheets. Closing the statement before fetching the records from it probably isn't your best idea either. Having read the first record to get the menu name and price you need to use a do { ... } while() loop instead of a while loop. Otherwise you lose item and category fro the first record.
  6. Barand

    Extract Data From JSON Using PHP

    Examples here
  7. Barand

    Mysqli - Can You Not Do These Short-Cuts ?

    It isn't a ternary in the second example, it's php7's new coalesce operator. BTW you could $amount = trim( $_GET['amount'] ?? 0 );
  8. Try reading your own code. Go through it line by line and check the value of each variable after each line executes. Does it look right to you? (you should end up with 0, 1, 5) vales after executing line ----------------------------- wallet balance amount 3 3 5 if($wallet + $balance >= $amount) { 3 3 5 $wallet = (string)($wallet - $amount); -2 3 5 //}elseif $wallet = 0; { $balance = ($balance - $amount); -2 -2 5
  9. Barand

    Mysqli - Can You Not Do These Short-Cuts ?

    What about $amount = (isset($_GET['amount'])) ? $_GET['amount'] : 0; $balance = $balance - $amount; versus $amount = $_GET['amount'] ?? 0; $balance -= $amount;
  10. try something like this $amount = 5; $wallet = 3; $balance = 3; if ($wallet >= $amount) { $wallet -= $amount; } elseif ($wallet + $balance >= $amount) { $amount_remain = $amount - $wallet; $wallet = 0; $balance -= $amount_remain; } else { echo "Insufficient funds<br>"; } echo "W: $wallet<br>B: $balance";
  11. Barand

    Create table fields with types

    FYI - Proof of Concept; DATA menu table menu_dish table dish_type table dish table +---------+-------------+ +---------+---------+ +--------------+-------------+ +---------+-----------------------------+--------------+ | menu_id | description | | menu_id | dish_id | | dish_type_id | description | | dish_id | name | dish_type_id | +---------+-------------+ +---------+---------+ +--------------+-------------+ +---------+-----------------------------+--------------+ | 1 | Lunch Menu | | 1 | 1 | | 1 | Starter | | 1 | Tomato and coriander soup | 1 | | 2 | Dinner Menu | | 1 | 3 | | 2 | Main | | 2 | Trout with almonds | 1 | +---------+-------------+ | 1 | 5 | | 3 | Dessert | | 3 | Avocado on Toast | 1 | | 1 | 7 | +--------------+-------------+ | 4 | Smoked Salmon | 1 | | 1 | 9 | | 5 | Chicken liver pate | 1 | | 1 | 11 | | 6 | Tomato and mozzarello salad | 1 | | 1 | 14 | | 7 | Mixed grill | 2 | | 1 | 15 | | 8 | 8oz sirloin steak | 2 | | 2 | 1 | | 9 | Rissoto with wild mushrooms | 2 | | 2 | 2 | | 10 | Cider braised pork | 2 | | 2 | 4 | | 11 | Cod, chips and mushy peas | 2 | | 2 | 5 | | 12 | Quail stuffed with prunes | 2 | | 2 | 6 | | 13 | Wild boar chops | 2 | | 2 | 8 | | 14 | Apple pie | 3 | | 2 | 9 | | 15 | Ice cream | 3 | | 2 | 10 | | 16 | Fruit salad | 3 | | 2 | 13 | | 17 | Rhubarb crumble | 3 | | 2 | 15 | | 18 | Cheese selection | 3 | | 2 | 16 | +---------+-----------------------------+--------------+ | 2 | 17 | | 2 | 18 | +---------+---------+ QUERY select m.description as Menu , dt.description as Course , GROUP_CONCAT(d.name SEPARATOR ', ') as Choices FROM menu_dish md JOIN menu m USING (menu_id) JOIN dish d USING (dish_id) JOIN dish_type dt USING (dish_type_id) GROUP BY md.menu_id, d.dish_type_id; RESULT +-------------+---------+---------------------------------------------------------------------------------------------------------------+ | Menu | Course | Choices | +-------------+---------+---------------------------------------------------------------------------------------------------------------+ | Lunch Menu | Starter | Tomato and coriander soup, Chicken liver pate, Avocado on Toast | | Lunch Menu | Main | Cod, chips and mushy peas, Rissoto with wild mushrooms, Mixed grill | | Lunch Menu | Dessert | Ice cream, Apple pie | | Dinner Menu | Starter | Smoked Salmon, Tomato and coriander soup, Tomato and mozzarello salad, Chicken liver pate, Trout with almonds | | Dinner Menu | Main | Wild boar chops, 8oz sirloin steak, Cider braised pork, Rissoto with wild mushrooms | | Dinner Menu | Dessert | Rhubarb crumble, Cheese selection, Fruit salad, Ice cream | +-------------+---------+---------------------------------------------------------------------------------------------------------------+ (Soup, rissotto and ice-cream on both menus)
  12. Barand

    Create table fields with types

    Then I'd go with something like this +-----------------+ +-------------------+ +-----------------+ | menu | | dish | | dish_type | +-----------------+ +-------------------+ +-----------------+ | menu_id |--+ +-------| dish_id | +------| dish_type_id | | description | | | | name | | | description | +-----------------+ | | | dish_type_id |>----+ +-----------------+ | | +-------------------+ | | | +---------------+ | | | menu_dishes | | | +---------------+ | +----<| menu_id | | | dish_id |>--+ +---------------+
  13. Barand

    Create table fields with types

    Could the same dish appear on more than one menu?
  14. Barand

    Mysqli - Can You Not Do These Short-Cuts ?

    Why don't you RTFM for a change instead of asking us to read it to you?
  15. Barand

    Create table fields with types

    How you store them is going to depend on your menu structure and the relationships between menus and dishes and dishes and other dishes. EG 1 menu with choice of all starters choice of all main dishes choice of all desserts or Menu 1 choice of some starters choice of some main dishes choice of some desserts Menu 2 (Vegan) choice of some starters choice of some main dishes choice of some desserts As you are the only one who knows this you are pretty much on your own. I advise you read up on "Data Normalization"
  16. Barand

    How to make simple if repeat in looping?

    In the absence of a reply, I estimated the radius to be about 3.5m from that photo. This gives a table of volmes and weights based on that radius. Note the absence of if..., if..., if..., if...) <?php const R = 350; // radius of tank cm NOTE: estimated - replace with actual radius const HC = 40; // cone height cm function tank_volume ($h) { $k = M_PI * R**2 ; if ($h <= HC) { $vol = $k * $h / 3 ; } else { $vol = $k * HC / 3 + $k * ($h - HC); } return $vol; } $results = []; foreach (range(1,500) as $h) { $vol = tank_volume($h); $results[] = [ 'ht' => $h, 'vol' => number_format($vol, 0), 'Kg' => number_format($vol/1000, 0) ]; } $chunks = array_chunk($results,100); $output = ''; foreach ($chunks as $ch) { $output .= "<div class='output'> <table><tr><th>Height<?th><th>Volume (cc)</th><th>Kg</th></tr> "; foreach ($ch as $res) { $cls = $res['ht'] < 41 ? "class='cone'" : ''; $output .= "<tr $cls><td>".join('</td><td>', $res)."</td></tr>\n"; } $output .= "</table></div>\n"; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Tank Volumes</title> <meta name="author" content="Barand"> <link rel="shortcut icon" href=""> <meta name="creation-date" content="02/01/2019"> <style type='text/css'> body { font-family: calibri, arial, sans-serif; font-size: 10pt; } #title { height: 50px; font-size: 24px; font-weight: 600; background-image: linear-gradient(#2DABE1, #FFFFFF); color: #FFF; padding: 15px; } .output { width: 300px; font-size: 20px; float: left; padding: 2px; border-right: 1px solid #BB9A21; } table { width: 100%; font-size: 10pt; } th { background-color: #BB9A21; color: #FFF; padding: 5px; } td { text-align: right; padding: 2px 5px; } .cone { background-color: #EEE; } </style> </head> <body> <div id='title'>Tank Volumes and Weights</div> <?=$output?> </body> </html> Sample output
  17. Barand

    a silly DATE question

    It is easy to see SQL results. Just run the query in your database GUI (PhpMyAdmin, Workbench etc) or run it from the command line EG mysql> SELECT dob -> , fname -> , lname -> , CASE WHEN RIGHT(dob, 5) = RIGHT(CURDATE(), 5) THEN 'Happy Birthday' -> ELSE '' -> END as message -> FROM pupil; +------------+----------+------------+----------------+ | dob | fname | lname | message | +------------+----------+------------+----------------+ | 2001-06-22 | Adam | Simms | | | 2001-03-04 | Allan | Blair | | | 2002-02-01 | Anna | Hamilton | Happy Birthday | | 2001-08-02 | Anne | Bailey | | | 2001-10-04 | Anthony | Bell | | | 2000-12-13 | Caroline | Freeman | |
  18. Barand

    a silly DATE question

    It is SQL. I was under the impression your data was in a DB table. You could test it by substituting '2020-02-29' for CURDATE() while you test
  19. Barand

    How to make simple if repeat in looping?

    What is the 1000 for? What is the radius of the tank to give those results you posted? I am assuming you have something like this (where the cone height (HC) is 40cm | | | | | | |______| | | | cylinder πR^2(h - HC) h > HC (add cylinder) | | | | | | |---R--| h \ / | | \ / | | cone πR^2h/3 h <= HC \ / HC | \/ | |
  20. Barand

    a silly DATE question

    You would do that only if it's not a leap year otherwise you send 2 birthday messages. send message WHERE RIGHT(dob, 5) = RIGHT(CURDATE(), 5) OR (YEAR(CURDATE())%4 <> 0 AND RIGHT(dob, 5) = '02-29' AND RIGHT(CURDATE(), 5) = '03-01')
  21. Barand

    a silly DATE question

    if it helps, here's a function what I wrote several years ago /******************************************** * Derive ordinal suffix for $n * * @param int $n the number * @returns string number with suffix eg 23rd */ function ordSuffix($n) { $str = "$n"; $t = $n > 9 ? substr($str,-2,1) : 0; $u = substr($str,-1); if ($t==1) return $str . 'th'; else switch ($u) { case 1: return $str . 'st'; case 2: return $str . 'nd'; case 3: return $str . 'rd'; default: return $str . 'th'; } } EG echo ordSuffix(61) //--> 61st
  22. Barand

    subtract a month from a date()

    First step is look at the output from phpinfo() That will tell you the location of the ini file that is being used and your current settings
  23. Barand

    How to make simple if repeat in looping?

    As we say here, "There's nought so rare as commom sense"
  24. Barand

    subtract a month from a date()

    Either that or do this echo date('Y-m-d', strtotime('first day of last month')); // 2018-12-01
×

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.