Jump to content

Cleaning up codes


Pjack125

Recommended Posts

I'm having a big brain fart is there any way to clean this without me having to write 6 if statements?

if ($_SESSION['EELEAD'] == 'NEW'){
$Query2b = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['EEEMPID']."', '".$_SESSION['EEFNAME']."', '".$_SESSION['EELName']."', '".$_SESSION['EEEmail']."', '".$_SESSION['EEPhone']."','EE', '<img src='“.$_SESSION['EEEMPID']."?size=small' width=150/>')";	
}
if ($_SESSION['MELEAD'] == 'NEW'){
$Query2c = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['MEEMPID']."', '".$_SESSION['MEFNAME']."', '".$_SESSION['MELName']."', '".$_SESSION['MEEmail']."', '".$_SESSION['MEPhone']."','ME', '<img src=‘“.$_SESSION['MEEMPID']."?size=small' width=150/>')";	
}
if ($_SESSION['OELEAD'] == 'NEW'){
$Query2d = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['OEEMPID']."', '".$_SESSION['OEFNAME']."', '".$_SESSION['OELName']."', '".$_SESSION['OEEmail']."', '".$_SESSION['OEPhone']."','OE', '<img src=‘“.$_SESSION['OEEMPID']."?size=small' width=150/>')";	
}
if ($_SESSION['SWLEAD'] == 'NEW'){
$Query2e = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['SWEMPID']."', '".$_SESSION['SWFNAME']."', '".$_SESSION['SWLName']."', '".$_SESSION['SWEmail']."', '".$_SESSION['SWPhone']."','SW', '<img src=‘“.$_SESSION['SWEMPID']."?size=small' width=150/>')";	
}
if ($_SESSION['SELEAD'] == 'NEW'){
$Query2f = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['SEEMPID']."', '".$_SESSION['SEFNAME']."', '".$_SESSION['SELName']."', '".$_SESSION['SEEmail']."', '".$_SESSION['SEPhone']."','SE', '<img src=‘“.$_SESSION['SEEMPID']."?size=small' width=150/>')";	
}
if ($_SESSION['ST'] == 'NEW'){
$Query2g = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
	  "VALUES ('".$_SESSION['STEMPID']."', '".$_SESSION['STFNAME']."', '".$_SESSION['STLName']."', '".$_SESSION['STEmail']."', '".$_SESSION['STPhone']."','ST', '<img src=‘“.$_SESSION['STEMPID']."?size=small' width=150/>')";	
}

 

there must be a way to loop it to where only the query number changes the first letter in the session variables change without having to write is a million times

Link to comment
https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/
Share on other sites

Looking at the code a loop would be much better in this case.

<?php

// list available departments
$keys = array('EELEAD', 'MELEAD', 'OELEAD', 'SWLEAD', 'SELEAD', 'ST');

// loop through the departments
foreach($keys as $key)
{
    // check to see if the department ($key) is within the $_SESSION and is set to NEW
    if(isset($_SESSION[$key]) && strtoupper($_SESSION[$key]) == 'NEW')
    {
        // work out the prefix from the key to access the fields within the $_SESSION
        $pfx = substr($key, 0, 2);

        // setup the query
        $query = "INSERT INTO Personel(EMPID, FName, LName, Email, Phone, Department, Picture) ".
                 "VALUES ( '".$_SESSION[ $pfx.'EMPID' ]."',
                           '".$_SESSION[ $pfx.'FNAME' ]."',
                           '".$_SESSION[ $pfx.'LName' ]."',
                           '".$_SESSION[ $pfx.'Email' ]."',
                           '".$_SESSION[ $pfx.'Phone' ]."',
                           '$pfx',
                           '<img src='".$_SESSION[ $pfx.'EMPID' ]."?size=small' width=150/>')";

        // run the query, and error will display if there is a problem
        mysql_query($query) or die('MySQL Error: '.mysql_error() . '<br />Query: <pre>'.$query.'</pre>');
    }
}

?>

 

Do note that the code above is untested.

Link to comment
https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/#findComment-713919
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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