Jump to content

Grabbing Combobox Values?


bishmedia

Recommended Posts

How do i get the value from a combobox to view certain xml files???

 

 

<p>Competitions<br>
<select name="select">
<option value="Option 1" selected>Butlins Mineworkers</option>
<option value="Option 2">West Midlands Area</option>
<option value="Option 3">Wales Area</option>
<option value="Option 4">Scotland Areas</option>
</select>
</p>


<?php
$xml = simplexml_load_file('xmltestdata.xml');

$search = ****above value***
$results = $xml->xpath("//result[compName='$search']");

echo "<u><b>$search</b></u><br /><br />";

foreach ($results as $res) {
echo "$res->bandName $res->year $res->position <br>";
}
?>

 

Sorry if im being thick but i a newbie :-p

 

Many Thanks in advance :-)

Edited by bishmedia
Link to comment
Share on other sites

Don't hard-code the options in your combo, get them from the file. If you don't you have to change the program each time.

 

<?php
function getOptions($file, $element)
{
   $xml = simplexml_load_file($file);
   $results = $xml->xpath("//result/$element");
   $results = array_unique($results);
   $options = "<option value=''>- select $element -</option>\n";
   foreach($results as $res) {
    $options .= "<option value='{$res}'>{$res}</option>\n";
   }
   return $options;
}
?>

<form method="post">
   <select name="bandName">
   <?php echo getOptions('testxmldata.xml', 'bandName')?>
   </select>
   <br>
   <input type="submit" name="btnSubmit" value="Search">
</form>

Link to comment
Share on other sites

Many thanks for your help today Barand

 

Ive put together below what youve done for me today, the select is now working but the $search string is throwing up and error...

Notice: Undefined variable: search in xmltest.php on line 27

How should the $search string be??

 

<?php
function getOptions($file, $element)
{
       $xml = simplexml_load_file($file);
       $results = $xml->xpath("//result/$element");
       $results = array_unique($results);
       $options = "<option value=''>- select $element -</option>\n";
       foreach($results as $res) {
               $options .= "<option value='{$res}'>{$res}</option>\n";
       }
       return $options;
}
?>

<form method="post">
       <select name="bandName">
       <?php echo getOptions('xmltestdata.xml', 'bandName')?>
       </select>
       <br>
       <input type="submit" name="btnSubmit" value="Search">
</form>

<?php

$xml = simplexml_load_file('xmltestdata.xml');

$search = ????    //This is line 27
$results = $xml->xpath("//result[bandName='$search']");

foreach ($results as $res) {
       echo "$res->compName $res->year $res->position <br>";
}

?>

 

Many thanks in advance

Link to comment
Share on other sites

Ok that works fine many thanks, just one last question when you load the page the select is there followed b an error...

Notice: Undefined index: bandName in \xmltest.php on line 27

But once i select a band it goes away and shows results...

 

Whats the best way of getting rid of this error when the page loads??

 

Oh, and your great :-)

Edited by bishmedia
Link to comment
Share on other sites

Ive changed everything to view by competition now, great.

How easy is it to change to have two combo boxes, one for compName and one for year and the results show this effect??

 

 

<?php
function getOptions($file, $element)
{
               $xml = simplexml_load_file($file);
               $results = $xml->xpath("//result/$element");
               $results = array_unique($results);
               $options = "<option value=''>- Choose Competition -</option>\n";
               foreach($results as $res) {
                               $options .= "<option value='{$res}'>{$res}</option>\n";
               }
               return $options;
}
?>

<form method="post" name="Band">
               <select name="compName">
               <?php echo getOptions('xmldata.xml', 'compName')?>
               </select>
               <input type="submit" name="btnSubmit" value="Search">
</form>

<?php
if (isset($_POST['btnSubmit'])) {
$xml = simplexml_load_file('xmldata.xml');

$search = $_POST['compName'];

$results = $xml->xpath("//result[compName='$search']");
echo "$search <br />";
echo "<table>";
echo "<tr>";
echo "<td>"; 
foreach ($results as $res) {
               echo "$res->bandName</td> <td style=\"width:175px\">$res->conductor</td> <td>$res->position";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"; 

}
echo "</table>";
}
?>  

Link to comment
Share on other sites

Here's an improved version. You can select competition, year or both.

 

<?php

function getOptions($file, $element)
{
   $xml = simplexml_load_file($file);
   $results = $xml->xpath("//result/$element");
   foreach ($results as &$v) $v = (string)$v;
   $results = array_unique($results);
   sort($results);
   $options='';
   foreach($results as $res) {
    $selected='';
    if (isset($_POST[$element]))
	    $selected = $res==$_POST[$element] ? 'selected="selected"' : '';
    $options .= "<option $selected value='{$res}'>{$res}</option>\n";
   }
   return $options;
}
?>

<form method="post">
   <select name="compName">
   <option value=''>- select competition -</option>
   <?php echo getOptions('xmltestdata.xml', 'compName')?>
   </select>
   <br>
   <select name="year">
   <option value=''>- select year -</option>
   <?php echo getOptions('xmltestdata.xml', 'year')?>
   </select>
   <br>
   <input type="submit" name="btnSubmit" value="Search">
</form>

<?php

$searcharray = array();
$search = '';

if (isset($_POST['compName']) && $_POST['compName']) {
   $searcharray[] = "[compName='{$_POST['compName']}']";
}
if (isset($_POST['year']) && $_POST['year']) {
   $searcharray[] = "[year='{$_POST['year']}']";
}

$search = join('', $searcharray);

if ($search) {
   $xml = simplexml_load_file('xmltestdata.xml');

   $results = $xml->xpath("//result$search");

   if ($results) {
    echo "<table>";
    foreach ($results as $res) {
	    echo "<tr><td>
	    $res->bandName</td> <td style=\"width:175px\">$res->conductor</td> <td>$res->position
	    </td></tr>";
    }
    echo "</table>";	  
   }
   else {
    echo "<p>No matching results</p>";
   }
}
else {
   echo "<p>Please enter search criteria</p>";
}


?>

Link to comment
Share on other sites

Ive added a new parameter in the xml file called 'date' but for the life of me i cant figure out how to add it to a title highlighted in red below, xml file is like below..

 

 

[b]<result>[/b]
[b]<year>2013</year>[/b]
[b]<date>19th January 2013</date>[/b]
[b]<compName>Butlins Mineworkers</compName>[/b]
[b]<section>First</section>[/b]
[b]<bandName>Stannington Brass</bandName>[/b]
[b]<position>12</position>[/b]
[b]<conductor>Geoff Hawley</conductor>[/b]
[b]</result>[/b]

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<?php include("../includes/head.php"); ?>

<title>Competition Results - Rushden Windmill Band</title>

<link href="../stylesheet/style.css" rel="stylesheet" type="text/css" />

 

</head>

 

<body>

 

<div style="padding:10px 10px 10px 10px">

 

<hr />

<h1>Competition Database</h1>

<hr />

<h4>

<?php

 

function getOptions($file, $element)

{

$xml = simplexml_load_file($file);

$results = $xml->xpath("//result/$element");

foreach ($results as &$v) $v = (string)$v;

$results = array_unique($results);

sort($results);

$options='';

foreach($results as $res) {

$selected='';

if (isset($_POST[$element]))

$selected = $res==$_POST[$element] ? 'selected="selected"' : '';

$options .= "<option $selected value='{$res}'>{$res}</option>\n";

}

return $options;

}

 

?>

 

<form method="post">

<select name="compName">

<option value=''>- select competition -</option>

<?php echo getOptions('xmldata.xml', 'compName')?>

</select>

 

<select name="section">

<option value=''>- select section -</option>

<?php echo getOptions('xmldata.xml', 'section')?>

</select>

 

<select name="year">

<option value=''>- select year -</option>

<?php echo getOptions('xmldata.xml', 'year')?>

</select>

 

<input type="submit" name="btnSubmit" value="Search">

</form>

 

<?php

 

$searcharray = array();

$search = '';

 

if (isset($_POST['compName']) && $_POST['compName']) {

$searcharray[] = "[compName={$_POST['compName]}']";

}

if (isset($_POST['section']) && $_POST['section']) {

$searcharray[] = "[section={$_POST['section]}']";

}

if (isset($_POST['year']) && $_POST['year']) {

$searcharray[] = "[year={$_POST['year]}']";

}

 

 

$search = join('', $searcharray);

 

if ($search) {

$xml = simplexml_load_file('xmldata.xml');

 

$results = $xml->xpath("//result$search");

 

if ($results) {

echo "<h4><u>{$_POST['compName']} - {$_POST['section']} Section - {$_POST['year']}</u></h4>"; <--- Title to include Date

echo "<table>";

foreach ($results as $res) {

echo "<tr><td>$res->position</td><td>$res->bandName</td> <td style=\"width:175px\">$res->conductor</td> </tr>";

}

echo "</table>";

}

else {

echo "<p>No matching results</p>";

}

}

else {

echo "<p>Please enter search criteria</p>";

}

 

?>

</h4>

<hr />

 

</div>

</body>

</html>

Link to comment
Share on other sites

Ive attached the xml so you can see what i mean, each final result will have the same date.

 

Im not 100% sure on what best for this but evertime i try something i screw it up and have to go

back to te original, i tougt if you have the end result it would be easy grabbing the date parameter?

xmldata.xml

Link to comment
Share on other sites

this is the revised php code section below you form

 

<?php

$searcharray = array();
$search = '';

if (isset($_POST['compName']) && $_POST['compName']) {
    $searcharray[] = "[compName='{$_POST['compName']}']";
}
if (isset($_POST['section']) && $_POST['section']) {
    $searcharray[] = "[section='{$_POST['section']}']";
}
if (isset($_POST['year']) && $_POST['year']) {
    $searcharray[] = "[year='{$_POST['year']}']";
}


$search = join('', $searcharray);

if ($search) {
   $xml = simplexml_load_file('xmldata.xml');

   $results = $xml->xpath("//result$search");

   if ($results) {
    $prev = '';
    foreach ($results as $res) {
	    $heads = $res->compName . $res->section . $res->date;
	    if ($heads != $prev) {
		    if ($prev) echo "</table>\n";
		    echo "<h4><u>{$res->compName} - {$res->section} Section - {$res->date}</u></h4>\n";
		    echo "<table>\n";
		    $prev = $heads;
	    }
	    echo "<tr><td>$res->position</td><td>$res->bandName</td> <td style=\"width:175px\">$res->conductor</td> </tr>\n";
    }
    echo "</table>\n";		  
   }
   else {
    echo "<p>No matching results</p>";
   }
}
else {
   echo "<p>Please enter search criteria</p>";
}

?>

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.