Page 1 of 1

Efficiency?

Posted: Fri Sep 04, 2009 1:01 am
by Roeltje
Hi,

I'm new on this forum and i'm from the netherlands. I've got a question.

I've got a 'huge' code for only a couple of queries. This is my code:

Code: Select all

<?
        $nvt = array('ladeGreep', 'ladeFrontVerst', 'loperBlumotion', 'loperKg', 'loperLengte', 'loperTipon', 'ladeBinnen');
        $aLade = array('ladeType','ladeSoort','ladeOpp','ladeBreedte','ladeDiepte','ladeHoogte','ladeBodem','ladeBinnen','ladeGreep','ladeBestek','ladeFrontVerst');        
        $aLoper = array('loperType','loperBlumotion','loperKg','loperLengte','loperTipon');
        foreach($nvt as $aanpas)
        {
            if(!isset($_POST[$aanpas]))
            {
                $_POST[$aanpas] = 'n.v.t.';
            }
        }
        $qCheck = "SELECT id FROM lade WHERE ladetype = ".((isset($_POST['ladeType']) && $_POST['ladeType'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeType']) . "'": "NULL")." AND houtsoort =
            ".((isset($_POST['ladeSoort']) && $_POST['ladeSoort'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeSoort']) . "'": "NULL")." AND oppervlak =
            ".((isset($_POST['ladeOpp']) && $_POST['ladeOpp'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeOpp']) . "'": "NULL")." AND breedte = 
            ".((isset($_POST['ladeBreedte']) && $_POST['ladeBreedte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBreedte']) . "'": "NULL")." AND diepte = 
            ".((isset($_POST['ladeDiepte']) && $_POST['ladeDiepte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeDiepte']) . "'": "NULL")." AND hoogte =
            ".((isset($_POST['ladeHoogte']) && $_POST['ladeHoogte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeHoogte']) . "'": "NULL")." AND bodem = 
            ".((isset($_POST['ladeBodem']) && $_POST['ladeBodem'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBodem']) . "'": "NULL")." AND binnenlade =
            ".((isset($_POST['ladeBinnen']) && $_POST['ladeBinnen'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBinnen']) . "'": "NULL")."
            ".((isset($_POST['ladeGreep']) && $_POST['ladeGreep'] != "n.v.t.") ? "AND greep ='" . mysql_real_escape_string($_POST['ladeGreep']) . "'": "")." AND bestek =
            ".((isset($_POST['ladeBestek']) && $_POST['ladeBestek'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBestek']) . "'": "NULL")." AND front_verst =
            ".((isset($_POST['ladeFrontVerst']) && $_POST['ladeFrontVerst'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeFrontVerst']) . "'": "NULL")."";
        $rCheck = mysql_query($qCheck) or die(mysql_error());
        if(mysql_num_rows($rCheck) == 0)
        {        
            if($_POST['loperType'] == 'geen' || !isset($_POST['loperType']))
            {
                $qInsert1 = "INSERT INTO lade VALUES
                ('',
                ".((isset($_POST['ladeType']) && $_POST['ladeType'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeType']) . "'": "NULL").",
                ".((isset($_POST['ladeSoort']) && $_POST['ladeSoort'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeSoort']) . "'": "NULL").",
                ".((isset($_POST['ladeOpp']) && $_POST['ladeOpp'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeOpp']) . "'": "NULL").",
                ".((isset($_POST['ladeBreedte']) && $_POST['ladeBreedte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBreedte']) . "'": "NULL").",
                ".((isset($_POST['ladeDiepte']) && $_POST['ladeDiepte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeDiepte']) . "'": "NULL").",
                ".((isset($_POST['ladeHoogte']) && $_POST['ladeHoogte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeHoogte']) . "'": "NULL").",
                ".((isset($_POST['ladeBodem']) && $_POST['ladeBodem'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBodem']) . "'": "NULL").",
                ".((isset($_POST['ladeBinnen']) && $_POST['ladeBinnen'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBinnen']) . "'": "NULL").",
                ".((isset($_POST['ladeGreep']) && $_POST['ladeGreep'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeGreep']) . "'": "NULL").",
                ".((isset($_POST['ladeBestek']) && $_POST['ladeBestek'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBestek']) . "'": "NULL").",
                ".((isset($_POST['ladeFrontVerst']) && $_POST['ladeFrontVerst'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeFrontVerst']) . "'": "NULL")."
                )";
               $rInsert1 = mysql_query($qInsert1) or die (mysql_error());
            }
            else
            {
                $qInsert1 = "INSERT INTO lade VALUES
                ('',
                ".((isset($_POST['ladeType']) && $_POST['ladeType'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeType']) . "'": "NULL").",
                ".((isset($_POST['ladeSoort']) && $_POST['ladeSoort'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeSoort']) . "'": "NULL").",
                ".((isset($_POST['ladeOpp']) && $_POST['ladeOpp'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeOpp']) . "'": "NULL").",
                ".((isset($_POST['ladeBreedte']) && $_POST['ladeBreedte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBreedte']) . "'": "NULL").",
                ".((isset($_POST['ladeDiepte']) && $_POST['ladeDiepte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeDiepte']) . "'": "NULL").",
                ".((isset($_POST['ladeHoogte']) && $_POST['ladeHoogte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeHoogte']) . "'": "NULL").",
                ".((isset($_POST['ladeBodem']) && $_POST['ladeBodem'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBodem']) . "'": "NULL").",
                ".((isset($_POST['ladeBinnen']) && $_POST['ladeBinnen'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBinnen']) . "'": "NULL").",
                ".((isset($_POST['ladeGreep']) && $_POST['ladeGreep'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeGreep']) . "'": "NULL").",
                ".((isset($_POST['ladeBestek']) && $_POST['ladeBestek'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeBestek']) . "'": "NULL").",
                ".((isset($_POST['ladeFrontVerst']) && $_POST['ladeFrontVerst'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeFrontVerst']) . "'": "NULL")."
                )";
               $rInsert1 = mysql_query($qInsert1) or die (mysql_error());    
               $qInsert2 = "INSERT INTO loper VALUES
               ('',
                ".((isset($_POST['loperType']) && $_POST['loperType'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['loperType']) . "'": "NULL").",
                ".((isset($_POST['loperBlumotion']) && $_POST['loperBlumotion'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['loperBlumotion']) . "'": "NULL").",
                ".((isset($_POST['ladeKg']) && $_POST['ladeKg'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeKg']) . "'": "NULL").",
                ".((isset($_POST['ladeLengte']) && $_POST['ladeLengte'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeLengte']) . "'": "NULL").",
                ".((isset($_POST['ladeTipon']) && $_POST['ladeTipon'] != "n.v.t.") ? "'" . mysql_real_escape_string($_POST['ladeTipon']) . "'": "NULL")."
                )";    
                $rInsert2 = mysql_query($qInsert2) or die (mysql_error());    
            }
?>
-- http://plaatscode.be/137516/

This is how the database looks

LADE
id ladetype houtsoort oppervlak breedte diepte hoogte bodem binnenlade greep bestek front_vers

LOPER
id type blumotion kg lengte tipon


It works this way: If the chosen options aren't in the database yet, then the code has to put it into the database, and get the ID and put this in a $_SESSION. Otherwise if the chosen options are in the database, i only have to get the ID and also put this in a $_SESSION. That's how the code works. But how can i do this easy? I think the way i use is not the easiest way is it? Is my database correctly?
Live example of the form which need to be filled in: http://yfrog.com/0h69493364j

Re: Efficiency?

Posted: Sun Sep 06, 2009 12:52 pm
by Robert07
Why are you setting the unset values to n.v.t. and then checking for both whether the values are set and whether they are n.v.t.? That seems unnecessary to me. I would suggest removing the first loop that sets unset values to n.v.t. and then removing the second condition in the many places it appears later. That would trim down your code a bit.