Solved

need to reverse order in php for loop

Posted on 2007-11-18
4
1,139 Views
Last Modified: 2008-02-01
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
Comment
Question by:bdipasquale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20308575
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
 

Author Comment

by:bdipasquale
ID: 20308599
Im concentrating on the 2nd array, I thought it might be easier to reverse the for loop due to the sorting ???
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20308608
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 20308621
small correction because the first index of an array is zero .

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question