/    Sign up×
Community /Pin to ProfileBookmark

Selecting default from dynamic select menu…

Hi,

I have the following PHP…

[code=php]
<tr>
<td><strong>Year : </strong></td>
<td><select name=”year”> <?
require_once (‘../xector.php’); // Connect to the database
$query = “SELECT id, year FROM year ORDER BY id”;
$result = mysql_query ($query); // Run the Query
while ($row = mysql_fetch_array($result)) {
echo ‘<option value=”‘ . $row[‘year’] . ‘”>’ . $row[‘year’] . ‘</option>’;
}
?> </select></td>
</tr>
[/code]

Now, I was wondering if there is a small IF statement I could perhaps place in the <option value… part. Basically saying if $row[‘year’] = $current_year then add ‘selected’ ?

Is there an easy way to do this? Of course, I’d define $current_year with date(‘Y’);

Manythanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@scragarJul 28.2008 — [code=php]<tr>
<td><strong>Year : </strong></td>
<td><select name="year"> <?
require_once ('../xector.php'); // Connect to the database
$query = "SELECT id, year FROM year ORDER BY id";
$result = mysql_query ($query); // Run the Query
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['year'] . '"'.
(($row['year']==$current_year)?' selected':'')
. '>' . $row['year'] . '</option>';
}
?> </select></td>
</tr>[/code]
Copy linkTweet thisAlerts:
@invisionauthorJul 28.2008 — Brilliant stuff scragar! That was quicker than the time it took to boil a cup of tea! ?

So the money shot is this part:
[code=php]'.(($row['year']==$current_year)?' selected':''). '[/code]

and it basically does a quick check to see if the value of that year from the db is the same as my current year value, then echo's selected?

Fantastic!
Copy linkTweet thisAlerts:
@Mr__E__CrypticJul 28.2008 — [code=php]<?php
require_once ('../xector.php'); // Connect to the database

$query = "SELECT id, year FROM year ORDER BY id";
$result = mysql_query ($query); // Run the Query

while ($row = mysql_fetch_assoc($result))
{
$selectedYear = $row['year'];
if( $selectedYear == $current_year )
{
$doSomething = 'something';
}
else
{
$doSomething = 'something Else';
}
echo '<option value="' . $doSomething . '">' . $row['year'] . '</option>';
}
?>[/code]


EDIT: Damn! I've rally gotta start checking no one else has posted before I come back to these...
Copy linkTweet thisAlerts:
@invisionauthorJul 28.2008 — Brilliant, many thanks guys. I wish to be good as you guys one day ?
Copy linkTweet thisAlerts:
@scragarJul 28.2008 — [code=php](($row['year']==$current_year)?' selected':'')[/code]
it's actualy like a mini if in a function, so it does the if part:

($row['year']==$current_year)</URL>the [b]?[/b] indicates the results to return, it's either' selected' if the result is true, or an empty string, if the result is false. the brackets around the whole just stop it from adding any more code into the comparison.

It's called the ternary operator BTW.
×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...