I can't see where I'm going wrong!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

I can't see where I'm going wrong!

Post by tweaker »

I have a small code that extracts from a mysql database (successfully) but it does not automatically use the $status = "selected"; that I have prorgammed to show that the field that is selected is the one I want to show up first. I still want the rest of them of course but I can't see where this code is wrong.

Code: Select all

//Drop Down Box
function get_forums($sel_id)
{
    global $db, $HTTP_GET_VARS, $sel_id;

   $sql = "SELECT forum_id, forum_name
      FROM " . FORUMS_TABLE;

   if( !$result = $db->sql_query($sql) )
   {
      message_die(GENERAL_ERROR, "Couldn't get list of forums from Forums Table", "", __LINE__, __FILE__, $sql);
   }

   $forumlist = '<select name="forum_id">';

   while( $row = $db->sql_fetchrow($result) )
   {
      if ( $sel_id == $row['forum_id'] )
      {
          $status = "selected";
      }
      else
      {
          $status = '';
      }
      $forumlist .= '<option value="' .$row['forum_id'] . '" ' . $status . '>' . $row['forum_name'] . '</option>';
   }

   $forumlist .= '</select>';

   return $forumlist;
}
// End Drop Down Box
Again, the list shows up, just not the one that's selected in the database. Like say forum_id = 5 when I go edit it will list forum_id = 1 as the selected one. That's not what I think I have programmed.

Does anyone see where I could have possibly gone astray?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how are you calling this function?

remove the global reference to $sel_id and pass the id in.
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

Post by tweaker »

So instead of $sed_id try the $forum_id? I'll try and let you know.

That did not change anything. I made the statement as follows:

Code: Select all

if ( $forum_id == $row['forum_id'] )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That little of code isn't going to help me help you. I need to see the lines around (say the 20 previous lines) and including the line that calls this function.
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

Post by tweaker »

Here is the code starting from the function edit

Code: Select all

if( $mode != "" )
{
   if( $mode == "edit" || $mode == "add" )
   {

$cat_id = ( isset($HTTP_GET_VARS['id']) ) ? $HTTP_GET_VARS['id'] : 0;

		$template->set_filenames(array(
			"body" => "admin/FTP_categories_edit.tpl")
		);

		$s_hidden_fields = '';

		if( $mode == "edit" )
		{
			if( $cat_id )
			{
				$sql = "SELECT *
					FROM " . FTP_CATEGORIES_TABLE . "
					WHERE cat_id = $cat_id";
				if(!$result = $db->sql_query($sql))
				{
					message_die(GENERAL_ERROR, "Could not query database.", "Error", __LINE__, __FILE__, $sql);
				}

				$cat_info = $db->sql_fetchrow($result);
				$s_hidden_fields .= '<input type="hidden" name="id" value="' . $cat_id . '" />';
			}
			else
			{
				message_die(GENERAL_MESSAGE, "You must choose category");
			}
		}

$sel_id = $cat_info['forum_id'];

//Drop Down Box
function get_forums($sel_id)
{
    global $db, $HTTP_GET_VARS, $sel_id;

   $sql = "SELECT forum_id, forum_name
      FROM " . FORUMS_TABLE;
   
   if( !$result = $db->sql_query($sql) )
   {
      message_die(GENERAL_ERROR, "Couldn't get list of forums from Forums Table", "", __LINE__, __FILE__, $sql);
   }

   $forumlist = '<select name="forum_id">';

   while( $row = $db->sql_fetchrow($result) )
   {
      if ( $forum_id == $row['forum_id'] )
      {
          $status = "selected";
      }
      else
      {
          $status = '';
      }
      $forumlist .= '<option value="' .$row['forum_id'] . '" ' . $status . '>' . $row['forum_name'] . '</option>';
   }

   $forumlist .= '</select>';
   
   return $forumlist;
}
// End Drop Down Box

$sel_id = $cat_info['group_id'];

//Group Drop Down
function get_groups($sel_id)
{
 	global $db, $sel_id;

	$sql = "SELECT group_name, group_id
		FROM " . GROUPS_TABLE . "
		WHERE group_id = group_id
			AND group_single_user = 0";
	
	
	if( !$result = $db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "Couldn't get list of groups", "", __LINE__, __FILE__, $sql);
	}

	$grouplist = '<select name="group_id"><option>No Group Selected</option>';

	while( $row = $db->sql_fetchrow($result) )
	{
		if ( $sel_id == $row['group_id'] )
		{
		    $status = "selected";
		}
		else
		{
		    $status = '';
		}
		$grouplist .= '<option value="' .$row['group_id'] . '" ' . $status . '>' . $row['group_name'] . '</option>';
	}

	$grouplist .= '</select>';
	
	return $grouplist;
}
//END Group Drop Down

$user_text_yes = ( $cat_info['user_text'] ) ? "checked=\"checked\"" : "";
$user_text_no = ( !$cat_info['user_text'] ) ? "checked=\"checked\"" : "";

$auth_reply_yes = ( $cat_info['auth_reply'] ) ? "checked=\"checked\"" : "";
$auth_reply_no = ( !$cat_info['auth_reply'] ) ? "checked=\"checked\"" : "";

$auth_none = ( $cat_info['auth'] == 0 ) ? "checked=\"checked\"" : "";
$auth_registered = ( $cat_info['auth'] == 1 ) ? "checked=\"checked\"" : "";
$auth_group = ( $cat_info['auth'] == 2 ) ? "checked=\"checked\"" : "";

$forums = get_forums($new['forum_id']);
$groups = get_groups($new['group_id']);

		$template->assign_vars(array(
			"FORUM_ID" => $cat_info['forum_id'],
			"CAT_TITLE" => $cat_info['cat_title'],
			"SUBJECT_NAME" => $cat_info['subject_name'],
			"SUBJECT_DESC" => $cat_info['subject_desc'],
			"NOTICE" => $cat_info['notice'],
			"FORUMS" => $forums,
			"GROUPS" => $groups,
			"USER_ID" => $cat_info['user_id'],
			"POST_NOTICE" => $cat_info['post_notice'],

			"L_ADD_CAT" => $lang['add_cat'],
			"L_CAT_NAME" => $lang['cat_name'],
			"L_ADD_SUBJECT" => $lang['add_subject'],
			"L_ADD_SUBJECT_DESC" => $lang['add_subject_desc'],
			"L_ADD_NOTICE_DESC" => $lang['add_notice_desc'],
			"L_NOTICE" => $lang['notice_title'],
			"L_WHICH_FORUM" => $lang['which_forum'],
			"L_WHICH_FORUM_DESC" => $lang['which_forum_desc'],
			"L_USER" => $lang['user'],
			"L_USER_NAME_USE" => $lang['add_user_desc'],
			"L_SPECIFIC_USER" => $lang['specific_user'],
			"L_SPECIFIC_USER_DESC" => $lang['specific_user_desc'],
			"L_SAVE" => $lang['save'],
			"L_POST_NOTICE" => $lang['post_notice'],
			"L_POST_NOTICE_EXAMPLE" => $lang['post_notice_example'],
			"L_AUTH" => $lang['ftp_auth'],
			"L_AUTH_EXPLAIN" => $lang['ftp_auth_explain'],
			"L_GROUP" => $lang['group'],
			"L_GROUP_DESCRIPTION2" => $lang['group_description2'],
			"L_AUTH_REPLY" => $lang['auth_reply'],
			"L_AUTH_REPLY_EXPLAIN" => $lang['auth_reply_explain'],
			"L_GROUP" => $lang['group'],
			"L_REGISTERED" => $lang['registered'],
			"L_GUEST" => $lang['guest'],
			"L_YES" => $lang['Yes'],
			"L_NO" => $lang['No'],

			"S_USER_TEXT_YES" => $user_text_yes, 
			"S_USER_TEXT_NO" => $user_text_no, 

			"S_AUTH_REPLY_YES" => $auth_reply_yes, 
			"S_AUTH_REPLY_NO" => $auth_reply_no, 

			"S_AUTH_NONE" => $auth_none,
			"S_AUTH_REGISTERED" => $auth_registered,
			"S_AUTH_GROUP" => $auth_group,

			"S_WORDS_ACTION" => append_sid("admin_FTP_categories.$phpEx"),
			"S_HIDDEN_FIELDS" => $s_hidden_fields)
		);

		$template->pparse("body");

		include('./page_footer_admin.'.$phpEx);
	}
	else if( $mode == "save" )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it appears $cat_info['forum_id'] may be what you are after.
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

Post by tweaker »

I'm afraid I don't understand exactly where you're refering too.

Can you show me the $cat_info['forum_id'] to which you mean?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$auth_none = ( $cat_info['auth'] == 0 ) ? "checked=\"checked\"" : "";
$auth_registered = ( $cat_info['auth'] == 1 ) ? "checked=\"checked\"" : "";
$auth_group = ( $cat_info['auth'] == 2 ) ? "checked=\"checked\"" : "";

$forums = get_forums($new['forum_id']); /// <-------
$groups = get_groups($new['group_id']);

        $template->assign_vars(array(
            "FORUM_ID" => $cat_info['forum_id'],
            "CAT_TITLE" => $cat_info['cat_title'],
            "SUBJECT_NAME" => $cat_info['subject_name'],
$new is not created in the code posted, therefore I can only assume it doesn't exist or is created elsewhere.

Code: Select all

message_die(GENERAL_MESSAGE, "You must choose category");
            }
        }

$sel_id = $cat_info['forum_id']; /// <--------------

//Drop Down Box
function get_forums($sel_id)
{
    global $db, $HTTP_GET_VARS, $sel_id;
It would appear that the intention was to call get_forums() with $sel_id. The $sel_id which was assigned the value in $cat_info['forum_id'] by code above.
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

Post by tweaker »

The full linkage is admin_FTP_categories.php?mode=edit&id=1. That should pull from my FTP table the fields and go from there. It's just something is preventing it from recognizing that "Which Forum" or [forum_id] has a value to it and thus making it selected. But you're right in that the code I have for

Code: Select all

$status = "selected"
is asking from the

Code: Select all

if ( $forum_id == $row['forum_id'] )
then I'd have to question the SQL query, shouldn't I? Cause it's asking what $forum_id == isn't it?
tweaker
Forum Newbie
Posts: 10
Joined: Wed Feb 08, 2006 1:09 pm
Contact:

Post by tweaker »

This has gotten me all confused because I half understand what you're saying or refering to. I see fact that $sel_id because = $cat_infp['forum_id']; but is that what is preventing it from determining which one to select because I thought that was handled above that.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

One part of the confusion may be the spagetti code. Start with some refactoring to separate presentation code from your database stuff. Maybe a function like this would help:

Code: Select all

function html_form_select($name, $options, $valuekey='id', $labelkey='name', $sel_id='')
{
    $html = "<select name=\"$name\">\n";

    foreach ($options as $option)
    {
        if ( $sel_id == $option[$valuekey] )
        {
            $status = ' selected="selected"';
        }
        else
        {
            $status = '';
        }
        $html .= '<option value="' . $option[$valuekey] . '"' . $status . '>' . $option[$labelkey] . "</option>\n";
    }

    $html .= "</select>\n";
    
    return $html;
}

$options = array(
			0 => array (
				'id' => 0,
				'name' => 'zero',
				),
			1 => array (
				'id' => 1,
				'name' => 'one',
				),
			2 => array (
				'id' => 2,
				'name' => 'two',
				),
			3 => array (
				'id' => 3,
				'name' => 'three',
				),
			4 => array (
				'id' => 4,
				'name' => 'four',
				),
			);
echo html_form_select('group_id', $options, 'id', 'name', 3);

echo html_form_select('forum_id', $options, 'id', 'name', 1);
(#10850)
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Does your page source print anything like:

Code: Select all

<option value="1" selected>1</option>
Or are all your <option> tags showing up without anything actually "selected"?

For testing add your $sel_id to your printed option name and see what the value is. Like others i would suspect your $sel_id value.

Code: Select all

while( $row = $db->sql_fetchrow($result) ) 
   { 
      if ( $sel_id == $row['forum_id'] ) 
      { 
          $status = "selected";
      } 
      else 
      { 
          $status = '';
      } 
      $forumlist .= '<option value="' .$row['forum_id'] . '" ' . $status . '>' .$sel_id.'   '.$row['forum_name'] . '</option>';   /// add $sel_id
   }
Post Reply