• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1152
  • Last Modified:

need to reverse order in php for loop

the following is a for loop, i created a select box values show as
1995
1996
1997
1998

I would like it to be

1998
1997
1996
1995


what do I need to do to my code ?

$cbbYearArray = array("R","S","T","V","W","X","Y","1","2","3","4","5","6","7","8","9");
	$yearArray = array("1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009");
	
//	$startYearString = "<select name='autoYear' class=\"textboxesAutoInfo\" onchange=\"changeYear()\"><option value=''></option>";	
	$getCBByearsQueryStr = "SELECT DISTINCT(data2) FROM cbb ORDER BY data2";
//	echo $getCBByearsQueryStr;
	$getCBByearsQuery = mysql_query($getCBByearsQueryStr) or die();
	$the_strings = array();
	while($getCBByearsResult = mysql_fetch_object($getCBByearsQuery))
	{
		for($i=0; $i <=sizeof($cbbYearArray); $i++)
		{
			if($getCBByearsResult->data2 == $cbbYearArray[$i])
			{
				if($autoYear == $cbbYearArray[$i])
				{
					$the_strings[$i] = "<option value='" . $yearArray[$i] . $cbbYearArray[$i] . "' selected>" . $yearArray[$i] . "</option>" . chr(13);
				}
				else
				{
					$the_strings[$i] = "<option value='" . $yearArray[$i] . $cbbYearArray[$i] . "'>" . $yearArray[$i] . "</option>" . chr(13);
				}
				
			}
		}
	}
	$the_strings_sorted = array_unique ($the_strings) ;
	for ($i=0; $i <=sizeof($the_strings_sorted); $i++)
	{
		$cbbsStartYearString .= $the_strings_sorted[$i];
		$cbbeEndYearString .= $the_strings_sorted[$i];
	}

Open in new window

0
bdipasquale
Asked:
bdipasquale
  • 3
1 Solution
 
steelseth12Commented:
reverse your arrays

$cbbYearArray = array("R","S","T","V","W","X","Y","1","2","3","4","5","6","7","8","9");
 $yearArray = array("1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009");
            
            $yearArray = array_reverse($yearArray);
            
            $cbbYearArray  = array_reverse($cbbYearArray);
0
 
bdipasqualeAuthor Commented:
Im concentrating on the 2nd array, I thought it might be easier to reverse the for loop due to the sorting ???
0
 
steelseth12Commented:
You could do
for($i=count($cbbYearArray); $i  > 0; $i--)

From what i can see the values of 1st array are relates to the 2nd array so if you are going to reverse the array you have to reverse both of them.
0
 
steelseth12Commented:
small correction because the first index of an array is zero .

 for($i=count($cbbYearArray) -1 ; $i  >= 0; $i--)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now