Jump to content

Recommended Posts

$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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/210164-explode-and-foreach-problems/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.