Jump to content

Barand

Moderators
  • Posts

    24,338
  • Joined

  • Last visited

  • Days Won

    795

Posts posted by Barand

  1. In what way is that "outside the function"?

    $db = DB();                                         // connection OUTSIDE the function
    
    $vehicle_data = UserDetails($db, $user_id);                        // pass the $db connection as a parameter

    plus you need to redefine the function to accept the extra parameter

    function UserDetails ($db, $user_id) {
       // function code
    }
  2. It was better as it was. Just add a WHERE clause to tell it which user.

        $query = $db->prepare("SELECT users.user_id
                                , users.registration
                                , users.email
                                , vehicle.reg_id
                                , vehicle.vehicle
                                , vehicle.registration
                                , vehicle.chassis 
                                FROM users JOIN vehicle
                                    ON vehicle.registration = users.registration
                                WHERE users.user_id = :user_id");
        $query->bindParam("user_id", $user_id, PDO::PARAM_STR);                      
    
    

    It looks like the line

    $db = DB();

    is connecting to your database. Move that code out of the function and pass $db as a parameter to your function with $user_id. Connecting is a relatively slow process and you don't want to do every for every query.

  3.  

    I put registration in both as I thought this may be used to help create a relationship between the 2

     

    Then that is what you should use for the join with your current tables.

    SELECT ....
    FROM user u
    JOIN vehicle v ON u.registration = v.registration

    However, a more flexible model would be ...

    +-------------+
    |   user      |
    +-------------+
    |  user_id    |--------+
    |  email      |        |         +--------------+
    |  etc        |        |         |  vehicle     |
    +-------------+        |         +--------------+
                           |         | vehicle_id   |
                           +--------<| user_id      |
                                     | registration |
                                     | vehicle      |
                                     | chassis_no   |
                                     +--------------+
    

    .. which easily allows you to add a second (and third...) car for a user just by adding another row with the user's user_id. In this model the join would be on the user_id columns.

  4. You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function.

     

    For example

    $files['file1.txt']  = [    'date' => '2017-01-03',
                                'content' => 'aaa'
                                ];
    $files['file2.txt']  = [    'date' => '2017-01-01',
                                'content' => 'bbb'
                                ];
    $files['file3.txt']  = [    'date' => '2017-01-02',
                                'content' => 'ccc'
                                ];
    $files['file4.txt']  = [    'date' => '2017-01-04',
                                'content' => 'ddd'
                                ];
    
    uasort($files, function ($a, $b) {
                        return strcmp ($a['date'], $b['date']);
                    });
    
    foreach ($files as $filename => $fdata) {
        // process the output
    }
    
    
  5.  How many are returned if any is irrelevant.

    How can you say that when you have no knowledge of the application?

     

    Let's say I want to list names in three columns and want the numbers in each column to be as near equal as possible. For example I want

            A   E   I
            B   F   J
            C   G   K
            D   H
    
    

    and not

            A   F   K
            B   G
            C   H
            D   I
            E   J
    

    I would be interested in your method of doing this without knowing how many there are.

  6. Benanamen - I am looking for a way to identify those who have already signed up, not preventing future ones.  But thank you for the reply.

    Benanamen's point was that you need to stop this happening future, so you don't have to keep repeating this exercise.

  7. Sorry, careless with my column aliases

     

    Try

    SELECT DATE(payments.date) as dateportion
    , users.username
    , COUNT(*) as tot 
    from payments 
    INNER JOIN users ON payments.user_id = users.id 
    WHERE payments.membership_id IN (6,7) 
    GROUP BY payments.user_id, dateportion 
    HAVING tot > 1
    
  8. try

    $results = array(
                  array("SALE_ID"=>"1","SALE_DATE"=>"2017-01-01"),
                  array("SALE_ID"=>"2","SALE_DATE"=>"2017-02-15"),
                  array("SALE_ID"=>"3","SALE_DATE"=>"2017-03-25"),
                  array("SALE_ID"=>"4","SALE_DATE"=>"2017-04-10"),
                  array("SALE_ID"=>"5","SALE_DATE"=>"2017-01-08"),
                  array("SALE_ID"=>"6","SALE_DATE"=>"2017-02-23"),
                  array("SALE_ID"=>"7","SALE_DATE"=>"2017-03-15"),
                  array("SALE_ID"=>"8","SALE_DATE"=>"2017-04-09")
            );
            
    $results_by_week = [];
    
    foreach ($results as $sale) {
        $w = week_number($sale['SALE_DATE']);
        $results_by_week[$w][] = (object)$sale;
    }       
    
    ksort($results_by_week);
    
    
    • Like 1
×
×
  • 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.