Jump to content

Problem with listbox and sort variable


wouterblacquiere

Recommended Posts

Hey there,

 

I´ve got an index.php page that displays data from a database.  (SELECT * FROM table WHERE id=1 ORDER BY $sort $order)

The user can then sort this data by clicking on the column header.  This works fine.

 

Now I´d like to add a listbox so the user can also change the data (id=2, id=3, etc.) she/he wishes to sort. This is where I run into a little trouble.

 

The query I'm trying to use to display the data is

SELECT * FROM table WHERE id = '$selectedoption' ORDER BY $sort $order

 

The selectedoption variable is

$selectedoption = $_GET['selectedoption'];

 

and the listbox code starts like this

<form action="index.php" method="get">

<select name="selectedoption">

<option value ="1">1</option>

<option value ="2">2</option>

etc.

 

Now the page doesn't display any information at all, until the user selects 1 or 2 or etc.

More importantly: the page then loses the ability to sort, because clicking on any of the column headers now brings us back to the empty page.

 

Could anyone give me a tip of where to go next?

 

All the best.

 

Wouter.

Link to comment
Share on other sites

I am not sure if I completely understand your problem, but it seems to me like you need to define a default action so that something is displayed until the user decides to select option "1" or "2" and so on.

 

Why don't you try something along the lines of:

 

 

if (isset($_GET['selectedoption'])) {

$selectedoption = $_GET['selectedoption'];
}
else {
$selectedoption = 0; //change it to whatever you want...
}

 

Make sure you put "SELECT * FROM table WHERE id = '$selectedoption' ORDER BY $sort $order" after $selectedoption is assigned.

Link to comment
Share on other sites

Wow! We're getting really close. Thank you very much!

 

The page now opens the data which I set to the default value and the sort function works perfectly when the column headers are clicked. Also, when the user changes the data she or he wants to see by selecting an option from the listbox, the new data is being displayed perfectly.

 

However, the last bit of trouble is that the sort function only works for the first, default data.

When the user changes to a new set of data, clicking on the column header now resets the page to the default value, and we're back at the start again.

Here are the essential parts of the code:

 

First the html code for the listbox:

<form action="index.php" method="get">
<select name="selectedoption">
<option value ="1">1</option>
<option value ="2">2</option>
etc...
</select>
<input name="submitbutton" type="submit" value="Choose" />
</form>

 

Then the php code for setting the selectedoption variable and the default data to display:

<?php
if (isset($_GET['selectedoption'])) {
$selectedoption = $_GET['selectedoption'];
}
else {
$selectedoption = 2; //change it to whatever you want...
}
?>

Finally the php code for sorting the data by clicking the column headers

<?php
include 'includes/connection.php';
$Default = 'nr'; 
$Columns = array('nr','name', ...);
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $Columns) ? $_GET['sort'] : $Default; 
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$result = mysql_query("SELECT * FROM table WHERE id = '$selectedoption' ORDER BY $sort $order");
?>

<table border='0' cellpadding='3'> 
<tr> 
<td><a href='?sort=nr&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Nr</a></td>
<td><a href='?sort=name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
...
</tr>

<?php
while ($row = mysql_fetch_assoc($result)) { 
echo "<tr> 
<td>$row[nr]</td>
<td>$row[name]</td>
...
</tr>"; 
}
echo "</table>";
?>

 

Does anyone know how I can get the sort script to run without setting the data to the default value?

 

Thank in advance for your time.

 

All the best,

 

Wouter.

Link to comment
Share on other sites

Wow! We're getting really close. Thank you very much!

 

Once again, I am not 100% sure what you mean. For something like this, it would be really useful if I could take a look at the actual problem live. But anyway, from what I see, the reason why it keeps going back to the default value is because when you "sort" it is retrieving the information from the database all over again and because the "selectedoption" is not set on the "NR" and "NAME" links, it retrieves the information from the default option because "selectedoption=" doesn't exist.

 

In other words, instead of having:

<td><a href='?sort=nr&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Nr</a></td>

 

Try:

<td><a href='?selectedoption=<?php echo $selectedoption; ?>&sort=nr&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Nr</a></td>

 

Now it should send over the current ID that the user selected and the database will retrieve the information based on the ID in the $selectedoption variable, do you get what I am saying? Add "selectedoption=<?php echo $selectedoption; ?>" to all your filter links.

 

 

Link to comment
Share on other sites

  • 2 weeks later...
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.