Jump to content

Barand

Moderators
  • Content Count

    18,927
  • Joined

  • Last visited

  • Days Won

    350

Posts posted by Barand


  1. 1. You could have a 2 day booking where the price on the first day is different from the price on the second (check in 2019-02-22, check out 2019-02-24) so you cannot just multiply the price by the number of days, you need to get price for each day and SUM() them.

    2. You get two records for type 1 rooms because two price records are being matched.

    So , match each day that is booked for a booking against the price table to get price for each day, SUM the prices and GROUP BY room_id

    I think your booking table is like

    +---------------+--------+---------------+----------------+
    | booking_id    |  room  |  arrive       |  depart        |
    +---------------+--------+---------------+----------------+
    |  101          |  1     | 2019-02-22    | 2019-02-24     |  (difficult to find prices for intervening days)
    +---------------+--------+---------------+----------------+

    I prefer

    booking
    +---------------+--------+---------------+----------------+
    | booking_id    | client |  arrive       |  depart        |
    +---------------+--------+---------------+----------------+
    |  101          |  123   | 2019-02-22    | 2019-02-24     |
    +---------------+--------+---------------+----------------+
    
    room_booking
    +--------+---------------+---------------+
    |Room_id |  occupied     | booking_id    |
    +--------+---------------+---------------+
    |   1    | 2019-02-22    |  101          |     (match against price where occupied between the price dates)
    |   1    | 2019-02-23    |  101          |
    +--------+---------------+---------------+

     


  2. fyi - my solution

    <?php
    
    // generate the array
    $data = [];
    for ($i=0; $i<100; $i++) $data[] = mt_rand(1, 99);
    
    // output array into table, sort, and output again
    $t1 = array2table($data);
    sort($data);
    $t2 = array2table($data);
    
    function array2table(array $arr) 
    {
        $result = "<table border='1'>\n";
        $rows = array_chunk($arr, 10);                            // split the array into rows of 10 numbers each
        foreach ($rows as $cols) {                                // process each row
            $result .= '<tr>';
            foreach ($cols as $c) {                               // process each column in the current row
                $cls = $c % 2 ? 'odd':'even';                     // apply the appropriate class
                $result .= "<td class='$cls'>$c</td>";
            }
            $result .= "</tr>\n";
        }
        $result .= "</table>\n";                                  // return the table
        return $result;
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Example</title>
    <style type='text/css'>
        table {
            border-collapse: collapse;
            margin: 20px;
            display: inline-block;
            font-family: calibri, sans-serif;
            font-size: 12pt;
        }
        td {
            width: 20px;
            height:20px;
            padding: 2px;
            text-align: center;
        }
        .odd {
            background-color: #FF8080;
        }
        .even {
            background-color: #C0FFC0;
        }
    </style>
    </head>
    <body>
        <?=$t1?> 
        <?=$t2?>
    </body>
    </html>

     

    Capture.PNG


  3. Create a function which outputs data from an array into a table (You do it with the first table so you have some idea how to do it)

    • Generate random array
    • call function to output it
    • sort the array
    • call same function to output it

    Your html markup is obsolete. I see you have a css file - use it.


  4. 6 minutes ago, phreak3r said:

    And why is a comma used instead of a period in reference to the echo "<pre>",print_r($_POST, true),"</pre>"; line?

    Unlike print() (which allows only a single argument and you would have to use "." to concatenate with that function) echo() will accept several arguments separated by commas. Either will do.


  5. try

    form code

    echo "<table>";
    for ($i=1; $i<=5; $i++) {
        echo <<<FORM
            <tr>
              <td style="width:70%;">Item $i</td>
              <td class="text-center">
                <input type="hidden" name="equipmentId[$i]" value="$i">
                <input name="equipmentQty[$i]" class="eqQty text-center up" type="text">
              </td>
            </tr>
    FORM;
    }
    echo "</table>";

    processing code

    if ($_SERVER['REQUEST_METHOD']=='POST') {
        $jobId = $_SESSION['current_job_id'];
        // prepare insert query
        $insert = $db->prepare("INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES (?,?,?) ");
        
        foreach ($_POST['equipmentId'] as $k => $eid) {
            if ($_POST['equipmentQty'][$k] > 0) {
                $data = [ $jobId, $eid, $_POST['equipmentQty'][$k] ] ;
                $insert->execute($data);
            }
        } 
    }

     


  6. Here's an optional version - less than 60% (red) over 80% (green)

    function bar($wid, $ht, $val, $max=100)
    {
        $bar = "<svg width='$wid' height='$ht' viewBox='0 0 $wid $ht'>\n
                <defs>
                <linearGradient id='pchi' x1='0' y1='0' x2='0' y2='1'>
                    <stop offset='0%' stop-color='#54BC54'/>
                    <stop offset='15%' stop-color='#eee'/>
                    <stop offset='100%' stop-color='#0B7604'/>
                    </lineargradient>
                <linearGradient id='pclo' x1='0' y1='0' x2='0' y2='1'>
                    <stop offset='0%' stop-color='#E02222'/>
                    <stop offset='15%' stop-color='#eee'/>
                    <stop offset='100%' stop-color='#A91723'/>  
                    </lineargradient>
                <linearGradient id='pcmid' x1='0' y1='0' x2='0' y2='1'>
                    <stop offset='0%' stop-color='#F2D335'/>
                    <stop offset='15%' stop-color='#eee'/>
                    <stop offset='100%' stop-color='#EC9807'/>
                    </lineargradient>
                </defs>
                ";
        $pix = $wid/$max;
        if ($val > 100) $val = 100;
        // percentage labels
        for ($p=25; $p<=75; $p+=25) {
            $ty=8;
            $tx = $p * $pix;
            $bar .= "<path d='M $tx $ty l 0 4' stroke='#AAA' />
                <text x='$tx' y='$ty' class='pcent' >{$p}%</text>\n";
        }
        // draw bar
        $w = $val * $pix;
        $h = $ht-12;
        
        if ($val > 80) $barfill = 'url(#pchi)';
        elseif ($val > 60) $barfill = 'url(#pcmid)';
        else $barfill = 'url(#pclo)';
        
        $bar .= "<rect x='0' y='12' width='$wid' height='$h' stroke='#444' fill='#DDD' />\n";
        $bar .= "<rect x='0' y='12' width='$w' height='$h' fill='$barfill' />\n";
        $bar .= "</svg>\n";
        return $bar;
    }

     

    Capture.PNG


  7. here's the code that produced the example

    <?php
    $res = $db->prepare("SELECT s.subject
                             , r.pcent
                        FROM pupil p 
                             JOIN result r USING (pupilid)
                             JOIN subject s USING (subjectid)
                        WHERE p.lname = ?
                              AND r.schoolyear = 2014
                        ORDER BY subject;
                        ");
    $res->execute( [ 'Wilson'] );
    $tdata = '';
    foreach ($res as $r)  {
        $tdata .= "<tr><td>{$r['subject']}</td>
                      <td>{$r['pcent']}</td>
                      <td>" . bar (400, 30, $r['pcent']) . "</td>
                  </tr>";
    }
    
    /**
    * bar chart
    * 
    * @param int $wid
    * @param int $ht
    * @param mixed $val
    * @param int $max       optional default 100
    */
    function bar($wid, $ht, $val, $max=100)
    {
        $bar = "<svg width='$wid' height='$ht' viewBox='0 0 $wid $ht'>\n";
        $pix = $wid/$max;
        if ($val > 100) $val = 100;
        // percentage labels
        for ($p=25; $p<=75; $p+=25) {
            $ty=10;
            $tx = $p * $pix;
            $bar .= "<path d='M $tx $ty l 0 2' stroke='#AAA' />
                <text x='$tx' y='$ty' class='pcent' >{$p}%</text>\n";
        }
        // draw bar    
        $w = $val * $pix;
        $h = $ht-12;
        $bar .= "<rect x='0' y='12' width='$wid' height='$h' stroke='#444' fill='#EEE' />\n";
        $bar .= "<rect x='0' y='12' width='$w' height='$h' fill='#F48A11' />\n";
        $bar .= "</svg>\n";
        return $bar;
    }
    
    
    ?>
    <!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>Sample</title>
    <meta name="creation-date" content="02/15/2019">
    <style type="text/css">
        table {
            width: 500px;
            margin: 10px auto;
        }
        .pcent {
            font-family: calibri, sans-serif;
            font-size: 8pt;
            fill: #AAA;
        }
    </style>
    </head>
    <body>
       <table>
           <tr><th>Subject</th><th>Pcent</th><th>Chart</th></tr>
           <?=$tdata?>
       </table>
    </body>
    </html>

     


  8. From the calculations in your code I am guessing you are trying to produce something like below using a weird method involving dynamic CSS (different data, I know, but similar scenario) .

    Am I right?

    As ginerjm pointed out, a single query should be used, not queries inside a loop. The output below used this data

    pupil                                 result                                                      subject
    +---------+--------+------------+     +---------+-----------+-------+------------+----------+     +-----------+-----------+
    | pupilID | fname  | lname      |     | pupilid | subjectid | pcent | schoolyear | resultid |     | subjectID | subject   |
    +---------+--------+------------+     +---------+-----------+-------+------------+----------+     +-----------+-----------+
    |       8 | Emma   | Watson     |     |       8 |         1 |    91 |       2014 |        4 |     |         1 | English   |
    |       9 | George | Wilson     |     |       9 |         1 |    62 |*      2014 |        5 |     |         2 | German    |
    |      15 | Jack   | Williams   |     |      14 |         1 |    64 |       2014 |        8 |     |         3 | Geography |
    |      19 | Mary   | Whitehouse |     |      15 |         1 |    93 |       2014 |        9 |     |         4 | Maths     |
    +---------+--------+------------+     |      19 |         1 |    60 |       2014 |       11 |     |         5 | Economics |
                                          |       9 |         5 |    50 |*      2014 |       41 |     |         6 | Biology   |
                                          |      14 |         5 |    51 |       2014 |       43 |     |         7 | Chemistry |
                                          |       9 |         7 |    89 |*      2014 |       57 |     |         8 | Physics   |
                                          |      14 |         7 |    53 |       2014 |       59 |     |         9 | History   |
                                          |       9 |         9 |    87 |*      2014 |       76 |     |        10 | Computing |
                                          |      15 |         9 |    54 |       2014 |       78 |     +-----------+-----------+
                                          |      19 |         9 |    58 |       2014 |       81 |
                                          |       8 |         1 |    73 |       2015 |       85 |
                                          |       9 |         1 |    93 |       2015 |       86 |
                                          |      14 |         1 |    79 |       2015 |       89 |
                                          |      15 |         1 |    94 |       2015 |       90 |     
                                          |      19 |         1 |    71 |       2015 |       92 |
                                          |       9 |         5 |    95 |       2015 |      122 |
                                          |      14 |         5 |    92 |       2015 |      124 |
                                          |       9 |         7 |    69 |       2015 |      138 |
                                          |      14 |         7 |    76 |       2015 |      140 |
                                          |       9 |         9 |    61 |       2015 |      157 |
                                          |      15 |         9 |    86 |       2015 |      159 |
                                          |      19 |         9 |    79 |       2015 |      162 |
                                          +---------+-----------+-------+------------+----------+
    

    with this single query to get the 2014 exam results for Wilson

    SELECT s.subject
         , r.pcent as pcent
    FROM pupil p 
         JOIN result r USING (pupilid)
         JOIN subject s USING (subjectid)
    WHERE p.lname = 'Wilson'
    	  AND r.schoolyear = 2014
    ORDER BY subject;

    The charts were drawn using just the pcent values from the database without specific classes for the pcent values, just generated SVG graphics.

    Capture.PNG


  9. If it helps, I use data attributes in such situations

    html

    <input type='submit' name='btn' class='a_menu $a_sz' data-num='99'
                value='Update Post Suicide Results From NFL Results'>

    JS

    var obj99 = $("input[type='submit'][data-num='99']")
    alert(obj99.val())

     


  10. If you have 5 API calls each taking 4 seconds then the total is 20 seconds if you call them in series.

    One thing you might want to experiment with is kicking off an AJAX request for each API call, to see if they execute in parallel, and then consolidating the returned results.


  11. Seems like your database got bigger

    +-----------------+                                                
    |     menu        |                                                
    +-----------------+                              
    |  menu_id        |--+                                             
    |  description    |  |                                 
    +-----------------+  |                                      
                         |                         
                         |     +-----------------+   
                         |     |  menu_dishes    |   
                         |     +-----------------+   
                         +----<|  menu_id        |         +-------------------+
                               |  dish_id        |>--+     |     dish          |
                               +-----------------+   |     +-------------------+
                                                     +----<|  dish_id          |----+      +----------------+
                               +-----------------+         |  name             |    |      | ingredient     |
                               |  course         |    +---<|  course_id        |    |      +----------------+
                               +-----------------+    |    +-------------------+    |      | ingred_id      |----+
                               |  course_id      |----+                             +-----<| dish_id        |    |     +----------------+
                               |  description    |                                         | name           |    |     | substitute     |
                               +-----------------+                                         +----------------+    |     +----------------+
                                                                                                                 |     | sub_id         |
                                                                                                                 +----<| ingred_id      |
                                                                                                                       | new_item_desc  |
                                                                                                                       | event_id       |
                                                                                                                       +----------------+

     


  12. How far can they go with changing the dish?


    "I want a full English breakfast, but I'd like roast potato instead of hash browns, roast turkey instead of bacon, chipolatas instead of sausage, carrot puree instead of mushrooms, cranbery sauce instead of tomato, sprouts instead of baked beans and some gravy instead of eggs.
    Toast and marmalade? No thanks, I'll have Christmas pudding with rum-flavoured white sauce."


  13. 21 minutes ago, phppup said:

    Do the ID's then become 'attached'?

    There won't be a piece of string stretched between them but as they will have the same value you can use them in a join.

    If they aren't in the same form you don't need to use last_insert_id as the customer will be pre-existing when you add the contact info.

×

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.