graham23s Posted November 14, 2007 Share Posted November 14, 2007 Hi Guys, i thought this was pretty simple but alas it's not working lol, i'm trying to display the users year they have selected if it matched the years in array it gets "selected" but its not for doing it code: <?php ################################################# # editcredits.php ################################################# ## edit id $editid = $_GET['id']; ## vars $years = range(1930,2007); ## grab the relevant details from mysql $queryedit = "SELECT * FROM `userscredits` WHERE `id`='$editid'"; $resultedit = mysql_query($queryedit); $row = mysql_fetch_array($resultedit); ## vars $year = $row['year']; echo $year; // 1948 echo $years; // array echo ('<br /><div style="border: 1px solid black;padding:10px; background: #e8f1fa; width: 50%; color: #000000; font-size: 12px;"><b>Edit your exhisting credit on this page.</b></div><br />'); echo ('<form action="credits.php" method="post">'); echo ("<table class=\"sub_table\" align=\"left\" width=\"500\" border=\"1\" bordercolor=\"#000000\" cellpadding=\"5\" cellspacing=\"0\">"); echo ("<tr>"); echo ("<td colspan=\"2\" class=\"header_boxes\" align=\"left\"><span class=\"prof_head\">Edit Credits</span></td>"); echo ("</tr>"); echo ("<tr>"); echo ("<td align=\"right\"><b>Year</b></td><td align=\"left\"><select name=\"year\">"); foreach ($years as $value) { echo "<option value=\"{$value}\""; if($years == $year) { echo 'selected'; } echo ">{$value}</option>"; } echo ("</select>"); echo ("</tr>"); echo ("</td>"); echo ("</table>"); ?> i can't see anything wrong, the year echoes out at 1948 fine but does't get selected in the if statement can anyone see the problem at all? cheers Graham Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 14, 2007 Share Posted November 14, 2007 You need to use the in_array() function. So change this line if($years == $year) { To if(in_array($year, $years)) { Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 14, 2007 Author Share Posted November 14, 2007 Hi Mate, really? thats weird iv used this code numerous other times with out the in_array() function , this is the first time iv had problems lol still never worked, instead of 1930 (where the range starts) it displayed 2007 where the range ends for some reason. Graham Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 14, 2007 Share Posted November 14, 2007 if($years = $year) { Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 14, 2007 Share Posted November 14, 2007 Change <?php if($years == $year) { ?> to <?php if($value == $year) { ?> Ken Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 14, 2007 Author Share Posted November 14, 2007 Hi Ken, just tried that aswell no joy, its crazy i have done this before with no problems, it even echoes out the correct year just doesn't select it. Graham Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 14, 2007 Share Posted November 14, 2007 <?php echo '<option value='.$value.''; if($value == $year) { echo 'selected'; } echo '>'.$value.'</option>';?> Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 15, 2007 Author Share Posted November 15, 2007 thanks a lot guys fixed. Graham Quote Link to comment 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.