Jump to content

Barand

Moderators
  • Posts

    24,302
  • Joined

  • Last visited

  • Days Won

    792

Posts posted by Barand

  1. Why set a superflous boolean variable when you have a perfectly good boolean expression already. All you are doing is adding an extra step

    $too_many_labels will have the same value as  $numberoflabels > 4

    I'd go for option 1 or option 3, which would be

    // style 3
    $labelCounter = $db->prepare("SELECT count(*) ... "); // mySQL counts labels
    
    if($numberoflabels > 4) {
         echo "Sorry visitor, no label could be created";
    }
    else {
        // RUN BIG CURL SCRIPT IF LESS THAN 5 LABELS
        // LONG CURL SCRIPT GOES HERE
    
        $log = $db->prepare("INSERT into label_counter... // log the good label 
    }  

     

    • Like 1
  2. try something like this example

    <script type="text/javascript">
        function myFunction(myform)
        {
            return myform.selA.value != '' || myform.textB.value != ''
        }
    </script>
    
    <form action="" onsubmit="return myFunction(this)">
        <select name='selA'><option value=''>- select -</option><option>AAAAAAAA</option><option>BBBBBBBBB</option></select>
        <input type="text" name="textB">
        <input type="submit" name="btnSub" value="Submit">
    </form>

     

  3. 2 hours ago, requinix said:

    As opposed to PHP's && and ||, which are actually subtly different than "and" and "or".

    Just to confirm Requinix's statement by psedocode examples

    This works:

    result = query(sql) or die(error message)

    Whereas this doesn't

    result = query(sql) || die(error message)

     

  4. In MySQL you use backticks "`" around identifiers when the identifier name is a reserved keyword or contains a space or other special characters. In your code they would not be necessary.

    In Postgres a different character from the backtick is used in those circumstances. The manual will tell you which.

    While most RDMS systems have a standard core SQL there are variations between the various dialects. You have to be aware of them.

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

     

  6. I find the easiest way to do these is to get the data you need then store in an array that is structured to match the required output. Then create your output from the array.

    DATA

    SELECT * FROM test.wp_lvsz_evr_attendee;                               SELECT * FROM test.wp_lvsz_evr_answer;
    +-----+-----------------+-------+-------+----------+                   +-----------------+-------------+--------+
    | id  | registration_id | lname | fname | event_id |                   | registration_id | question_id | answer |
    +-----+-----------------+-------+-------+----------+                   +-----------------+-------------+--------+
    | 459 | 1               | Doe   | John  | 8        |                   |               1 |          40 | aaa    |
    | 460 | 2               | Evans | Jane  | 9        |                   |               1 |          41 | bbb    |
    | 461 | 3               | Smith | Dave  | 8        |                   |               1 |          42 | ccc    |
    | 462 | 4               | Jones | Alec  | 9        |                   |               1 |          43 | ddd    |
    +-----+-----------------+-------+-------+----------+                   |               2 |          40 | eee    |
                                                                           |               2 |          41 | fff    |
                                                                           |               2 |          42 | ggg    |
                                                                           |               2 |          43 | hhh    |
                                                                           |               3 |          40 | kkk    |
                                                                           |               3 |          41 | mmm    |
                                                                           |               3 |          42 | nnn    |
                                                                           |               3 |          43 | ooo    |
                                                                           |               4 |          40 | ppp    |
                                                                           |               4 |          41 | qqq    |
                                                                           |               4 |          42 | rrr    |
                                                                           |               4 |          43 | sss    |
                                                                           +-----------------+-------------+--------+

    QUERY

    SELECT  att.id
         , concat( lname, ' ', left( fname, 1 ) ) AS fullname
         , question_id
         , answer
    FROM wp_lvsz_evr_answer ans 
         JOIN wp_lvsz_evr_attendee att 
                    ON att.registration_id = ans.registration_id
                    AND question_id BETWEEN 41 AND 43;
    +-----+----------+-------------+--------+
    | id  | fullname | question_id | answer |
    +-----+----------+-------------+--------+
    | 459 | Doe J    |          41 | bbb    |
    | 459 | Doe J    |          42 | ccc    |
    | 459 | Doe J    |          43 | ddd    |
    | 460 | Evans J  |          41 | fff    |
    | 460 | Evans J  |          42 | ggg    |
    | 460 | Evans J  |          43 | hhh    |
    | 461 | Smith D  |          41 | mmm    |
    | 461 | Smith D  |          42 | nnn    |
    | 461 | Smith D  |          43 | ooo    |
    | 462 | Jones A  |          41 | qqq    |
    | 462 | Jones A  |          42 | rrr    |
    | 462 | Jones A  |          43 | sss    |
    +-----+----------+-------------+--------+

    CODE

    <?php
    $res = $db->query("SELECT  att.id
                             , concat( lname, ' ', left( fname, 1 ) ) AS fullname
                             , question_id
                             , answer
                        FROM wp_lvsz_evr_answer ans 
                             JOIN wp_lvsz_evr_attendee att 
                                        ON att.registration_id = ans.registration_id
                                        AND question_id BETWEEN 41 AND 43
                        ");
    
    $headings = [  42 => 'Belt' ,
                   43 => 'Event' , 
                   41 => 'School' 
                ];
                
    $temp_array = array_fill_keys(array_keys($headings), '');    // array for each attendee to be filled in fro query results 
    
    // process query results and place in array  with attendee as the key 
    $data = [];
    foreach ($res as $r) {
        if ( !isset($data[$r['id']])) {
            $data[$r['id']] = [ 'name' => $r['fullname'], 'answers' => $temp_array ] ;
        }
        $data[$r['id']]['answers'][$r['question_id']] = $r['answer'] ;   // store answer in its array position
    }
    
    // now we can easily output the array into an html table
    
    $theads = "<tr><th>Name</th><th>" . join('</th><th>', $headings) . "</th></tr>\n";
    $tdata = '';
    foreach ($data as $att) {
        $tdata .= "<tr><td>{$att['name']}</td><td>" . join('</td><td>', $att['answers']) . "</td></tr>\n";
    }                
    ?>
    
    <table border='1' style='border-collapse: collapse; width: 600px'>
        <?=$theads?>
        <?=$tdata?>
    <?table>

    RESULT

    Capture.PNG

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