Function help.

PHP coding talk.

Moderator: Moderators

Function help.

Postby Link on Thu Mar 22, 2007 10:07 pm

Hi.
I want to insert a drop down box into a function, but i want it so that a certain option is selected if their is a variable. However i can't get it to work.

Here's the code:

Code: Select all
function group_list($ulevel)
{
$sql = "SELECT * FROM groups";
if( !$result = mysql_query($sql) )
{
print" i am error";
}

$group_select = '<select name="newlevel" class="text_inputs" >';
while( $row = mysql_fetch_array($result) )
{

  $selected = ( $ulevel == $row['name'] ) ? ' selected="selected"' : ''; 
    $group_select .= '<option value="' . $row['name'] . '"' . $selected . '>' . $row['name'] . '</option>';   
}
$group_select .= '</select>';

echo $group_select;

}


The two values are Admin and Staff.

if i call it using:

Code: Select all
group_list("Staff");


it doesn't have the Staff option selected.[/code]
Link
Registered User
Registered User
 
Posts: 31
Joined: Sat Sep 10, 2005 8:43 pm

Postby UseLess on Fri Mar 23, 2007 2:47 am

Greetings,

Try;
[code start="13"]$selected = ( strtolower($ulevel) == strtolower($row['name']) ) ? ' selected="selected"' : '';[/code]
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Link on Fri Mar 23, 2007 5:38 pm

Nope, still no good.

The stand-alone code works fine if i move it out of the function.

If i view the page source the code is correct

Code: Select all
<select name="newlevel" class="text_inputs" >
<option value="admin">Admin</option>
<option value="staff" selected="selected">Staff</option>
</select>


But the admin option is selected on the form.
Link
Registered User
Registered User
 
Posts: 31
Joined: Sat Sep 10, 2005 8:43 pm

Postby UseLess on Fri Mar 23, 2007 6:09 pm

Greetings,

Try returning '$group_select' from the function and then printing it.

So line 18 would become;
[code start="18"]return $group_select;[/code]
you can then echo this with;
Code: Select all
echo group_list("Staff");
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Link on Fri Mar 23, 2007 9:33 pm

Still doesn't work...
Link
Registered User
Registered User
 
Posts: 31
Joined: Sat Sep 10, 2005 8:43 pm

Postby Gabriel® on Sat Mar 24, 2007 4:17 pm

Try this:

Code: Select all
function group_list($ulevel)
{
   // SQL Query
   $sql = "SELECT * FROM `groups`";
   $result = mysql_query($sql) or die ("Error in SQL");

   // Begin Select
   echo "<select name=\"newlevel\" class=\"text_inputs\">\n";

   while($row = mysql_fetch_array($result))
   {
      // Print Result
      $select = ($row['name'] == $ulevel) ? " selected=\"selected\"" : "";
      echo "<option value=\"{$row['name']}\"{$select}>{$row['name']}</option>\n";
   }

   // End Select
   echo '</select>';
}

group_list('Staff');


Here is ok :D
I need support about CSS... Help me?
Gabriel®
Registered User
Registered User
 
Posts: 48
Joined: Mon May 08, 2006 2:51 pm

Postby Link on Sun Mar 25, 2007 6:16 pm

Yep that seems to be working, thanks!
Link
Registered User
Registered User
 
Posts: 31
Joined: Sat Sep 10, 2005 8:43 pm


Return to PHP Programming

Who is online

Users browsing this forum: No registered users and 1 guest