chestnuth Posted March 6, 2008 Share Posted March 6, 2008 I have a textbox that contains the following values (A1 A2 A3 A5). How can i loop through it to print each value to the screen on at a time? there maybe different amounts of numbers in the textbox each time. Would i use a for each? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/ Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 So, the values are in the textbox and are separated by a single space? <?php $values = explode(' ', $_POST['fieldname']); foreach ($values as $value) { echo "Value: $value<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/#findComment-485031 Share on other sites More sharing options...
chestnuth Posted March 6, 2008 Author Share Posted March 6, 2008 Yes, seperated by a space! Thanks for that!! However, when i run this code it produces this: Value: A1 Value: A2 Value: A3 Value: A5 Value: and i am left with an empty value at the end, is this to do with my code? <?php session_start(); if (isset($_SESSION['username']) == false) { header("location:login.php"); exit(); } $values = explode(' ', $_POST['thenames']); foreach ($values as $value) { echo "Value: $value<br>"; } //header("location: changecontrol.php?var=change_control.ccid desc&view=1"); //exit(); ?> thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/#findComment-485037 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2008 Share Posted March 6, 2008 Replace <?php $values = explode(' ', $_POST['thenames']); ?> with <?php $values = explode(' ', trim($_POST['thenames'])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/#findComment-485050 Share on other sites More sharing options...
chestnuth Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks guys, brill!! i now want to use the id i got from the loop so 'A1' to start with. This will allow me to pick up the contents of the text box with the same name. would i need to use something like this? echo $_post['.$value.']; or am i on the wrong lines completely? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/#findComment-485068 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2008 Share Posted March 6, 2008 Almost correct, you would use <?php echo $_POST[$value]; ?> BTW, please surround your code with the not or [blue] Ken Quote Link to comment https://forums.phpfreaks.com/topic/94737-quick-fix-loops-driving-me-loopy/#findComment-485073 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.