NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Pre-Populating Dropdown's

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 04-21-2007, 09:24 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,225
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!

Pre-Populating Dropdown's


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 Code:
<?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 Code:
<?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 Code:
<?php

function construct_static_menu($match$options = array())
{
    if(
$match == null AND count($options) <= 0)
    {
        die(
'Argument mis-use for construct_static_menu()');
????: NamePros.com http://www.namepros.com/code/319272-pre-populating-dropdowns.html
    }
    
    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>';
????: NamePros.com http://www.namepros.com/showthread.php?t=319272
    }
    
    return 
$option_list;
}

?>
Use
PHP Code:
<?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?"

Adrian
__________________
Cueburst - Support System - FAQ Knowledge Base, Tickets and more.
Hitch is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 01:01 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger