Jump to content


Photo

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


  • Please log in to reply
7 replies to this topic

#1 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 29 May 2006 - 10:42 PM

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!

#2 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 29 May 2006 - 10:56 PM

<?php
$i = 1999;
while ($i <= 2006):
echo $i,"<br />";
$i++;
endwhile;
?>
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 29 May 2006 - 11:03 PM

Another alternative is the for loop:
for ($i=1999; $i<=2006; $i++) {
   echo $i . '<br />';
}

~ D Kuang

#4 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 29 May 2006 - 11:08 PM

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?

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 29 May 2006 - 11:22 PM

Can you please clarify? These "sets of numbers", what are they?

As for getting the last number, just use something like:
$last = count($array);
$last_element = $array[$last];
This assuming your array is numeric.
~ D Kuang

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 May 2006 - 12:14 AM

$array = array(1999, 2001,2006);
$min = min($array);
$max = max($array);

$full_array = range($min, $max);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 30 May 2006 - 12:15 AM

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:
<?
$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 />';
}
?>


#8 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 30 May 2006 - 12:38 AM

ok, I updated what I am going to use. Thank you everyone for your help!

Final Code Sample:
<?
$array = array(2001, 1999, 2006);
$min = min($array);
$max = max($array);
$full_array = range($min, $max);
foreach($full_array as $arrayValue){
    echo $arrayValue . '<br />';
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users