Jump to content

Sorting by Year


phpbeginner

Recommended Posts

I am using the following code to sort by Year. It displays the current year fine and current year also displays in selection but does give the option for the previous years selection.

 

<form action="archivednews.php" method="post">	
<select name="year" id="year">	
<?PHP for($i=date("Y");
$i<=date("Y"); 
$i++)		  	
if($year == $i)				
echo "<option value='$i' selected>$i</option>";			
else				
echo "<option value='$i'>$i</option>";	
?>
</select>
<input type="submit" value="GO">
</form>

Link to comment
Share on other sites

what is is the value of $year... it seems like $year and $i has the same value...

if $year is '2010', then try

<form action="archivednews.php" method="post">
    <select name="year" id="year">
        <?PHP
        for($i='1986';$i<=date("Y");$i++){
            if($year == $i) {
                echo "<option value='$i' selected>$i</option>";
            }
            else {
                echo "<option value='$i'>$i</option>";
            }
        }
        ?>
    </select>
    <input type="submit" value="GO">
</form>

 

Link to comment
Share on other sites

1. That is an improper use of selected within an OPTION tag. It should be selected="selected".

 

2. That code can be written much more efficiently. I will provide some code (see below), but it would be helpful if you stated if the prior years should be fixed (should start at 1998 as ras1986 did in his example) or if it should be dynamic (e.g. should include the previous 10 years - before January it will go back to 2000, but after Jan 1st it will only go back to 2001). Also, since 2010 is the default selected value I would "assume" that the list should be sorted in reverse order (in which case you don't need to use the selected parameter)

 

3. It is good practice to break up your logic (the core PHP code) from the presentation (the HTML). So, put the logic to determine the options at the top of your script and just output the results int he HTML. This makes your code much, much easier to maintain.

 

The HTML code

<html>
<body>
<form action="archivednews.php" method="post">
    <select name="year" id="year">
    <?php echo $yearOptions; ?>
    </select>
    <input type="submit" value="GO">
</form>
</body>
</html>

 

PHP code to output from currentYear-10 to current year, autoselecting current year

<?php
$yearOptions = '';
for($currentYear=date('Y'), $year=$currentYear-10; $year<=$currentYear; $year++)
{
    $selected = ($year==$currentYear) ? ' selected="selected"' : '';
    $yearOptions .= "<option value=\"{$year}\"{$selected}>{$year}</option>\n";
}
?>

 

PHP code to output from current year to currentYear-10 (i.e. reverse order). Current year will be selected by defaults since it is the first item in the list.

<?php
$yearOptions = '';
for($year=date('Y'), $lastYear=$year-10; $year>=$lastYear; $year--)
{
    $yearOptions .= "<option value=\"{$year}\">{$year}</option>\n";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.