bishmedia Posted February 3, 2013 Share Posted February 3, 2013 (edited) 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 February 3, 2013 by bishmedia Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/ Share on other sites More sharing options...
kicken Posted February 3, 2013 Share Posted February 3, 2013 $_POST['select'] or $_GET['select'], depending on what the method of your form is. Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409854 Share on other sites More sharing options...
bishmedia Posted February 3, 2013 Author Share Posted February 3, 2013 I'll have to try and build a form then, i better get googling Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409856 Share on other sites More sharing options...
Barand Posted February 3, 2013 Share Posted February 3, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409859 Share on other sites More sharing options...
bishmedia Posted February 3, 2013 Author Share Posted February 3, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409861 Share on other sites More sharing options...
Barand Posted February 3, 2013 Share Posted February 3, 2013 As Kicken told you $search = $_POST['bandName']; Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409862 Share on other sites More sharing options...
bishmedia Posted February 3, 2013 Author Share Posted February 3, 2013 (edited) 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 February 3, 2013 by bishmedia Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409871 Share on other sites More sharing options...
Barand Posted February 3, 2013 Share Posted February 3, 2013 (edited) Check to see if anything has been posted if (isset($_POST['btnSubmit'])) { // put the search processing in here } Edited February 3, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409874 Share on other sites More sharing options...
bishmedia Posted February 3, 2013 Author Share Posted February 3, 2013 Thats perfect, i would like to thank you ver much for your help today :-) Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409875 Share on other sites More sharing options...
bishmedia Posted February 3, 2013 Author Share Posted February 3, 2013 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409922 Share on other sites More sharing options...
Barand Posted February 3, 2013 Share Posted February 3, 2013 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1409946 Share on other sites More sharing options...
bishmedia Posted February 4, 2013 Author Share Posted February 4, 2013 Ok coulnt wait to get home from work to try this guess what... it works :-) Once again many thanks, in the future i would like to try and change the look and perhaps have an auto select instead of a search button :-) Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1410045 Share on other sites More sharing options...
bishmedia Posted February 7, 2013 Author Share Posted February 7, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1410801 Share on other sites More sharing options...
Barand Posted February 7, 2013 Share Posted February 7, 2013 Will all results have the same date? If not, you will have to alter the loop to test for changes in the date and output the heading when there is a new data then start a new table. Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1410853 Share on other sites More sharing options...
bishmedia Posted February 9, 2013 Author Share Posted February 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1411289 Share on other sites More sharing options...
Barand Posted February 9, 2013 Share Posted February 9, 2013 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1411312 Share on other sites More sharing options...
bishmedia Posted February 9, 2013 Author Share Posted February 9, 2013 Thats great, many thanks.... you make it look so easy Quote Link to comment https://forums.phpfreaks.com/topic/273981-grabbing-combobox-values/#findComment-1411325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.