Pjack125 Posted December 12, 2008 Share Posted December 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/ Share on other sites More sharing options...
Mark Baker Posted December 12, 2008 Share Posted December 12, 2008 switch, or you could use dynamic variables Quote Link to comment https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/#findComment-713877 Share on other sites More sharing options...
Pjack125 Posted December 12, 2008 Author Share Posted December 12, 2008 Switch wouldn't help much... but Dynamic variables might or a combo of the both might even be better. I'll just look at it some more but thanks for the Dynamic var idea.. Quote Link to comment https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/#findComment-713897 Share on other sites More sharing options...
wildteen88 Posted December 12, 2008 Share Posted December 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136705-cleaning-up-codes/#findComment-713919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.