Pre-Populating Dropdown's

SpaceshipSpaceship
Watch

Hitch

Established Member
Impact
74
Hey,

Thought i'd post some code.
It's not a big piece of code, but very useful.

In some scripts, it's annoying when you are editing a MySql (For Example) Entry, and it has some dropdown menu's, lets say,

Forum Leader
-- Adrian
-- Hitch
-- xD
-- Jake

Right, so, when we edit that form, we sometimes get the Dropdown List in that order ^, though, what if we have already assigned Hitch to that forum, and we are editing the Forum Name.

Everytime we click submit, we are gonna have to check if the FL dropdown is set the user we want.

It can get very annoying, anyway, this will automatically select the current FL as the Default option...

Ok, that may have being a bit hard to understand? ^
Anyway, here's the code.

When i use terms such as user, it's meant as a example.

So, list all the users, and set the current FL as default, according to the Forum Table.
PHP:
<?php

$forum_info = mysql_query("SELECT * FROM `" . PREFIX . "forums` WHERE `id` = '$forum_id'");
$forum = mysql_fetch_array($forum_info);

$list_users = mysql_query("SELECT userid, username FROM `" . PREFIX . "users`");
while($user = $db->mysql_fetch_array($list_users))
{
	$current_fl = ($user['id'] == $forum['forum_leader']) ? 'selected="selected"' : '';
	$user_fl_list .= '<option value="' . $user['id'] . '" ' . $current_fl . '>' . $user['username'] . '</option>';
}

?>

Lets say, your Dropdown Option's arn't stored in the Database.
So, you have 4 options, and you want to Pre-Populate them?

Do the following,
PHP:
<?php

$forum_info = mysql_query("SELECT * FROM `" . PREFIX . "forums` WHERE `id` = '$forum_id'");
$forum = mysql_fetch_array($forum_info);

$status = array(
	'0' => 'Open',
	'1' => 'Loccked',
	'2' => 'Staff Only',
	'4' => 'Guest Only'
);

foreach($status AS $status_id => $status_text)
{
	$status_selected = ($forum['status'] == $status_id) ? 'selected="selected"' : '';
	
	$status_list = '<option value="' . $status_id . '"' . $status_selected . '>' . $status_text . '</option>';
}

?>

It's just a little thing to make your applications more user friendly.

I made a quick function for the Static Dropdown Menu's.
PHP:
<?php

function construct_static_menu($match, $options = array())
{
	if($match == null AND count($options) <= 0)
	{
		die('Argument mis-use for construct_static_menu()');
	}
	
	foreach($options AS $option_id => $option_text)
	{
		$option_selected = ($match == $option_id) ? 'selected="selected"' : '';
		
		$option_list = '<option value="' . $option_id . '"' . $option_selected . '>' . $option_text . '</option>';
	}
	
	return $option_list;
}

?>

Use
PHP:
<?php

$forum_info = mysql_query("SELECT * FROM `" . PREFIX . "forums` WHERE `id` = '$forum_id'");
$forum = mysql_fetch_array($forum_info);

$status = array(
	'0' => 'Open',
	'1' => 'Loccked',
	'2' => 'Staff Only',
	'4' => 'Guest Only'
);

$status_list = construct_static_menu($forum['status'], $status);

?>

Hope it helps some of you, like i said, it's not a huge thing, but it deffinately makes your applications better. ;)
Don't forget about the little things.

Oh, and i don't want any moaning about the queries, they were just examples, so no "Why not use JOINS?" :hehe:

Adrian
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Dynadot — .com TransferDynadot — .com Transfer
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back