dmacman Posted March 26, 2008 Share Posted March 26, 2008 Hi all, I am stumped in how to code this. What I am trying to do is to code this out so that it will give me... foreach($_SESSION['products'] as $key => $value) { if(isset($_POST['Rating[0]']) && (isset($_POST['form2_submit']))) //some more code goes here.... } in the end. Where 0 in $_POST['Rating[0]'] is replaced $key so I can increment thru my foreach and loop thru the keys and get all the POST data. But I am stuck and I tried... $_POST['Rating['.$key.']] $_POST['Rating'.[$key]] and so forth and never get a value for $POST['Rating[0]'] and the rest when it exists, so I know I am not writing it correctly in my foreach loop. Does this make sense? Thanks for the help. Don Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/ Share on other sites More sharing options...
rhodesa Posted March 26, 2008 Share Posted March 26, 2008 You are probably looking for $_POST['Rating'][0] Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-501508 Share on other sites More sharing options...
dmacman Posted March 27, 2008 Author Share Posted March 27, 2008 Yep, that was it. That solved that. But now I ended up with another issue. I have an array for this item that I want the user to rate. And I am checking to see if they picked a value or not and I am testing it, but I always get a true and its not correct. Here are the values for the array: // $key added to make the drop down dynmamic and uses the $key from the foreach loop function makeDropListKey($name,$list,$selected="",$key) { // $name select name // $list array of value, label pair // $selected selected value global $x;//for the tab index global $key;//gets this value from the foreach loop while(list($value,$label) = each($list)) { $options .= '<option value="'.$value.'">'.$label.'</option>'; } $dropList = '<select name="'.$name.'['.$key.']" tabindex="'.$x.'">'.$options.'</select>'."\n"; $dropList = ereg_replace("value=\"$selected\"","value=\"$selected\" selected",$dropList); return $dropList; } $label = array( "" => "Rate this video:", "1" => "1 Poor", "2" => "2", "3" => "3", "4" => "4", "5" => "5 Excellent"); // default for empty selected And what I am testing for is the $key, but it will always test as TRUE since there is POST data for the keys. But unless they choose an item from the menu, there wont be a $value. So how would I test for the $value dynamically instead of the $key? Here is my $key test code: foreach($_SESSION['products'] as $key => $value) { if(!isset($_POST['Rating'][$key]) && (isset($_POST['form2_submit']))) { echo '<font face="Arial" color="#FF0000" size="+1"><b>' . $name . '</b></font>'; } else { echo $name . '</td>'; } //more code goes here that does not apply (for the $value) $list = makeDropListKey('Rating',$label,$_SESSION['Rating'][$key],$key); //used makeDropListKey which adds $key to make the Rating[$key] <-- dyanamic ability to function echo $list; { Thanks, Don Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502196 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 To check the value of form field use: !empty($_POST['Rating'][$key]) Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502200 Share on other sites More sharing options...
dmacman Posted March 27, 2008 Author Share Posted March 27, 2008 I am sorry, I am not explaining myself very well. I don't think you understand what I need. I need to test the value of Rating[0] - x to ensure they chose a value for each of the drop downs, and the default for each is "" => "Rate this video:" So if I test for !empty and the $key, it would return that the $key is not empty and let them continue. And they did not pick a value for each of the drop downs. I have this in place now before my code... if(empty($_POST['Rating'][$key])) { echo '$_POST[\'Rating\']['.$key.']\'=' . $_POST['Rating'][$key] . '<br><br>'; } And if they do not select anything from the drop downs, it gives me... $_POST['Rating'][0]'= $_POST['Rating'][1]'= $_POST['Rating'][2]'= I need to test that they picked one of the values for each of the ratings. Does that make more sense now? Thanks a lot, Don Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502236 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 empty($_POST['rating'][$key]) it is not checking the key it is checking the value of the item in the rating array. Also I modified your code a bite: makeDropListkey function function makeDropListKey($name, $list, $selected='') { // $name select name // $list array of value, label pair // $selected selected value global $x; // for the tab index global $key; // gets this value from the foreach loop while(list($value, $label) = each($list)) { if($selected == $value) { $options .= '<option value="'.$value.'" selected="selected">'.$label.'</option>'; } else { $options .= '<option value="'.$value.'">'.$label.'</option>'; } } $dropList = '<select name="'.$name.'['.$key.']" tabindex="'.$x.'">'.$options.'</select>'."\n"; return $dropList; } Form processing code: if(isset($_POST['form2_submit'])) { $label = array( "" => "Rate this video:", "1" => "1 Poor", "2" => "2", "3" => "3", "4" => "4", "5" => "5 Excellent"); // default for empty selected foreach($_SESSION['products'] as $key => $value) { if(!isset($_POST['Rating'][$key]) || !empty($_POST['Rating'][$key])) { echo '<font face="Arial" color="#FF0000" size="+1"><b>' . $name . '</b></font>'; } else { echo $name . '</td>'; } //more code goes here that does not apply (for the $value) //used makeDropListKey which adds $key to make the Rating[$key] <-- dyanamic ability to function $list = makeDropListKey('Rating', $label, $_SESSION['Rating'][$key]); echo $list; } } Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502252 Share on other sites More sharing options...
dmacman Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks for the mods and code cleanup (sorry, dreamweaver [and me] sometimes do not indent correctly) I made your changes and it still did not identify when they did not pick a drop down. I played with this and got it working. I had to change it to: if(!isset($_POST['Rating'][$key]) || empty($_POST['Rating'][$key])) And now, it they do not select a video, it will tell them. Thanks for the help. I think this is resolved, unless you see something I missed? Don Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502340 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 Ohh! Another syntax error escaped passed me again, must slow down on my typing however you managed to correct it. Glad I could help. Dreamweaver is not the best tool in the box when it comes to PHP. I only use it for HTML/CSS I use a separate PHP IDE for my PHP coding. Quote Link to comment https://forums.phpfreaks.com/topic/98017-how-to-escape-text-in-a-foreach-loop/#findComment-502346 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.