grunshaw Posted August 8, 2009 Share Posted August 8, 2009 Hi Guys, Here is the scope. I am writing a mobility system for the visually impaired that will store information about how to get from one place to another in several steps. The number of steps changes depending on where the route goes. I've stored these into a database. The 2 columns we are interested in are "steps" which contains the number of steps in the route, and "description" which is the textual description of the steps to take, where a step is defined as a section of the route. In the description field, the data is stored seperated by a comma. For example, Step 1 - Go here, then turn left, Step 2 - Then go straight, Step 3 - Your now there. Since we know the number of steps is 3, the steps column in the databsse will contain 3. What we do not know is the number of commas and where to split. What I want to do is to be able to split at the commas and store these in variables so I can format them into an ordered list. Is there a way to loop through this and split at the commas and also the dashes without messing up the order of everything and then being able to make everything into bullet points even if there are 20 steps or 100 steps? Thanks for your help. James Grunshaw. Quote Link to comment https://forums.phpfreaks.com/topic/169336-solved-create-loop-and-split-string-into-x-variables/ Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 You should seperate your steps with a different character, such as |*| Your steps will be like so tep 1 - Go here, then turn left|*|Step 2 - Then go straight|*|Step 3 - Your now there This allow you to split your steps in to an array easily $steps = explode('|*|', $row['steps']); echo 'Steps to take: <ul>'; foreach($steps as $step) { echo '<li>' . $step . '</li>'; } echo '</ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/169336-solved-create-loop-and-split-string-into-x-variables/#findComment-893561 Share on other sites More sharing options...
grunshaw Posted August 8, 2009 Author Share Posted August 8, 2009 Excellant, thats working a bit better for what I wanted. I just have one more thing. Is it possible to split at the comma aswell so in affect you would have say a nested list? Step 1 Go here Then here Step 2 Then do this Then that Step 3 Finish Thanks again. PS > I'm Visually Impaired, having a very difficult time with these CAPATCHAS - are they really neccesary on an already authenticated and registered system? Quote Link to comment https://forums.phpfreaks.com/topic/169336-solved-create-loop-and-split-string-into-x-variables/#findComment-893591 Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 Excellant, thats working a bit better for what I wanted. I just have one more thing. Is it possible to split at the comma aswell so in affect you would have say a nested list? <?php $row['steps'] = 'Step 1 - Go here, then turn left|*|Step 2 - Then go straight, turn right at next road|*|Step 3 - Your now there'; $route_steps = explode('|*|', $row['steps']); echo "Steps to take: <ul>\n"; foreach($route_steps as $steps) { list($step_num, $steps) = explode(' - ', $steps); $sub_steps = explode(', ', $steps); echo ' <li>' . $step_num . ' <ul> <li>' . implode("</li>\n <li>", $sub_steps) . '</li> </ul> </li>'."\n"; } echo '</ul>'; ?> PS > I'm Visually Impaired, having a very difficult time with these CAPATCHAS - are they really neccesary on an already authenticated and registered system? As you have less than 10 posts you are required to full in the Captcha. Once you have reached 10 posts or more it will not display. Quote Link to comment https://forums.phpfreaks.com/topic/169336-solved-create-loop-and-split-string-into-x-variables/#findComment-893605 Share on other sites More sharing options...
grunshaw Posted August 8, 2009 Author Share Posted August 8, 2009 That is Awesome! It worked perfectly! Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/169336-solved-create-loop-and-split-string-into-x-variables/#findComment-893613 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.