Jump to content

How to set a field sorting criteria using a dropdown menu


ed01

Recommended Posts

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;
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

Hi
I 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!
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.