For Statment

Discussion forum for MOD Writers regarding MOD Development.
Locked
cheechm
Registered User
Posts: 69
Joined: Mon Jan 15, 2007 10:28 pm

For Statment

Post by cheechm » Sat May 10, 2008 12:44 am

How do I display this in a template@

Code: Select all

     $sql = 'SELECT *
			FROM site_menu
			ORDER BY `order` ASC';
        $result = $db->sql_query($sql);

        $num = mysql_num_rows($result);
        
             for ($n = 1; $n<$num; $n++)
            {
            	$iorder = '<select name="order">';
            	$iorder .= '<option value="'. $n .'">'. $n .'</option></select>';
            	$template->assign_vars(array('SL_ORDER' => $iorder));
            	            	}

Code: Select all

<td style="text-align: center;">{SL_ORDER}</td>
I want it so output this:
Option--1
Option--2
Option--3
but it doesn't work.

Thanks

User avatar
Highway of Life
Former Team Member
Posts: 6048
Joined: Wed Feb 02, 2005 5:41 pm
Location: Seattle, WA
Name: David Lewis
Contact:

Re: For Statment

Post by Highway of Life » Sat May 10, 2008 1:40 am

There are two ways of doing that...

Example #1, the best method:

Code: Select all

$sql = 'SELECT *
        FROM ' . SOME_TABLE . "
        WHERE some_field = '" . $db->sql_escape($some_value) . "'";
$result = $db->sql_query($sql);

$s_option_list = '';
$i = 1;

while ($row = $db->sql_fetchrow($result))
{
    $s_option_list .= '<option value="' . $i . '">' . $row['some_field'] . '</option>';
    $i++;
}
$db->sql_freeresult($result);

$template->assign_var('S_OPTION_LIST', $s_option_list); 
Template:

Code: Select all

<select name="option_list" id="option_list">{S_OPTION_LIST}</select>
Example #2:

Code: Select all

$sql = 'SELECT *
        FROM ' . SOME_TABLE . "
        WHERE some_field = '" . $db->sql_escape($some_value) . "'";
$result = $db->sql_query($sql);

$s_option_list = '<select name="option_list" id="option_list">';
$i = 1;

while ($row = $db->sql_fetchrow($result))
{
    $s_option_list .= '<option value="' . $i . '">' . $row['some_field'] . '</option>';
    $i++;
}
$db->sql_freeresult($result);
$s_option_list .= '</select>';

$template->assign_var('S_OPTION_LIST', $s_option_list); 
Template:

Code: Select all

{S_OPTION_LIST}
Note that we correctly use $db->sql_fetchrow(), we use TABLE_CONSTANT for the table name, and we use a while loop instead of of a for loop for a database call.
In the first example, you're letting the template control the select box, as you may want different boxes containing the same data, in the second example, you would use that if you knew that no other box on a single page would contain that drop-down.
Since we are assigning only asingle var, we use assign_var() instead of assign_vars(array());
The phpBB Weekly Podcast - Discussing the developments of phpBB4 and beyond.

New to phpBB3? Want to learn about programing?
Visit phpBB Academy at StarTrekGuide to learn how.

User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: For Statment

Post by igorw » Sat May 10, 2008 10:34 am

The first method pointed out by highway is prefered though, as it gives style authors more flexibility for styling the <select>.
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three

User avatar
3Di
Former Team Member
Posts: 14348
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: For Statment

Post by 3Di » Sat May 10, 2008 10:36 am

Yes, i preferred the first though till now, maybe I have now to re-consider my though, dunno. :)
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Highway of Life
Former Team Member
Posts: 6048
Joined: Wed Feb 02, 2005 5:41 pm
Location: Seattle, WA
Name: David Lewis
Contact:

Re: For Statment

Post by Highway of Life » Sat May 10, 2008 6:07 pm

I wouldn't. I would personally avoid option #2, and leave it up to the template to decide the styling of the select.
The phpBB Weekly Podcast - Discussing the developments of phpBB4 and beyond.

New to phpBB3? Want to learn about programing?
Visit phpBB Academy at StarTrekGuide to learn how.

Locked

Return to “[3.0.x] MOD Writers Discussion”