PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Oct 16, 2018 8:29 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Oct 27, 2003 2:38 pm 
Offline
Forum Regular
User avatar

Joined: Fri May 09, 2003 11:53 pm
Posts: 589
Location: Belo Horizonte, Brazil
Syntax: [ Download ] [ Hide ]
<?php

/**

 * @author Scorphus

 * @return string

 * @param $optArray Array

 * @param $selName string

 * @param $selected string

 * @param $addAttrib string

 * @desc Return a string containing the HTML code of a select/options tags

 * according to $optArray's keys and values.

 */


function printSelect ($optArray, $selName = '', $selected = '', $addAttrib = '') {

        $conteudo = '';

        if (!empty($selName))

                $conteudo .= "<select name="$selName" $addAttrib>\n";

        foreach ($optArray as $key => $value)

                $conteudo .= "\t<option label="$key" value="$key"" . ($key == $selected ? ' selected' : '') . ">&nbsp;$value&nbsp;</option>\n";

        if (!empty($selName))

                $conteudo .= "</select>\n";

        return $conteudo;

}

?>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 2:42 pm 
Offline
Forum Regular
User avatar

Joined: Fri May 09, 2003 11:53 pm
Posts: 589
Location: Belo Horizonte, Brazil
This is one of the many functions of my very large funcbase.php file. I decided to share some of them with the community. This is the first.

Examples of usage:
Syntax: [ Download ] [ Hide ]
<?php

$opt = array(

        'opt1' => 'This is the option #1',

        'opt2' => 'This is the option #2',

        'opt3' => 'This is the option #3',

        'opt4' => 'This is the option #4',

        'opt5' => 'This is the option #5'

);



echo "Just print the options tags in case the select tag is already present:\n";

echo printSelect($opt), "\n";



echo "Print the select tag and the options tag inside it:\n";

echo printSelect($opt, 'options'), "\n";



echo "Now we say which option is selected by default:\n";

next($opt);

echo printSelect($opt, 'options', key($opt)), "\n";



echo "Now take the form information to tell which option is default, and also an additional attribute (onChange for a jump menu):\n";

next($opt);

$_POST['options'] = key($opt);

echo printSelect($opt, 'options', $_POST['options'], 'onChange="javascript:document.form1.submit();"'), "\n";

?>

output:
Syntax: [ Download ] [ Hide ]
Just print the options tags in case the select tag is already present:
        &amp;lt;option label="opt1" value="opt1"&amp;gt;&amp;amp;nbsp;This is the option #1&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt2" value="opt2"&amp;gt;&amp;amp;nbsp;This is the option #2&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt3" value="opt3"&amp;gt;&amp;amp;nbsp;This is the option #3&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt4" value="opt4"&amp;gt;&amp;amp;nbsp;This is the option #4&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt5" value="opt5"&amp;gt;&amp;amp;nbsp;This is the option #5&amp;amp;nbsp;&amp;lt;/option&amp;gt;

Print the select tag and the options tag inside it:
&amp;lt;select name="options" &amp;gt;
        &amp;lt;option label="opt1" value="opt1"&amp;gt;&amp;amp;nbsp;This is the option #1&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt2" value="opt2"&amp;gt;&amp;amp;nbsp;This is the option #2&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt3" value="opt3"&amp;gt;&amp;amp;nbsp;This is the option #3&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt4" value="opt4"&amp;gt;&amp;amp;nbsp;This is the option #4&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt5" value="opt5"&amp;gt;&amp;amp;nbsp;This is the option #5&amp;amp;nbsp;&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;

Now we say which option is selected by default:
&amp;lt;select name="options" &amp;gt;
        &amp;lt;option label="opt1" value="opt1" selected&amp;gt;&amp;amp;nbsp;This is the option #1&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt2" value="opt2"&amp;gt;&amp;amp;nbsp;This is the option #2&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt3" value="opt3"&amp;gt;&amp;amp;nbsp;This is the option #3&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt4" value="opt4"&amp;gt;&amp;amp;nbsp;This is the option #4&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt5" value="opt5"&amp;gt;&amp;amp;nbsp;This is the option #5&amp;amp;nbsp;&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;

Now take the form information to tell which option is default, and also an additional attribute (onChange for a jump menu):
&amp;lt;select name="options" onChange="javascript:document.form1.submit();"&amp;gt;
        &amp;lt;option label="opt1" value="opt1"&amp;gt;&amp;amp;nbsp;This is the option #1&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt2" value="opt2" selected&amp;gt;&amp;amp;nbsp;This is the option #2&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt3" value="opt3"&amp;gt;&amp;amp;nbsp;This is the option #3&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt4" value="opt4"&amp;gt;&amp;amp;nbsp;This is the option #4&amp;amp;nbsp;&amp;lt;/option&amp;gt;
        &amp;lt;option label="opt5" value="opt5"&amp;gt;&amp;amp;nbsp;This is the option #5&amp;amp;nbsp;&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;

Hope it's gonna be useful for you.

Regards,
Scorphus.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group