Pjack125 Posted December 15, 2008 Share Posted December 15, 2008 I recently posted a question back a few days ago which resulted in the creation of a this foreach loop. <?php //input optional buttons info into Buttons table $pages = array('MAPageID','PMPageID','EEPageID', 'MEPageID', 'OEPageID', 'SWPageID', 'SEPageID', 'STPageID'); // loop through the pages foreach($pages as $key) { // check to see if the department ($key) is within the $_SESSION if(isset($_SESSION[$key]) && strtoupper($_SESSION[$key]) != '') { // work out the prefix from the key to access the fields within the $_SESSION $pfx = substr($key, 0, 2); // setup the query $Query6 = "INSERT INTO Buttons (ButtonID, URL, Lable,Alt,PageID) ". "VALUES ( NULL, '".$_SESSION[ $pfx.'BL1' ]."', '".$_SESSION[ $pfx.'BLab1' ]."', '".$_SESSION[ $pfx.'BL1' ]."', '".$_SESSION[ $pfx.'PageID' ]."'), ( NULL, '".$_SESSION[ $pfx.'BL2' ]."', '".$_SESSION[ $pfx.'BLab2' ]."', '".$_SESSION[ $pfx.'BL2' ]."', '".$_SESSION[ $pfx.'PageID' ]."'), ( NULL, '".$_SESSION[ $pfx.'BL3' ]."', '".$_SESSION[ $pfx.'BLab3' ]."', '".$_SESSION[ $pfx.'BL3' ]."', '".$_SESSION[ $pfx.'PageID' ]."'), ( NULL, '".$_SESSION[ $pfx.'BL4' ]."', '".$_SESSION[ $pfx.'BLab4' ]."', '".$_SESSION[ $pfx.'BL4' ]."', '".$_SESSION[ $pfx.'PageID' ]."'), ( NULL, '".$_SESSION[ $pfx.'BL5' ]."', '".$_SESSION[ $pfx.'BLab5' ]."', '".$_SESSION[ $pfx.'BL5' ]."', '".$_SESSION[ $pfx.'PageID' ]."')"; // run the query, and error will display if there is a problem mysql_query($Query6) or die('MySQL Error: '.mysql_error() . '<br />Query: <pre>'.$Query6.'</pre>'); echo $Query6; } } ?> now the problem I am having is that some of the buttons in the query may have blank values and i don't want those blank values to be inserted into the database for example $_SESSION['MEBL1'] may be blank and if that is blank i don't want inserted what is the best way to approach this? Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/ Share on other sites More sharing options...
B34ST Posted December 15, 2008 Share Posted December 15, 2008 can you insert a NULL value? if (!$_SESSION['MEBL1']) { $_SESSION['MEBL1'] = 'NULL'; } //insert into db Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/#findComment-716000 Share on other sites More sharing options...
Pjack125 Posted December 15, 2008 Author Share Posted December 15, 2008 I don't think so because that would still create a record and if you have the record with null data when i pull this info into the other page i have it would just create a blank button. Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/#findComment-716026 Share on other sites More sharing options...
wildteen88 Posted December 15, 2008 Share Posted December 15, 2008 I remember this, I provided the function. I think what you need to do is construct your session better. Rather than do: $_SESSION[ 'MEBL1' ] $_SESSION[ 'MEBLab1' ] $_SESSION[ 'MEBL1' ] $_SESSION[ 'ME'PageID' ] $_SESSION[ 'MEBL2' ] $_SESSION[ 'MEBLab2' ] $_SESSION[ 'MEBL2' ] $_SESSION[ 'ME'PageID' ] etc I'd construct it like so: $_SESSION['ME'][1]['BL'] $_SESSION['ME'][1]['BLab'] $_SESSION['ME'][1]['BL'] $_SESSION['ME'][1]['PageID'] $_SESSION['ME'][2]['BL'] $_SESSION['ME'][2]['BLab'] $_SESSION['ME'][2]['BL'] $_SESSION['ME'][2]['PageID'] etc Which makes it a multiple dimensional array. This will make it a lot more easier to work with as you can then easily loop through your session data, eg <?php // dummy session data $_SESSION['ME'][1]['BL'] = 'ButtonID 1'; $_SESSION['ME'][1]['BLab'] = 'Lab 1'; $_SESSION['ME'][1]['PageID'] = 'PageID 1'; $_SESSION['ME'][2]['BL'] = 'ButtonID 2'; $_SESSION['ME'][2]['BLab'] = 'Lab 2'; $_SESSION['ME'][2]['PageID'] = 'PageID 2'; $_SESSION['SW'][1]['BL'] = 'ButtonID 4'; $_SESSION['SW'][1]['BLab'] = 'Lab 4'; $_SESSION['SW'][1]['PageID'] = 'PageID 4'; $_SESSION['SW'][2]['BL'] = 'ButtonID 5'; $_SESSION['SW'][2]['BLab'] = 'Lab 5'; $_SESSION['SW'][2]['PageID'] = 'PageID 5'; // departments $depts = array('MA','PM','EE', 'ME', 'OE', 'SW', 'SE', 'ST'); // loops through departments foreach($depts as $dept ) { // check to see if the department is in the array if(isset($_SESSION[$dept]) && is_array($_SESSION[$dept])) { echo '<p>Depertment: <b>'.$dept.'</b><br />'; // loop through the fields foreach($_SESSION[$dept] as $fid => $field_data) { echo '<b>Field #'.$fid.'</b><br />'; echo $field_data['BL'].'<br />'; echo $field_data['BLab'].'<br />'; echo $field_data['PageID'].'<br />'; echo '<br />'; } echo '</p><hr />'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/#findComment-716031 Share on other sites More sharing options...
Pjack125 Posted December 15, 2008 Author Share Posted December 15, 2008 although this makes my life easier but I still have the problem of place in the array that holds a value of '' The previous code you wrote worked perfectly. The first part is all good. the headache is having variables that carries a value of blank. so the headache really is in the query part. Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/#findComment-716080 Share on other sites More sharing options...
mrMarcus Posted December 15, 2008 Share Posted December 15, 2008 I don't think so because that would still create a record and if you have the record with null data when i pull this info into the other page i have it would just create a blank button. why don't you just check the length of the record(s) coming from the database and if any are empty or null then they aren't displayed .. that way a null record wouldn't be such a problem. other than that, check out array_values() .. eliminates any null/empty values in an array. try and work with that to construct your array/query for the database. Quote Link to comment https://forums.phpfreaks.com/topic/137090-help-with-foreach-loop/#findComment-716099 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.