We help IT Professionals succeed at work.
Get Started

Open selected listbox item

Pierre K
Pierre K asked
on
95 Views
Last Modified: 2015-08-06
Hi all,

I have the following script and would like to know where I would place code to open the item once it is selected from the listbox.
I'm planning on using the header('Location: ...;) method but have no idea where to place it in the code

I hope I made myself clear on what I need
Thanks a lot

<?php
session_start();

error_reporting(E_ALL);

// THE DESIRABLE FILE NAMES ARE KEPT IN AN ARRAY
//should be able to find 123*.xml
//$signals = Array($_SESSION['username'].'*.xml'=>'regex');
//$signals = Array($_SESSION['username'].'.*\.xml'=>'regex');
  $signals = Array( '^' . $_SESSION['username'].'.*\.xml'=>'regex');
  
// THE OPTION STATEMENTS ARE COLLECTED IN AN ARRAY
$options = Array();

// COLLECT THE FILE OBJECTS
$path = realpath(getcwd());
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS TO FIND THE DESIRABLE FILES
foreach($objs as $path => $obj)
{
    $name = $obj->getFileName();
    // this is looking for "exact" matches.
    if (in_array($name, $signals))
    {
        $options[]
        = '<option value="'
        . $path
        . '">'
        . $name
        . '</option>'
        ;
    }
	else
	{ //echo 'Searching for '.$name.PHP_EOL;
		foreach($signals as $k=>$v)
		{
            // is you set $signals = [ '123*.xml/'=>'regex', '456*.txt'=>'regex']
            //the if clause below will get executed
			if( $v=='regex' )
			{
				if( preg_match('/'. str_replace('/','\\/',$k) .'/',$name))
				{
					$options[]
					= '<option value="'
					. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
					. '">'
					. substr($name,strlen($_SESSION['username']),strlen($name)-4-strlen($_SESSION['username'])) //Only pass file name without ext
					. '</option>'
					;
				}
			}

            // is you set $signals = [ '123'], but you have the following files: '123.xml', 'xyz-123.txt'
            // the if clause below will get executed and will find both, because this is a "contains" filter
			elseif( stripos($name, $v)!==false )
			{
				$options[]
				= '<option value="'
				. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
				. '">'
				. $name
				. '</option>'
				;
			}
		}
	}
}

// GENERATE THE OPTION STATEMENTS
$opts = implode(PHP_EOL, $options);

// CREATE THE HTML USING HEREDOC NOTATION
$html = <<<EOD
<p>Leave History<br>
<select name="listbox" size="1">
<option value="">Open to view</option>
$opts
</select>
</p>
EOD;

// SHOW THE HTML
echo $html;

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE