Jump to content

Serperating Array


phpretard

Recommended Posts

I am pulling all the counties from a table [ZipCodes] based on State values (comma seperated) from a different table [members].

 

Right now the code is working fine in terms of pulling ALL COUNTIES...but

 

I am trying to seperate the counties according to State and am having difficullty there.

 

Can some one look at this come and help me out?

 

<?

$GET_STATESsql = "SELECT * FROM members WHERE MemberID='APP-1299754007' "; 

$GET_STATESresult = mysql_query($GET_STATESsql);

while($row = mysql_fetch_array($GET_STATESresult)) { 

$States=$row['States2'];

}

$States2=explode(", ", $States);

// print_r($States2); I DONT KNOW IF THIS HELPS BUT THE PRINT IS  -- Array ( [0] => FL [1] => GA [2] => AL ) --



  foreach($States2 as $States1){
  
	$ALL_COUNTIESresult = mysql_query("SELECT DISTINCT County FROM ZipCodes WHERE State='$States1' ORDER by County"); 

	while($row = mysql_fetch_array($ALL_COUNTIESresult))
	{

	$AllCounties[] = $row['County'];

	}
	$CountyIM=implode(",", $AllCounties);
	$CountyEX=explode(",", $CountyIM);
	$CountyTotal=count($CountyEX);


	foreach ($CountyEX as $County1){

	if(stristr($States, $County1)) {

	$County3="<div style='width:24%; float:left; font-size:13px; height:25px;'><input type=checkbox name=Counties[] Value='$County1' checked> $County1</div>"; 

	} // END STRISTR

	else { 

	$County3="<div style='width:24%; float:left; font-size:13px; height:25px;'><input type=checkbox name=Counties[] Value='$County1'> $County1</div>"; 

	} // END ELSE

	echo "$County3";

	} // END FOR FOR EACH 

} // END FOR EACH STATES 2

?>

Link to comment
Share on other sites

Your code is very hard to read, so its hard to inerpuret the flow. Even the lack of detail in your explanation of the problem is not helping. What I gathered from what you said is, your searching for states by member ID, then searching for counties in that state, ok. What seams to be the problem. Or, give an example of the step by step of your code so that we can think about how we would write it out and address your problem that way. Dont just give 4 lines of text for a problem your having with an obviously complex script, especially when it comes to mysql

Link to comment
Share on other sites

the problem is it display all of the county checkboxes in a big "pile". 

 

Yes I can order the display but I need help seperating them by State.

 

I want to display all the counties in FL in a table or DIV then all the counties in GA in table or DIV and so on.

 

The closest I can get is Marking each county with it's State name.

 

I hope this helps you help me...

 

 

http://www.appraisernow.com/stateselectCOUTIES2.php  <-- THE CODE AT WORK

 

 

 

/********************** THIS CHECKS TO SEE WHAT COUNTIES ARE ALREADY LISTED  IN THE MEMBERS DB ****************************/

$EXTsql = "SELECT * FROM members WHERE MemberID='".$_SESSION['MemberID']."' ORDER by Counties"; 

$EXTresult = mysql_query($EXTsql);

while($row = mysql_fetch_array($EXTresult)) { 

$CountiesEXT=$row['Counties']; 

}



/********************** THIS GET THE STATES FROM THE MEMBERS DATABASE (COMMA SEPERATED) ****************************/


$GET_STATESsql = "SELECT * FROM members WHERE MemberID='APP-1299754007' "; 

$GET_STATESresult = mysql_query($GET_STATESsql);

while($row = mysql_fetch_array($GET_STATESresult)) { 

$States=$row['States2'];

}

$States2=explode(", ", $States);

// print_r($States2); I DONT KNOW IF THIS HELPS BUT THE PRINT IS  -- Array ( [0] => FL [1] => GA [2] => AL ) --



/******** TAKES THE STATES FROM THE MEMBERS TABLE AND QUERIES THE ZIPCODE TABLE TO FIND THE CORRISPONDING COUNTIES AND DISPLAY THEM AS CHECKBOXES *********/


  foreach($States2 as $States => $States1){
  
	$ALL_COUNTIESresult = mysql_query("SELECT DISTINCT County FROM ZipCodes WHERE State='$States1' ORDER by County"); 

	while($row = mysql_fetch_array($ALL_COUNTIESresult))
	{

	$AllCounties[] = $row['County'];

	}

	$CountyIM=implode(",", $AllCounties);
	$CountyEX=explode(",", $CountyIM);
	$CountyTotal=count($CountyEX);


	foreach ($CountyEX as $County1){

	if(stristr($CountiesEXT, $County1)) {

	$County3="<div style='width:24%; float:left; font-size:13px; height:25px;'><input type=checkbox name=Counties[] Value='$County1' checked> $County1, $States1</div>";  // AT THE TOP OF THE SCRIPT WE CHECKED FOR COUNTIES IF THEY ARE ARLEADY IN THE DB THEY'RE CHECKED  

	} // END STRISTR

	else { 

	$County3="<div style='width:24%; float:left; font-size:13px; height:25px;'><input type=checkbox name=Counties[] Value='$County1'> $County1, $States1</div>"; // (TOP OF THE SCRIPT) IF THEY ARE NOT IN THE DB THEY ARE NOT CHECKED

	} // END ELSE

	echo "$County3";  // DISPLAY CHECKBOXES

	} // END FOR FOR EACH 

  } // END FOR EACH 

Link to comment
Share on other sites

I'm not really gonna go through your code to figure out which var actually holds your state code, but assuming it's called for example $statecode, just include inside your loop something like this:

 

(pseudo code)

 

while (blahblahblah) {

  if ($currentcode != $statecode) {

   // end prev div and start new one here

  }

  $currentcode = $statecode;

  // rest of code here

}

Link to comment
Share on other sites

The idea is that you have one variable and the value of that variable is the state code for the current iteration of your loop that lists the states out.  You already have that variable somewhere in your script.  That is, whichever variable you use in your script to print out "AZ" or "TX" or "FL", that's the variable I'm talking about.  But let's assume it's called $statecode.   

 

And the goal here is that you have a list and it's saying for instance AZ AZ AZ TX .. oh damn, we hit TX, let's make a new div, right?  So the idea is that inside your loop that's listing things out, you assign $statecode to another variable, which we will call $currentcode ($currentcode = $statecode;). 

 

But before we actually do that, we are first going to compare those two variables (if ($currentcode != $statecode)).  If those two variables do not equal each other, it means that your state code has changed from AZ to TX.  We use $currentcode as a means for php to 'remember' what the previous state code was, before you had it move on to the next entry.  That's why we assign $statecode to $currentcode after the condition instead of before.  If you assign it before the condition and then run the condition, it's always going to be true, and your "make a new div" code will execute every loop iteration.  If we put it after the condition, the values will be staggered when the loop gets to a new entry.  $statecode was AZ but the next entry (next loop iteration) will now assign TX to $statecode, and then we will compare it to $currentcode, which will still be AZ, because we are comparing it before assigning the new value to it.   If they are not equal, that's where we want to make a new div. 

 

The only thing you should have to do is somewhere inside your while loop (after the $statecode var is assigned, if it's not being assigned in the loop condition itself), is drop in that code I provided before.  Just change $statecode to whatever the real variable is, and put the actual echo "</div><div>"; code inside the if statement, and you should be good to go. 

 

So...I'm really not sure what you mean by "I don't get it."  If all that typing did not somehow enlighten your status of not getting it, then perhaps you need to let me know what exactly it is you don't get?

 

Link to comment
Share on other sites

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.