ldsmike88 Posted May 29, 2006 Share Posted May 29, 2006 If I have an array of years, (such as: 1999, 2001, 2006) how would I add the years in between? In other words how would I fill in the array so it would equal 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/ Share on other sites More sharing options...
bobleny Posted May 29, 2006 Share Posted May 29, 2006 <?php$i = 1999;while ($i <= 2006): echo $i,"<br />"; $i++;endwhile;?> Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40107 Share on other sites More sharing options...
poirot Posted May 29, 2006 Share Posted May 29, 2006 Another alternative is the for loop:[code]for ($i=1999; $i<=2006; $i++) { echo $i . '<br />';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40110 Share on other sites More sharing options...
ldsmike88 Posted May 29, 2006 Author Share Posted May 29, 2006 I'm getting this array from my database for all the items on my site. Each item or row has its own set of numbers so the first and last numbers change. I can easily get the first number because I sort the array and that would make the first variable in the array the lowest number. But how would I get the last number in the array? Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40114 Share on other sites More sharing options...
poirot Posted May 29, 2006 Share Posted May 29, 2006 Can you please clarify? These "sets of numbers", what are they? As for getting the last number, just use something like:[code]$last = count($array);$last_element = $array[$last];[/code]This assuming your array is numeric. Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40120 Share on other sites More sharing options...
Barand Posted May 30, 2006 Share Posted May 30, 2006 [code]$array = array(1999, 2001,2006);$min = min($array);$max = max($array);$full_array = range($min, $max);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40125 Share on other sites More sharing options...
ldsmike88 Posted May 30, 2006 Author Share Posted May 30, 2006 Sorry, I am making a script that will write a javascript to fill in all the required information for three select menu's. The menu's are make, model, and year. I already have this working in javascript, but it takes SOOOO long to fill in every model for each make and then every year for each model. Now I have a php script that will get the information it needs from the database and write the javascript that adds the options into the select boxes. For a working example see eGrilles.com. Here is the final code I compiled from this topic:[code]<?$years = '1999, 2001, 2006';$im = explode(", ", $years);echo min($im) . ' - ' . max($im);echo '<br /><br />';for ($i=min($im); $i<=max($im); $i++) { echo $i . '<br />';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40126 Share on other sites More sharing options...
ldsmike88 Posted May 30, 2006 Author Share Posted May 30, 2006 ok, I updated what I am going to use. Thank you everyone for your help!Final Code Sample:[code]<?$array = array(2001, 1999, 2006);$min = min($array);$max = max($array);$full_array = range($min, $max);foreach($full_array as $arrayValue){ echo $arrayValue . '<br />';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10737-first-number-last-number-now-fill-in-everything-inbetween/#findComment-40129 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.