Jump to content

An option problem


FifeBirder

Recommended Posts

Evening People

 

I have this drop downbox on a HTML page, it is populated from a PHP page, if the user makes a selection i want that selection to be retained once the page refreshed.

 

What i have is

 

		while ($row = $db->sql_fetchrow($result))
		{
		     if(isset($_GET["REGION_FILTER"]) AND $_GET["REGION_FILTER"] = $row['region'] AND strlen($_GET["REGION_FILTER"]) > 0)
   				 //if($region_filter = $row['region'] AND strlen($region_filter) > 0)
                {
			$region_filter_options .= '<option value="' . $row['region'] . '" SELECTED>' . $row['region'] . '</option>';
                }
                else
                {
                $region_filter_options .= '<option value="' . $row['region']. '">' . $row['region'] . '</option>';
                   
                }
               
		}

 

REGION_FILTER is the ID of the option box

 

The drop down box is populated from the database but i cant seem to get the selected bit working. Can someone help identify what i have done wrong ?

 

regards

 

Link to comment
Share on other sites

This is the code on the HTML page

 

         
<form action="alertz_VIP.php" method="GET">

<select name=REGION_FILTER size="1" id=REGION_FILTER onchange="this.form.submit()">
          <option value="">Select </option>
            <option value={REGION_FILTER}></option>
          </select>

 

As you can see changing the dropdown option results in the page being submitted. The Alertz_VIP.php page populates the {REGION_FILTER} Field if that makes sense

Link to comment
Share on other sites

You need to intercept that value on the new page with $_GET, give it a variable name, and echo it out in the select box as SELECTED  on the new page:

 

<SELECT NAME="partnumber">
<OPTION VALUE="7382"          >steam turbine
<OPTION VALUE="2928"          >resistor array
<OPTION VALUE="3993" SELECTED >widget analyzer
<OPTION VALUE="9398"          >fiber identifier
</SELECT>

Link to comment
Share on other sites

I have read that artcle somewhere and that is why i wrote this

 

if(isset($_GET["REGION_FILTER"]) AND $_GET["REGION_FILTER"] = $row['region'] AND strlen($_GET["REGION_FILTER"]) > 0)
   
//if($region_filter = $row['region'] AND strlen($region_filter) > 0)
                {
                    $region_filter_options .= '<option value="' . $row['region'] . '" SELECTED>' . $row['region'] . '</option>';
                }
                else
                {
                    $region_filter_options .= '<option value="' . $row['region']. '">' . $row['region'] . '</option>';
                   
                }

 

if i am correct this translates to

 

if(isset($_GET["REGION_FILTER"]) AND $_GET["REGION_FILTER"] = $row['region'] AND strlen($_GET["REGION_FILTER"]) > 0)

 

if REGION_FILTER is set and REGION_FILTER = field.Region and the Length of REGION_FILTER >0 then

 

$region_filter_options .= '<option value="' . $row['region'] . '" SELECTED>' . $row['region'] . '</option>';

 

let region_filter_options = <option value= " field.Region " SELECTED>  field.Region </option>';

 

this should only be done once a match of value selected is found in field.region.

 

else this gets done

 

$region_filter_options .= '<option value="' . $row['region']. '">' . $row['region'] . '</option>';

  ( Which is non SELECTED ) for all other field.region values

 

Does that make sense or have i got this completely wrong

 

Link to comment
Share on other sites

Well, the problem is that you are doing an ASSIGNMENT in the IF condition and not a COMPARISON (i.e. a single equal sign instead of a double equal sign).

 

But, you are making the logic in that IF statemetn way more complicated than it needs to be. You don't need to test if the value has a string length > 0 since you are already testing if it is equal to one of the available options.

 

Also, I would advise against having to completely different lines of code for creating the options. It will only lead to problems down the road. Here is a more elegant solution.

while ($row = $db->sql_fetchrow($result))
{
    $selected = (isset($_GET['REGION_FILTER']) AND $_GET['REGION_FILTER']==$row['region']) ? ' selected="selected"' : '' ;
    $region_filter_options .= "<option value=\"{$row['region']}{$selected}>{$row['region']}</option>\n";
}

Link to comment
Share on other sites

mjdamato

 

Working alot better, stable drop downs, but what i am finding is that the drop down box is not showing all selections, it is as if it is missing every second one if that makes sense.

 

The full Alertz_VIP.php is :-

 

<?php
/***************************************************************************
*                                Alertz.php
*                            -------------------
*
***************************************************************************/

define('IN_ICYPHOENIX', true); 

if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1)); 

// Include files 
include(IP_ROOT_PATH . 'common.' . PHP_EXT);
include_once(IP_ROOT_PATH . 'includes/functions_groups.' . PHP_EXT);
include(IP_ROOT_PATH . '/alerts/alertsconfig.'.PHP_EXT);

// Page Authorise
$cms_page_id = 'scotalertz';
$cms_page_nav = (!empty($cms_config_layouts[$cms_page_id]['page_nav']) ? true : false);
$cms_global_blocks = (!empty($cms_config_layouts[$cms_page_id]['global_blocks']) ? true : false);
$cms_auth_level = (isset($cms_config_layouts[$cms_page_id]['view']) ? $cms_config_layouts[$cms_page_id]['view'] : AUTH_ALL);
check_page_auth($cms_page_id, $cms_auth_level);

// Obtain Select Criteria
$temp_region_filter = $_GET["REGION_FILTER"];
$temp_species_filter = $_GET["SPECIES_FILTER"];
$temp_date_filter = $_GET["DATE_FILTER"];

// Filter characters if required
$region_filter =str_replace(" & ", "&", $temp_region_filter);
$species_filter = $temp_species_filter;
$date_filter = $temp_date_filter;



// standard session management
$userdata = session_pagestart($user_ip);
// Check to see if user is logged in  
if ((!$userdata['session_logged_in']) ) 
// No he isnt
   {
        redirect(append_sid(LOGIN_MG . '?redirect=alerts.' . PHP_EXT));

   }

else
// Yes they are
   {
	init_userprefs($userdata);
	// set page title
	$page_title = "ScotBird alerts - VIP's ONLY !! ";
	// standard page header
	include(IP_ROOT_PATH . 'includes/page_header.'.PHP_EXT);
	// Connect to the database 
	$db = new sql_db($alerts_mysql_host,$alerts_mysql_username,$alerts_mysql_password,$alerts_mysql_db,false);
     	if(!$db) 
	{
        	die("Database Connection Failed:- Please Contact Site Admin" . mysql_error());
    	}
	$template->set_filenames(array('body' => 'alertz_VIP.tpl'));
// Some consistant Variables
$year =date("Y");
$month = date("m");
$day = date("d");
       
        function datedropdown_funct($switchstring)
        {
            $year =date("Y");
            $month = date("m");
            $day = date("d");
                    switch ($switchstring) 
               {
                    
			    case "Today":
                        $day = date("d")-1;
				    $retval = '<option value="">Select</option><option value="Today" SELECTED>Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option>';
                    break;
			    case "Last 48hrs":
				   $day = date("d")-2;
				   $retval = '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs" SELECTED>Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option>';
                    break;	
			    case "Last week":
    					$day = date("d")-8;
    					$retval =  '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week" SELECTED>Last Week</option><option value="last month">Last Month</option>';
                	break;
    				case "last month":
    					$month = date("m")-2;
    					$retval =  '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month" SELECTED>Last Month</option>';
                    break;
    				case "last 3 months":
    					$month = date("m")-4;
    					$retval = '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option><option value="last 3 months" SELECTED>Last 3 Month</option>';
                    break;
    				default:
    					// Default is the last 48hrs
    					$day = date("d")-1;
                        
		        }  
                    $thestartdatecalc  = $year . '-' .$month . '-' . $day ;
                    echo $thestartdate;
                    return array($retval, $thestartdatecalc);
            }        


$theenddate  = $year . '-' .$month . '-' . $day ;

        // Check if the Date is Set
        if(isset($date_filter) and strlen($date_filter) > 0)
	{
		// The date is Set
		echo "Date is Set :- " . $date_filter .  " - ";
		// Check to see if Region is set
		if(isset($region_filter) and strlen($region_filter) > 0)
		{
            // The region is set
			echo "Region is Set :- " . $region_filter .  " - ";
			// Check to see if species is set
                if(isset($species_filter) and strlen($species_filter) > 0)
                {
				// The species is set
				echo "Species is Set :- " . $species_filter .  " - ";
                    list($arr1, $arr2) = datedropdown_funct($date_filter);
                    $date_filter_options= $arr1;
                    $thestartdate.= $arr2;  
               		$sql2 = "SELECT * FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND region = '".$region_filter ."' AND species = '".$species_filter ."' ORDER BY Date DESC, time DESC  ";
                $sql3 = "SELECT DISTINCT species FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND region = '". $region_filter . "' GROUP BY species";
                    $sql = "SELECT DISTINCT region FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND species = '".$species_filter ."'  GROUP BY region";   
                
                }
                else
                {
				// The species is Not Set
				echo "Species is NOT set - ";
                    list($arr1, $arr2) = datedropdown_funct($date_filter);
                    $date_filter_options= $arr1;
                    $thestartdate.= $arr2; 
    				$sql2 = "SELECT * FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND region = '".$region_filter ."'  ORDER BY Date DESC, time DESC  ";
                $sql3 = "SELECT DISTINCT species FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND region = '". $region_filter . "' GROUP BY species";
                    $sql = "SELECT DISTINCT region FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' GROUP BY region";   

                
               }
            }
          else
          {
			// The region is not set
			echo "Region is NOT set - ";         
			// Check to see if species is set
			if(isset($species_filter) and strlen($species_filter) > 0)
			{
				// The species is set
				echo "Species is Set :- " . $species_filter .  " - ";
				list($arr1, $arr2) = datedropdown_funct($date_filter);
				$date_filter_options= $arr1;
				$thestartdate.= $arr2;     
				$sql2 = "SELECT * FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND species = '".$species_filter  ."' ORDER BY Date DESC, time DESC  ";
				$sql3 = "SELECT DISTINCT species FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."'  GROUP BY species";
				$sql = "SELECT DISTINCT region FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."' AND species = '".$species_filter  ."'  GROUP BY region";   
			}
			else
			{
				// The species is NOT Set
				echo "Species is NOT set - ";
				list($arr1, $arr2) = datedropdown_funct($date_filter);
				$date_filter_options= $arr1;
				$thestartdate.= $arr2; 
				$sql2 = "SELECT * FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."'  ORDER BY Date DESC, time DESC  ";
				$sql3 = "SELECT DISTINCT species FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."'  GROUP BY species";
				$sql = "SELECT DISTINCT region FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."'  GROUP BY region";   
			}
  		   }
        }
	else
	{
	 // The date is not set
	 $date_filter_options = '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option>';
	 echo "Date is NOT set - ";
          // Check to see if Region is set
          if(isset($region_filter) and strlen($region_filter) > 0)
          {         
            // The region is set
            echo "Region is Set :- " . $region_filter .  " - ";
            // Check to see if species is set
                if(isset($species_filter) and strlen($species_filter) > 0)
                {
                // The species is Set
                echo "Species is Set :- " . $species_filter .  " - ";
            
                }
                else
                {
                 // The species is not set
                echo "Species is NOT set - ";
                }              
                
          }
          else
          {
            // The region is Set
            echo "Region is NOT set - ";
            // Check to see if species is set
                if(isset($species_filter) and strlen($species_filter) > 0)
                {
                // The species is Set
                echo "Species is Set :- " . $species_filter .  " - " ;

                // Build the SQL strings
			$sql2 = "SELECT * FROM alerts WHERE species ='".$species_filter ."'  ORDER BY Date DESC, time DESC  ";
			$sql = "SELECT DISTINCT region FROM alerts GROUP BY region";
                $sql3 = "SELECT DISTINCT species FROM alerts WHERE species ='".$species_filter ."' GROUP BY species";
                 // 
  
                
            
                }
                else
                {
                 // The species is not set
                 echo "Species is NOT set - ";
                 
                // We will only display 5 days worth of Data
			$year =date("Y");
			$month = date("m");
			$day = date("d")-5;
			$thestartdate  = $year . '-' .$month . '-' . $day ;
                // Build the SQL strings
			$sql2 = "SELECT * FROM alerts WHERE Date BETWEEN '".$thestartdate ."' AND '".$theenddate ."'  ORDER BY Date DESC, time DESC  ";
			$sql3 = "SELECT DISTINCT species FROM alerts GROUP BY species";
			$sql = "SELECT DISTINCT region FROM alerts GROUP BY region";
                }
          }
	}


	$result2 = $db->sql_query($sql2);
	if (!($result2 = $db->sql_query($sql2)))
	{
		die("Database Query Failed" . mysql_error());
	} 
	$i  = 1;

	while ( $row2 = $db->sql_fetchrow($result2) )
		{
			$template->assign_block_vars('alerts', array( 'POS' => $i ,
					'REGION' => str_replace("&", " & ", $row2['region']),
					'SPECIES' => str_replace("%", "'",$row2['species']),
					'DATE' => $row2['Date'],
					'TIME' =>  $row2['time'],
					'COMMENTS' => str_replace("'", "%",$row2['comments']),
					)
				);
			$i++;
		}

	$template->assign_vars(array(
			'USERNAME' => htmlspecialchars($userdata[username]),
			'REGION_FILTER' => str_replace("&", " & ", $region_filter_options),
			'SPECIES_FILTER' => $species_filter_options,
			)
		);




	$result = $db->sql_query($sql);
  
	if (!($result = $db->sql_query($sql)))
		{
  			 	die("Database Query Failed" . mysql_error());
		} 

	// This is where you would add a new VARS Array if you intend to use your own custom VARS.


        while ($row = $db->sql_fetchrow($result))
        {
            $selected = (isset($_GET['REGION_FILTER']) AND $_GET['REGION_FILTER']==$row['region']) ? ' selected="selected"' : '' ;
            $region_filter_options .= "<option value=\"{$row['region']}{$selected}>{$row['region']}</option>\n";
        }

	$result = $db->sql_query($sql3);
  
	if (!($result = $db->sql_query($sql3)))
		{
  			 	die("Database Query Failed" . mysql_error());
		} 

	// This is where you would add a new VARS Array if you intend to use your own custom VARS.

	while ($row = $db->sql_fetchrow($result))
        {
            $selected = (isset($_GET['SPECIES_FILTER']) AND $_GET['SPECIES_FILTER']==$row['species']) ? ' selected="selected"' : '' ;
            $species_filter_options .= "<option value=\"{$row['species']}{$selected}>{$row['species']}</option>\n";
        }


	$template->assign_vars(array(
			'USERNAME' => htmlspecialchars($userdata[username]),
			'REGION_FILTER' => str_replace("&", " & ", $region_filter_options),
			'SPECIES_FILTER' => $species_filter_options,

		)
	);	


	$template->assign_vars(array(
			'USERNAME' => htmlspecialchars($userdata[username]),
			'DATE_FILTER' => $date_filter_options,	
		)
	);	

	// Build the page
	$template->pparse('body');

	// standard page footer
	include(IP_ROOT_PATH . 'includes/page_tail.'.PHP_EXT);

   }
?>

 

regards

 

 

Link to comment
Share on other sites

As my signature states, I don't test my code, so there may be typos. I expect that once the code is dropped into the application that the person with the code can find and fix them.

 

Anyway, I left out the ending double quote for the value inside the option tag. This line should be correct:

$region_filter_options .= "<option value=\"{$row['region']}\"{$selected}>{$row['region']}</option>\n";

Link to comment
Share on other sites

I did not really read through your entire thread in detail, so I don't know if this will even be of any use, but if the array pointer is starting at position 1 as opposed to 0, then your problem MIGHT happen, so there is a function to reset the array pointer back at 0 which is reset() .. you can find out more about it here:

 

http://php.net/manual/en/function.reset.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.