tags:

views:

80

answers:

3

Hey Folk

I've got a little problem i hope you're able to help me fix.

First of all my DB table that is involved look like this:

+---------------+--------------+------+-----+-------------------+----------------+
| Field         | Type         | Null | Key | Default           | Extra          |
+---------------+--------------+------+-----+-------------------+----------------+
| id            | int(11)      | NO   | PRI | NULL              | auto_increment |
| KlubID        | varchar(11)  | NO   |     | 0                 |                |
| Medlemsnummer | varchar(11)  | NO   |     | 0                 |                |
| KlubType      | varchar(128) | NO   |     |                   |                |
| Handling      | varchar(128) | NO   |     |                   |                |
| Tidspunkt     | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
+---------------+--------------+------+-----+-------------------+----------------+

Say i got X entries where "Handling" equals Y in my table lets call the results P . Now I would like to take all P and insert a row foreach P where the value of "Handling" now will be equal to Q.

The problem here is that I want to do the insert with one button (submit) not multiple forms.

In advance thank you very much for your help. Feel free to ask for more info if this needs more clarity.

/Nicki

EDIT: For more clarity

public static function find_todays_children() {
    global $db;
    $sql = "SELECT * 
            FROM (
                SELECT * 
                FROM Handlinger 
                AS a
                WHERE date(Tidspunkt) = curdate() 
                AND Tidspunkt = (
                    SELECT max(Tidspunkt) 
                    FROM Handlinger 
                    AS b
                    WHERE a.Medlemsnummer = b.Medlemsnummer
                    )
                )
            AS c
            ORDER BY handling DESC, medlemsnummer";
    return static::find_by_sql($sql);
}

This query above returns the following.

+------+--------+---------------+----------+----------+---------------------+
| id   | KlubID | Medlemsnummer | KlubType | Handling | Tidspunkt           |
+------+--------+---------------+----------+----------+---------------------+
| 5786 | 0      | 1             | FK       | Kommet   | 2010-10-06 13:48:06 |
| 5787 | 0      | 2             | FK       | Kommet   | 2010-10-06 13:48:10 |
| 5789 | 0      | 4             | FK       | Kommet   | 2010-10-06 13:48:16 |
| 5790 | 0      | 3             | FK       | G?et     | 2010-10-06 13:48:27 |
+------+--------+---------------+----------+----------+---------------------+

I then want to be able to insert 3 rows where the field "Handling" is another value. I can do this by HTML form with PHP but I can't figure out how to do it all by only clickin once... The important thing is that not to insert anything where the latest entry for a specific "Medlemsnummer" (user_id) is already equal to the value "Gået"

My form looks like this:

...
    if(isset($action->Handling) != "Kommet") {
        $do_action = "Kommet";
    } else {
        $do_action = "Gået";
    }
...

<section>
    <form action="phineaslog_barn.php" method="post">
        <label for="Status">Klub Navn: </label>
        <input type="text" name="Medlemsnummer" value="<?php echo $child->Medlemsnummer; ?>" />
        <input type="text" name="Handling" value="<?php echo $do_action; ?>" />
        <input type="submit" name="submit" value="<?php echo $do_action; ?>" />
    </form>
</section>
A: 

You could do something like this:

Write a Method to return a DataTable
Ex:
private DataTable pResutls()
{
do your select handling=Y;
return DataTableWithResult;
}



in your button click event

DataTable p = pResutls();
foreach(DataRow row in p)
{ if (row["Handling"].toSring()=='Q')
{

then insert this row (write your method to insert)
}

}
by this way you will need a method to get the Results (handling=='y')
a method that insert a row (you could pass a DataRow as parameter and perform the update from there)
and then from your Button you can do everything in one single Click like above.

rafaelsr
A: 

In essence, you want to run this query?

INSERT INTO tablename (KlubID,Medlemsnummer,KlubType,Handling,Tidspunkt)
SELECT KlubID,Medlemsnummer,KlubType,'Gået',NOW()
FROM tablename
WHERE Handling = 'Kommet';

Or, more in PHP:

$from = $_POST['Handling'] == 'Kommet' ? 'Kommet' : 'Gået';
$to   = $from == 'Kommet' ? 'Gået' :'Kommet';

mysql_query("
INSERT INTO tablename (KlubID,Medlemsnummer,KlubType,Handling,Tidspunkt)
SELECT t1.KlubID,t1.Medlemsnummer,t1.KlubType,'$to',NOW()
FROM tablename t1
LEFT JOIN tablename t2
ON t1.Medlemsnummer = t2.Medlemsnummer
AND t2.Tidspunkt > t1.Tidspunkt
WHERE DATE(t1.Tidspunkt) = CURDATE() AND t1.Handling = '$from' AND t2.id IS NULL");

Or are there more restrictions on which rows you'd like to insert besides Handling?

Wrikken
I see your point but this will create a problem in terms of inserting way more than actually needed. Consider it a login/logout function. If the user is logged in then logout else do nothing. Except i want to be all to do this for all who is "logged in" at moment instantly... Notice this isn't a login/logout function but it share some similarity in order to register actions from an user.
nickifrandsen
I am sadly not capable to convert 'this is way more then needed' into an actual limitation. What are the _other_ fields we should limit on, what is their data, can we get that into the `WHERE` clause? Should we only get the records for which the _last_ Handling type was 'Kommet', instead of _any_ Handling?
Wrikken
I have made an edit to the original question which i hope clarify thing a bit. The problem with your suggestion is that it inserts the value "Gået" foreach row where "Handling" equals "Kommet". But this creates a problem if the latest "Handling" for an "Medlemsnummer"/user_id is already equal to "Gået".
nickifrandsen
Edited the answer to reflect those extra requirements.
Wrikken
+1  A: 

My understanding of the question:

  • For each row which is the final entry for a user, where handling = kommet

    • Insert another row with handling = gået, tidspunkt = now()

So... to combine the best of two worlds...

INSERT INTO tablename (KlubID,Medlemsnummer,KlubType,Handling,Tidspunkt)
    SELECT KlubID,Medlemsnummer,KlubType,'Gået',NOW()
    FROM Handlinger 
    AS a
    WHERE date(Tidspunkt) = curdate() 
    AND Handling = 'Kommet'
    AND Tidspunkt = (
        SELECT max(Tidspunkt) 
        FROM Handlinger 
        AS b
        WHERE a.Medlemsnummer = b.Medlemsnummer
     )
Alexander Sagen
This works. Thanks a lot :) I got a little bonus question though. Is it possible to automatize this process so i runs it self each day at a specific time.
nickifrandsen
Always ready to help out a fellow Scandinavian.. Depends what system you're on, and what access you have. Cannot be done in pure php/mysql. Cron jobs are made for this if you're on a unix/linux system.
Alexander Sagen
BTW, you could check out http://www.setcronjob.com/ they can call your script (any given URL) at a set time interval, your scenario should be free. Might be an easier option.
Alexander Sagen