Jump to content

Drop Down Query


yandoo

Recommended Posts

Hi there,

 

Im bit stuck and was hoping some expert advice please :)

 

Basically i have a simple drop down menu thats options display how a user will order records of a database by...

 

For example: Drop Down options - ID, Type, Name.  It is suppose to bring records from my DB and order then by the option selected...

 

Heres what i have so far...:

 

<?php require_once('Connections/woodside.php'); ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
if (isset($_GET['orderby'])) { 
$orderby= $_GET['orderby']; 
}else{ $orderby="AnimalID"; }
//CHECKING TO SEE IF THE PERSON HAS CHOSEN TO ORDER BY A CERTAIN FIELD
?>
       <select name=orderby>
<option value="-">Select one...</option>
<option value="ID" onclick="window.location='?orderby=AnimalID';">ID</option>
<option value="name" onclick="window.location='test8.php?orderby=Name';">Name</option>
<option value="animaltype" onclick="window.location='test8.php?orderby=AnimalTypeID';">Animal Type</option>
</select>
<?php
$test= mysql_query("SELECT * FROM animal ORDER BY $orderby DESC");
$test = mysql_query($query_test, $woodside) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);
?>
<?php echo $row_test['AnimalID']; ?>
</body>
</html>

 

There is a mesage on screen saying query is always empty??? It doesnt do anything with IE 7. But in Firefox it will link to the page but no records are displayed (when this happens addres bar on browser says: /test8.php?orderby=Name) and it still says Query enpty.

 

I tried to echo out query to see if it works and nothing appears...

 

 

Please help me,  think im close to getting this to work, but could really use some help.

 

Thank You

 

 

 

 

Link to comment
Share on other sites

You are trying to set a mysql_query funtion into a mysql_query funtion and then array it.

 

It should be

<?php
$test= mysql_query("SELECT * FROM `animal` ORDER BY `{$orderby}` DESC", $woodside) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);
?>

Link to comment
Share on other sites

$test = mysql_query($query_test, $woodside) or die(mysql_error());

 

should be:

$test = mysql_query($test) or die(mysql_error());

 

 

 

Not technically $woodside could be a var we don't see that is the mysql connection resource.

 

however I have a problem with the $query_test part as yuo don't define the query thus yes it is empty so php is keeping it real as the kids like to say

Link to comment
Share on other sites

Hi there and thank you everyboy who has replied!

 

I have made changes accordingly and it now works with IE7 and also the "Query is empty" message no longer appears....instead is another saying "no Database Selected".

 

How can this be? because the table & database name is all correct???

 

Thank All for Your help!!!

 

Tom :)

 

 

Link to comment
Share on other sites

Hey,

 

thats brilliant thank you im obviously quite a noob at this :)

 

Manged to ovrecome that little obstacle only to hit another (blast!) 

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

 

Thank You :)

Link to comment
Share on other sites

Hi there,

 

 

Heres the full code....theres not much to it really..

 <?php require_once('Connections/woodside.php'); ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
if (isset($_GET['orderby'])) { 
$orderby= $_GET['orderby']; 
}else{ $orderby="AnimalID"; }
//CHECKING TO SEE IF THE PERSON HAS CHOSEN TO ORDER BY A CERTAIN FIELD
?>
       <select name=orderby>
<option value="-">Select one...</option>
<option value="ID" onclick="window.location='test8.php?orderby=AnimalID';">ID</option>
<option value="name" onclick="window.location='test8.php?orderby=Name';">Name</option>
<option value="animaltype" onclick="window.location='test8.php?orderby=AnimalTypeID';">Animal Type</option>
</select>
<?php
mysql_select_db($database_woodside, $woodside);
$test= mysql_query("SELECT * FROM `animal` ORDER BY `{$orderby}` DESC", $woodside) or die(mysql_error());
$test = mysql_query($test) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);
?>

<?php echo $row_test['AnimalID']; ?>
</body>
</html>

 

Thank You :)

Link to comment
Share on other sites

You are doing your query twice

 

$test= mysql_query("SELECT * FROM `animal` ORDER BY `{$orderby}` DESC", $woodside) or die(mysql_error());

$test = mysql_query($test) or die(mysql_error());

 

change to

 

$test= ("SELECT * FROM `animal` ORDER BY `{$orderby}` DESC");

$result = mysql_query($test) or die(mysql_error());

$row_test = mysql_fetch_assoc($result);

$totalRows_test = mysql_num_rows($result);

 

 

 

 

Also, you may want to use two different variables here instead of reusing the one, makes it easier to troubleshoot.

Link to comment
Share on other sites

Hey there,

 

thats brilliant thank you very much for helping me its greatly appreciated :)

 

 

I made the suggested change and it works a charm....

 

In order to test it fully i added a Repeat region to display all of the records by using:

 <?php do { ?>
  <?php echo $row_test['AnimalID']; ?>
  <?php } while ($row_test = mysql_fetch_assoc($test)); ?>

 

This would then hopefully display all the AnimalIDs of the records....

 

I must be doing something totally wrong as i get another Resource error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Woodside\test8.php on line 32

 

I have created many repeat regions before so am confussed why the hell its not working??

 

Thank you all for your help so far, i certainly wouldnt have got to where i am with out your support :)

Link to comment
Share on other sites

Hey there,

 

thats brilliant thank you very much for helping me its greatly appreciated :)

 

 

I made the suggested change and it works a charm....

 

In order to test it fully i added a Repeat region to display all of the records by using:

 <?php do { ?>
  <?php echo $row_test['AnimalID']; ?>
  <?php } while ($row_test = mysql_fetch_assoc($test)); ?>

 

This would then hopefully display all the AnimalIDs of the records....

 

I must be doing something totally wrong as i get another Resource error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Woodside\test8.php on line 32

 

I have created many repeat regions before so am confussed why the hell its not working??

 

Thank you all for your help so far, i certainly wouldnt have got to where i am with out your support :)

in his code use

 

<?php } while ($row_test = mysql_fetch_assoc($result)); ?>

 

instead of your

<?php } while ($row_test = mysql_fetch_assoc($test)); ?>

Link to comment
Share on other sites

Hi there,

 

Thank You very much indeed, thats it!!

 

Thanks again to everybody for helping me!!

 

Its all for my university project and im going to give you peeps a mention and dedication because you all been so kind!

 

Thanks again

 

Tom

 

p.s. Topic Solved doesnt appear at bottom of my threads anymore?? Tried looking at forum post on it and it said should be there now??

 

:)

Link to comment
Share on other sites

Ok cool, ill look out for that 1..

 

Ive just done so more testing and it isnt properly working....It only displays the default (ORDERBY ID) records....When i select Name or Type option from drop down nothing happens???

 

Oh man, how gutting..  Feel really crap now :(

 

Heres the code with all the modifications (please NOTE included drop down code and added table to display results):

 <?php require_once('Connections/woodside.php'); ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
if (isset($_GET['orderby'])) { 
$orderby= $_GET['orderby']; 
}else{ $orderby="AnimalID"; }
//CHECKING TO SEE IF THE PERSON HAS CHOSEN TO ORDER BY A CERTAIN FIELD
?>
       <select name=orderby>
<option value="-">Select one...</option>
<option value="ID" onclick="window.location='test8.php?orderby=AnimalID';">ID</option>
<option value="name" onclick="window.location='test8.php?orderby=Name';">Name</option>
<option value="animaltype" onclick="window.location='test8.php?orderby=AnimalTypeID';">Animal Type</option>
</select>
<?php
mysql_select_db($database_woodside, $woodside);

$test= ("SELECT * FROM `animal` ORDER BY `{$orderby}` DESC");
$result = mysql_query($test) or die(mysql_error());
$row_test = mysql_fetch_assoc($result);
$totalRows_test = mysql_num_rows($result);
?>
<?php do { ?>
  <?php echo'<table width="10" border="0">';
echo '<tr>';
    echo '<td>Animal ID</td>';
    echo'<td>Animal Type</td>';
    echo'<td> </td>';
    echo '<td> </td>';
    echo '<td> </td>';
  echo '</tr>';
  echo'<tr>';
    echo'<td>'; 
echo $row_test['AnimalID']; 
echo'</td>';
    echo'<td>';
echo $row_test['AnimalTypeID'];
echo'</td>';
    echo'<td> </td>';
    echo'<td> </td>';
    echo'<td> </td>';
  echo'</tr>';
echo'</table>';
?>
  <?php } while ($row_test = mysql_fetch_assoc($result)); ?>
</body>
</html>

 

If anybody can help still that be sweet, but i think im loosing will to live.

 

Thank You

 

 

Link to comment
Share on other sites

Not as strange as you think.  For one, you are using javascript for your select/options.  If JS is turned off, you will have problems.

 

Second, IE and FF treat forms differently, so one may ignore errors while the other halts on it.

Link to comment
Share on other sites

Try this:

 

Change:

       <select name=orderby>
<option value="-">Select one...</option>
<option value="ID" onclick="window.location='test8.php?orderby=AnimalID';">ID</option>
<option value="name" onclick="window.location='test8.php?orderby=Name';">Name</option>
<option value="animaltype" onclick="window.location='test8.php?orderby=AnimalTypeID';">Animal Type</option>

</select>

 

To:

<form name="reorder" action="test8.php" method="GET">
<select name="orderby">
<option "-">Select one...</option>
<option value="AnimalID" onclick="document.reorder.submit();">ID</option>
<option value="Name" onclick="document.reorder.submit();">Name</option>
<option value="AnimalTypeID" onclick="document.reorder.submit();">Animal Type</option>
</select></form>

 

Tell me if that works any better

Link to comment
Share on other sites

Hi there,

 

I tried removing back ticks & JavaScipt is enabled on IE7...Also using

<?php ini_set('error_reporting',E_ALL);?>

and NO errors are generated on either browser at all???

 

Also... I changed the drop down code to your suggestion with form and there is no change?? Still woirks fine with Firefox (both with and without suggested change) and NOT with IE7.

 

Im 100% IE7 has JS enabled as i use it on other pages fine!

 

Surely the code cant be far off as it is working perfectly in Firefox??

 

Thanks :)

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.