tgeorge06 Posted August 8, 2010 Share Posted August 8, 2010 $st = $rowcompany['states']; $statesearch = explode(',', $st); $st has already been declared via mysql query earlier in the document. What I am trying to accomplish here is when a user profile is listed as say "New York, Ohio" or however many states they wish to have on their profile, it will explode the states list that is seperated by commas. Then it should search the mysql database for each state it breaks out via explode. With this code it is stopping at New York and not showing Ohio results in the select list options. <?php foreach($statesearch as $s){ echo "You Searched $s "; } echo "<form action='phpforms/updatecounties.php' method='POST'>"; echo "<select name='counties[]' MULTIPLE SIZE=5>"; foreach($statesearch as $s){ //Connect to the database include_once 'phpforms/connect.php'; $sql = mysql_query("SELECT * FROM counties WHERE state LIKE '$s'"); while($row = mysql_fetch_array($sql)){ $counties = $row['counties']; echo "<option value='$counties'>$counties</option>"; }//End While }//End Foreach echo "</select>"; echo "<input type='submit' name='button' value='Update The Counties You Cover'>"; echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210164-explode-and-foreach-problems/ Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 Add this line: echo '<pre>'; print_r($statesearch); echo '</pre>'; Right after these lines: $st = $rowcompany['states']; $statesearch = explode(',', $st); And copy/paste what is printed to the browser window. Quote Link to comment https://forums.phpfreaks.com/topic/210164-explode-and-foreach-problems/#findComment-1096826 Share on other sites More sharing options...
JasonLewis Posted August 9, 2010 Share Posted August 9, 2010 Re-arrange the code, move the include_once() outside of the foreach, so your not trying to include it over and over. Also, it may not be matching Ohio because of extra whitespace due to your explode. If you're storing states in the database how you've said: New York, Ohio Then you have a space between the comma and the next state. When you explode, you're array will look like this: array(2) { [0]=> string( "New York" [1]=> string(5) " Ohio" } Ohio has whitespace at the start. You can get around this by using array_map. Example: $states = "New York, Ohio"; $explode = array_map('trim', explode(',', $states)); That will trim all extra whitespace from any of the elements after the explode. Also take a look at the MySQL Wildcards. Quote Link to comment https://forums.phpfreaks.com/topic/210164-explode-and-foreach-problems/#findComment-1096831 Share on other sites More sharing options...
tgeorge06 Posted August 9, 2010 Author Share Posted August 9, 2010 Thank you both of you for the help. Cleaning up my code solved the problem. Trimming white space and also using the wild cards for the mysql query. I can't believe I forgot to use wildcards, I kind of feel dumb. Anyways, it works as I thought it should have worked the first time now. I appreciate the help and sorry for the belated reply with results. Quote Link to comment https://forums.phpfreaks.com/topic/210164-explode-and-foreach-problems/#findComment-1096986 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.