Jump to content


Photo

Trying to sort by headers....


  • Please log in to reply
9 replies to this topic

#1 ccutla

ccutla
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 April 2006 - 04:17 PM

I am trying to be able to sort a table by the different column headings. The table is the result of a search of a mysql db and I have no idea how or where to start. Any suggestions???

Thanks a lot!

This is the results page of my search where I need the sort to happen:

php:


<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b>DT_STRING</b></td>
<td width="100"><b>ACCOUNT</b></td>
<td width="30"><b>ACCOUNT_TYPE</b></td>
<td width="150"><b>CLIENT_ID</b></td>
<td width="150"><b>USER_ID</b></td>
</tr>
<tr>
<td>
<? $hostname = "mysql"; // The Thinkhost DB server.
$username = ""; // The username you created for this database.
$password = ""; // The password you created for the username.
$usertable = "AUDIT"; // The name of the table you made.
$dbName = "AUDITMED"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");


//error message (not found message)begins
$XX = "No Record Found, to search again please close this window";

//query details table begins

$metode = $_POST['metode'];
$search = $_POST['search'];
$metode2 = $_POST['metode2'];
$search2 = $_POST['search2'];

$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%$search%' AND $metode2 LIKE '%$search2%'");

while ($row = @mysql_fetch_array($query))
{
$variable1=$row["DT_STRING"];
$variable2=$row["ACCOUNT"];
$variable3=$row["ACCOUNT_TYPE"];
$variable4=$row["CLIENT_ID"];
$variable5=$row["USER_ID"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("<td>$variable5</td>");
print ("</tr>");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
?>
</table>
</center>


Thanks again!

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 05:03 PM

Change this:
<td width="60"><b>DT_STRING</b></td>
<td width="100"><b>ACCOUNT</b></td>
<td width="30"><b>ACCOUNT_TYPE</b></td>
<td width="150"><b>CLIENT_ID</b></td>
<td width="150"><b>USER_ID</b></td>
to the following:
<td width="60"><b><a href="?sortby=DT_STRING">DT_STRING</a></b></td>
<td width="100"><b><a href="?sortby=ACCOUNT">ACCOUNT</a></b></td>
<td width="30"><b><a href="?sortby=ACCOUNT_TYPE">ACCOUNT_TYPE</a></b></td>
<td width="150"><b><a href="?sortby=CLIENT_ID">CLIENT_ID</a></b></td>
<td width="150"><b><a href="?sortby=USER_ID">USER_ID</a></b></td>
Now change the following code:
$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%$search%' AND $metode2 LIKE '%$search2%'");
to this:
//Create an array that sotes the possible values $_GET['sortby'] can hold
$sortvalues = array("DT_STRING", "ACCOUNT", "ACCOUNT_TYPE", "CLIENT_ID", "USER_ID");

//Check that $_GET['sortby'] is set and that the value it holds is in the $sortvalues array
if(isset($_GET['sortby']) && in_array($_GET['sortby'], $sortvalues))
{
    //set up the sortby variable that'll be used in the ORDER BY clause in the MySQL Query
    $sortby = $_GET['sortby'];
}
else
{
    // This is the defualt value if $_GET['sortby'] is not set or the value it 
    /// contains isn't in the $sortvalue array.
    $sortby = "DT_STRING";
}

$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%$search%' AND $metode2 LIKE '%$search2%' ORDER BY $sortby ASC");

Hoep that helps.

#3 ccutla

ccutla
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 April 2006 - 05:16 PM

Thanks for the reply! I put in the new code and it does try to sort when I click on the headers, but it does not return any results it shows up as the no records found error. The original search still works, but not the sort. I don't know if you have any idea of why that is, I am trying to see if the values for the $sortby are all correct. Any further suggestions would be appreciated greatly!

Thanks again!


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 05:46 PM

Chnage this:
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["DT_STRING"];
$variable2=$row["ACCOUNT"];
$variable3=$row["ACCOUNT_TYPE"];
$variable4=$row["CLIENT_ID"];
$variable5=$row["USER_ID"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("<td>$variable5</td>");
print ("</tr>");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
to:
// This is the proper way of checking whether your 
// MySQL query returned any results or not
if(mysql_num_rows($query) >= 1)
{
    while ($row = mysql_fetch_array($query))
    {
        $variable1=$row["DT_STRING"];
        $variable2=$row["ACCOUNT"];
        $variable3=$row["ACCOUNT_TYPE"];
        $variable4=$row["CLIENT_ID"];
        $variable5=$row["USER_ID"];
        //table layout for results

        print ("<tr>");
        print ("<td>$variable1</td>");
        print ("<td>$variable2</td>");
        print ("<td>$variable3</td>");
        print ("<td>$variable4</td>");
        print ("<td>$variable5</td>");
        print ("</tr>");
    }
}
else
{
    "Your search criterea returned no results";
}
Also change your mysql query part of the code to this:
$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%$search%' AND $metode2 LIKE '%$search2%' ORDER BY $sortby ASC") or die("Unable to perform query: "  . mysql_error());


#5 ccutla

ccutla
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 April 2006 - 07:22 PM

I did that but now when I click on a table heading it gives me this error:

"Unable to perform query: 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 'LIKE '%%' AND LIKE '%%' ORDER BY DT_STRING ASC' at line 1"

I don't know exactly what that means but if anybody does I could really use the help.

Thanks again!

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 08:56 PM

Try:
$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%{$search}%' AND $metode2 LIKE '%{$search2}%' ORDER BY '$sortby' ASC") or die("Unable to perform query: "  . mysql_error());

I've never used the LIKE clause, so I'm going in blind sorry. With all the examples I've seen with the LIKE Clause they never use the ORDER BY clause prehaps you cant use it with LIKE.

#7 ccutla

ccutla
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 April 2006 - 10:42 PM

Thanks for trying, I am still getting the exact same message when I do that so I don't know what the deal is. If you have any other suggestions, I am willing to try anything at this point. Thanks again!

#8 gavinandresen

gavinandresen
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 07 April 2006 - 12:48 AM

[!--quoteo(post=362390:date=Apr 6 2006, 05:42 PM:name=BoofBoof)--][div class=\'quotetop\']QUOTE(BoofBoof @ Apr 6 2006, 05:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for trying, I am still getting the exact same message when I do that so I don't know what the deal is. If you have any other suggestions, I am willing to try anything at this point. Thanks again!
[/quote]

You need to pass search and search2 (and any other variables from $_POST that matter) through in the 're-sort-me' column headers links.

So... you'd change $_POST['search'] to $_REQUEST['search'], and make the column headers:
<a href="?search=$search&search2=$search2&sortBy=BLAH">BLAH</a>
That will pass in search and search2 via GET (which is why you'd need to switch to _REQUEST-- you'll get them either from _POST or _GET).

You might think about changing the form that shows the page to use GET rather than POST, too-- in general, use GET whenever sending the form info doesn't change any data (e.g. it just displays lists of data), and use POST when it does (e.g. adding/editing something).

#9 ccutla

ccutla
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 07 April 2006 - 02:47 PM

Well, I have tried about everything and I still get an error message saying:
"Unable to perform query: 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 'LIKE '%$search%' AND LIKE '%$search2%' ORDER BY 'DT_STRING' ASC' at line 1"
It doesn't make sense to me, because I changed it so that it would search through including search and search2 in the column headers. Anybody else have any ideas? Here is what my script looks like at this point:

php:


<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b><a href="?search=$search&search2=$search2&sortby=DT_STRING">DT_STRING</a></b></td>
<td width="100"><b><a href="?search=$search&search2=$search2&sortby=ACCOUNT">ACCOUNT</a></b></td>
<td width="30"><b><a href="?search=$search&search2=$search2&sortby=ACCOUNT_TYPE">ACCOUNT_TYPE</a></b></td>
<td width="150"><b><a href="?search=$search&search2=$search2&sortby=CLIENT_ID">CLIENT_ID</a></b></td>
<td width="150"><b><a href="?search=$search&search2=$search2&sortby=USER_ID">USER_ID</a></b></td>
</tr>
<tr>
<td>
<? $hostname = "mysql"; // The Thinkhost DB server.
$username = ""; // The username you created for this database.
$password = ""; // The password you created for the username.
$usertable = "AUDIT"; // The name of the table you made.
$dbName = "AUDITMED"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");


//error message (not found message)begins
$XX = "No Record Found, to search again please close this window";

//query details table begins

$metode = $_REQUEST['metode'];
$search = $_REQUEST['search'];
$metode2 = $_REQUEST['metode2'];
$search2 = $_REQUEST['search2'];




$sortvalues = array("DT_STRING", "ACCOUNT", "ACCOUNT_TYPE", "CLIENT_ID", "USER_ID");


if(isset($_GET['sortby']) && in_array($_GET['sortby'], $sortvalues))
{

$sortby = $_GET['sortby'];
}
else
{

$sortby = "DT_STRING";
}


$query = mysql_query("SELECT * FROM AUDIT WHERE $metode LIKE '%{$search}%' AND $metode2 LIKE '%{$search2}%' ORDER BY '$sortby' ASC")
or die("Unable to perform query: " . mysql_error());



if(mysql_num_rows($query) >= 1)
{
while ($row = mysql_fetch_array($query))
{
$variable1=$row["DT_STRING"];
$variable2=$row["ACCOUNT"];
$variable3=$row["ACCOUNT_TYPE"];
$variable4=$row["CLIENT_ID"];
$variable5=$row["USER_ID"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("<td>$variable5</td>");
print ("</tr>");
}
}
else
{
"Your search criterea returned no results";
}
//end
?>
</table>
</center>





Thanks again for any ideas!


#10 blulagoon

blulagoon
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 April 2006 - 03:21 PM

[!--quoteo(post=362528:date=Apr 7 2006, 03:47 PM:name=BoofBoof)--][div class=\'quotetop\']QUOTE(BoofBoof @ Apr 7 2006, 03:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]

"Unable to perform query: 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 'LIKE '%$search%' AND LIKE '%$search2%' ORDER BY 'DT_STRING' ASC' at line 1"
It doesn't make sense to me, because I changed it so that it would search through including search and search2 in the column headers.
[/quote]

I still think that this error message means that your SQL query is putting $search and $search2 into the query string, rather than the values of $search and $search2.

Blu





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users