...

View Full Version : 'Sort by' problem



imatus
07-10-2005, 03:42 PM
Hello again everybody :)

I'm facing a small problem.

<form name="f" action="finished.php" method="post">

Sort by: <select name="sortby">
<option value="Game asc">Game (ascending)</option>
<option value="Game desc">Game (descending)</option>
...
<option value="Opponent desc">Opponent (descending)</option>
</select>
<input type="submit" name="go" value="Go!">
</form>

$query1 = 'SELECT * FROM finished ORDER BY number';
$query2 = 'SELECT * FROM finished ORDER BY number DESC';
...
$query8 = 'SELECT * FROM finished ORDER BY opponent DESC';

if ( !isset ($go) ) { // Here is the problem... me thinks... :S
$r = mysql_query ($query1);
} else {
if ($sortby == 'Game asc') {
$r = mysql_query ($query1);
}
if ($sortby == 'Game desc') {
$r = mysql_query ($query2);
}

...

if ($sortby == 'Opponent desc') {
$r = mysql_query ($query8);
}
}
Now THE problem is that whatever I choose in Sort by' and hit 'go' the entries are sorted and displayed BUT the option shown in the box goes back to "Game ascending" so I actually don't know what kind of sorting it was!!

Please drop me a hint how the problem can be sorted out!
Thank you in advance!
Have an excellent weekend!

delinear
07-10-2005, 03:54 PM
If you're displaying the results on the same page, you can find out which option should be selected using the following:

<option value="Game asc"<?php echo (isset($sortby) && $sortby == 'Game asc' ? ' selected="selected" : ''); ?>>Game (ascending)</option>
<option value="Game desc"<?php echo (isset($sortby) && $sortby == 'Game desc' ? ' selected="selected" : ''); ?>>Game (descending)</option>
.
.
etc

Fou-Lu
07-10-2005, 04:06 PM
Is this:


<form name="f" action="finished.php" method="post">

Sort by: <select name="sortby">
<option value="Game asc">Game (ascending)</option>
<option value="Game desc">Game (descending)</option>
...
<option value="Opponent desc">Opponent (descending)</option>
</select>
<input type="submit" name="go" value="Go!">
</form>

within a php script? Cause if it is, you can have it dynamically set by the name. As well, I don't know why you have like 8 possible queries there, you can build it dynamically as well:


<?php
if (empty($_REQUEST))
{
$_REQUEST = array_merge($_GET, $_POST);
}

if (!isset($_REQUEST['do']))
{
$_REQUEST['do'] = 'search';
}

if ($_REQUEST['do'] == 'search')
{
?>
<form name="f" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Sort By: <select name="sortby">
<?php
$options = array(
g_asc => 'Game (Ascending)',
g_desc => 'Game (Descending)',
.....
opp_desc => 'Opponent (Descending)',
);

if (is_array($options))
{
foreach ($options AS $opval => $opname)
{
if ($opval == $_REQUEST['sortby'])
{
$selected = ' selected="selected"';
}
else
{
$selected = '';
}
echo "<option value=\"" . $optval . $selected . "\">" . $optname . "</option>\n";
}
}
?>
</select>
<input type="hidden" name="do" value="dosearch" />
<input type="submit" name="go" value="Go!">
</form>
<?php
}

if ($_REQUEST['do'] == 'dosearch')
{
// Get the sorted options by its underscore split:
$split = explode('_', $_POST['sort']);
$orderby = '';
$direction = '';

switch($split[0])
{
case 'g':
$orderby = 'number'; // Maybe number?
break;
case 'opp':
$orderby = 'opponent';
break;
}
switch($split[1])
{
case 'asc':
$direction = 'ascending';
break;
case 'desc':
$direction = 'descending';
break;
}

if (!empty($orderby))
{
$orderby = ' ORDER BY ' . $orderby;
}
if (!empty($direction))
{
$direction = ' ' . $direction;
}
$query = "SELECT * FROM `finished`" . $orderby$direction;

$r = mysql_query($query);

/* etc. */
}


Untested and unsure of all initializations. Hope that helps!


Man... took me ten minutes to type that. I'm slipping, lol.

Taylor_1978
07-10-2005, 05:48 PM
A small suggestion.. its no good for you now as you've already typed it.. but I'm bored so I'll suggest it anyway...

To save typing ALL those options and ALL those queries, an alternative may have been:

<form name="f" action="finished.php" method="post">

Sort: <select name="sort">
<option value="game">By Game</option>
<option value="opponent">By Opponent</option>
</select>
Order: <select name="order">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
<input type="submit" name="go" value="Go!">
</form>

Then on your finsihed.php page:



mysql_query('SELECT * FROM finished ORDER BY $sort $order');


1 line would fix it all :)

imatus
07-11-2005, 12:00 AM
Thank you very much for your help, guys. I'm an absolut rookie and I highly appreciate your time and consideration :)

Have a wonderful week :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum