ed01 Posted November 14, 2006 Share Posted November 14, 2006 Hi guys, I'm a new user to this forum and pretty new to php/sql. Here's what's got me stumped....I'm having difficulty getting my retrieved database data to display the way I need. I would like to be able to choose the fields to filter out and I'm using a drop-down menu to do so. It works fine except that instead of displaying all of the chosen fields, it only displays the field coresponding to the LAST item in the drop-down list (rather than ALL the selected ones). I should be able to use Ctrl-click to select the fields I want to display. Here's the relevant code. I hope it's enough.Any help would be greatly appreciated!$querycolumns = $_POST['showcolumns'];$query = "select {$querycolumns} from momentum_apex";// Select a sorting criteria.$HTML=<<<HTML<form action="{$_SERVER['PHP_SELF']}" method="post"> <select name="showcolumns" size="3" multiple="multiple"> <option value="role">role</option> <option value="first_name">first_name</option> <option value="last_name">last_name</option> </select> <input name="filter" type="submit" value="Filter now!" /> </form><br />HTML;echo $HTML; Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 16, 2006 Share Posted November 16, 2006 The name for your [i]select[/i] should have some square brackets on the end to define it as an array, like so:[code]<select name="showcolumns[]" size="3" multiple="multiple">[/code]$_POST['showcolumns'] will then be an array, so you'll have to loop through it and build your query within the loop:[code]<?php$qc = $_POST['showcolumns'];$query = "SELECT ";foreach($qc as $c) $query .= "`$c`, ";$query = substr($query,0,-2)." FROM `momentum_apex`";?>[/code] Quote Link to comment Share on other sites More sharing options...
ed01 Posted November 22, 2006 Author Share Posted November 22, 2006 HiI just wanted to follow up on this issue. First of all thank you so much, it set me on the right path to come up with a working solution. I ended up going with check boxes instead of a select element. I've included my code below hoping that it may be helpful to others.[code]<input type="checkbox" name="formfield[]" value="first_name" checked />first_name<br />[/code][code]if (isset ($_POST['formfield'])) {if (is_array ($_POST['formfield'])) { $query = "SELECT "; foreach ($_POST['formfield'] as $field) { $query .= "$field, "; } // end foreach } else { print 'formfield is not an array!'; } // end else$stringlength = strlen($query); // determine number of characters in query$stringlengthtrimmed = ($stringlength - 2); // remove last space and comma from query string$query = substr($query,0,$stringlengthtrimmed)." FROM momentum_apex";}[/code]Cheers! Quote Link to comment 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.