efficacious Posted September 25, 2008 Share Posted September 25, 2008 Hi all, Need some help with this loop here. Been having a good ole time with it so far. The two arrays that need to match up in order to select them before output. $Filter_Item = array( [0]=>3 //THE ITEM SELECTED AND POSTED LAST ); $I_EquipID = array ( [0]=>1 //First ITEM IN DB [1]=>2 //Second ITEM IN DB [2]=>3 //Third ITEM IN DB ); THE LOOP: $I_Count=count($I_EquipID); for($x=0;$x<$I_Count;$x++) { if(isset($E_H_Equip)) { if(isset($Filter_Item[$x])) { if($Filter_Item[$x]==$I_EquipID[$x]) { echo("<option SELECTED value='".$I_EquipID[$x]."'>*".$I_No[$x]." | ".$I_Description[$x]."</option>"); } else { echo("<option value='".$I_EquipID[$x]."'>".$I_No[$x]." | ".$I_Description[$x]."</option>"); } } else { echo("<option value='".$I_EquipID[$x]."'>".$I_No[$x]." | ".$I_Description[$x]."</option>"); } } else { echo("<option value='".$I_EquipID[$x]."'>".$I_No[$x]." | ".$I_Description[$x]."</option>"); } } The problem obviously is that the array keys do not match up.. So how do I get the right option to be selected??? Also $Filter_Item , I will never know what combonation that array is in. Its based on users selections. It however will always be at least 1 of the options from the DB but never in the same order. Meaning if the users picks option 1 then this loop will work. however if user picks option 2 or 3 only it will not work.. If users picks ALL 3 options.. it will work.. Ya see where I'm going with this? Thanks for all help in advanced, `eff Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/ Share on other sites More sharing options...
efficacious Posted September 25, 2008 Author Share Posted September 25, 2008 bump.. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650507 Share on other sites More sharing options...
Push Eject Posted September 25, 2008 Share Posted September 25, 2008 Just a quick observation, you are relying on $x for all your key needs. I think you are going to need to use another variable within your loop to iterate through the second array checking for a match. Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650521 Share on other sites More sharing options...
efficacious Posted September 25, 2008 Author Share Posted September 25, 2008 I appreciate you posting your suggestion. However I'm not sure I quite understand what you mean. If I was to use a different increment variable my count would be thrown off.. I need the count to stay the same the whole time through the loop all of the IF statements besides the actual matching up statement or simply filters. That are saying this is not a selected item. This form does two things.. Submit data and displays it. Some checks are for submitting data and some checks are for displaying it.. If the one variable $E_H_Equip is set then that means we are going to be displaying data. The next check is saying if the index specified of that array does not have a value set then it obviously cant be a selected item. The third and final check is the one sayin ok.. now that we know we have no blank data or variables to look through lets find a match. See cuz I'm matching up what Items are available in the DB against which Items were chosen by the client. This would all be working if I could get the last two arrays keys to match up. Thats my only problem. I just need some if statement or function to say.. If the value of this variable = one of the values available from the DB then check that variables option as selected. Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650541 Share on other sites More sharing options...
Push Eject Posted September 25, 2008 Share Posted September 25, 2008 It seems to me the problem is that the $Filter_Item array and the $I_EquipID array are not ordinally related. Nor should they need to be, I don't think. I'm not sure how $Filter_Item is being populated, but perhaps something like this will work for you: <?php $I_Count=count($I_EquipID); for($x=0;$x<$I_Count;$x++) { if(isset($E_H_Equip)) { echo "<option"; foreach( $Filter_Item as $item ){ if( $I_EquipID[$x] == $item ) echo " SELECTED"; } echo " value='".$I_EquipID[$x]."'>*".$I_No[$x]." | ".$I_Description[$x]."</option>"; } else { echo("<option value='".$I_EquipID[$x]."'>".$I_No[$x]." | ".$I_Description[$x]."</option>"); } } ?> Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650579 Share on other sites More sharing options...
efficacious Posted September 25, 2008 Author Share Posted September 25, 2008 Awesome job man... this works perfectly... Much much appreciated. I compeletly forgot about foreach statements... I need to use thos more.. 1 Gold Star to Push Eject!! Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650586 Share on other sites More sharing options...
efficacious Posted September 25, 2008 Author Share Posted September 25, 2008 For clarifications.. $Filter_Item is being created like so /////////////////////////// //* GET EMP INFO IF ANY *// /////////////////////////// if(isset($_POST['Employee']) && $_POST['Employee']!='None') { $datatable=$tblpre."EmpHours"; $EHours_SQL="SELECT * FROM [".$datatable."] WHERE EmpID='".$_POST['Employee']."'"; $EHours_Result=mssql_query($EHours_SQL); while($EHours_row=mssql_fetch_array($EHours_Result)) { $E_H_ID =$EHours_row['EmpID']; $E_H_Amount =$EHours_row['HourAmount']; $E_H_Equip =$EHours_row['EquipmentID']; $E_H_Serv =$EHours_row['ServiceID']; $E_H_Mon =$EHours_row['Mon']; $E_H_Tues =$EHours_row['Tues']; $E_H_Wens =$EHours_row['Wens']; $E_H_Thurs =$EHours_row['Thurs']; $E_H_Fri =$EHours_row['Fri']; $E_H_Sat =$EHours_row['Sat']; $E_H_Sun =$EHours_row['Sun']; } if(isset($E_H_ID)) { /////////////////// //* FILTER DATA *// /////////////////// //* FILTER ITEM DATA *// $Filter_Item=explode("-",$E_H_Equip); //THIS ONE!! $E_H_Equip looks like this 1-2-3 ect... print_r($Filter_Item); // They are the ID for the item put together to fit into // A single database column //* FILTER SERV DATA *// $Filter_Serv=explode("-",$E_H_Serv); print_r($Filter_Serv); //* FILTER TIMES DATA *// $Filter_Time_Sun =explode("-", $E_H_Sun); $Filter_Time_Mon =explode("-", $E_H_Mon); $Filter_Time_Tues =explode("-", $E_H_Tues); $Filter_Time_Wens =explode("-", $E_H_Wens); $Filter_Time_Thurs =explode("-", $E_H_Thurs); $Filter_Time_Fri =explode("-", $E_H_Fri); $Filter_Time_Sat =explode("-", $E_H_Sat); //DAY OPEN CHECKS $CHECKSUN=($Filter_Time_Sun[0]=='Available') ? 'CHECKED' : ''; $CHECKMON=($Filter_Time_Mon[0]=='Available') ? 'CHECKED' : ''; $CHECKTUES=($Filter_Time_Tues[0]=='Available') ? 'CHECKED' : ''; $CHECKWENS=($Filter_Time_Wens[0]=='Available') ? 'CHECKED' : ''; $CHECKTHURS=($Filter_Time_Thurs[0]=='Available') ? 'CHECKED' : ''; $CHECKFRI=($Filter_Time_Fri[0]=='Available') ? 'CHECKED' : ''; $CHECKSAT=($Filter_Time_Sat[0]=='Available') ? 'CHECKED' : ''; } } Link to comment https://forums.phpfreaks.com/topic/125787-solved-need-help-with-loop-logic/#findComment-650589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.