Jump to content

Archived

This topic is now archived and is closed to further replies.

ldsmike88

First number, last number... now fill in everything inbetween

Recommended Posts

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!

Share this post


Link to post
Share on other sites
<?php
$i = 1999;
while ($i <= 2006):
echo $i,"<br />";
$i++;
endwhile;
?>

Share this post


Link to post
Share on other sites
Another alternative is the for loop:
[code]for ($i=1999; $i<=2006; $i++) {
   echo $i . '<br />';
}[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]
$array = array(1999, 2001,2006);
$min = min($array);
$max = max($array);

$full_array = range($min, $max);
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.