![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Apr 2006
Posts: 35
Rep Power: 0
![]() |
mysql table view require()or suggestions.
i am trying to create a form that will show a database table in the required format eg. name a-z or email a-z etc.. i would like this so that when the page loads up the table is displayed and then by selecting a drop down menu. you can change the format. so far i have been totally confused by doing this on one page. so i am trying to do it over 2(if you get what i mean).
This is my form: <html> <body> <form action="table.php" name="form" method="post"> <select name="view"> <option value="fullname">fullname a-z <option value="email"> email a-z </select> <input type="submit" name="submit"> </form> </body> </html> and this is the php code i have to interpret the form. $view = $_POST["view"];
print("$view");
$db = mysql_connect('localhost', 'guest', 'password');
mysql_select_db('test',$db);
$result = mysql_query('SELECT * FROM messages ORDER BY $view DESC',$db);
echo '<TABLE border="1">';
echo '<TR><TD><B>Full Name</B></td><TD><B>Email Address</B></td><TD><B>Date and time</B></td><TD><B>Message</td></B></TR>';
while($myrow = mysql_fetch_array($result))
{
echo '<TR><TD>';
echo $myrow['fullname'];
echo "<TD>";
echo $myrow['emailaddress'];
echo "<TD>";
echo $myrow['date'];
echo "<TD>";
echo $myrow['message'];
}
echo '</TABLE>';
?>please help me sort this one out or even better help me create a code thats all on one page ![]()
__________________
this forum rules you guys are great! thanks to all who help piercy |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You don't give quite enough information, but I suspect that your orderby is 'email' and your column name is 'emailaddress'. What's a poor, non-mindreading database to do?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2006
Posts: 35
Rep Power: 0
![]() |
database = test
table = messages table fields = fullname, emailaddress, date, message i would like to be able to view this information so that i can view it a-z by name and email. is this enough information? if not please ask me what you need to know.
__________________
this forum rules you guys are great! thanks to all who help piercy |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That's enough information. Did you not pluck information from mine? You have a column named 'emailaddress' and your are ordering by 'email' (the value you submit from the form). Ibedam', tain't the same thang, donchano. If that ISN'T the case, then your information presented to us is inaccurate. In that case, there's no such thing as enough.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Apr 2006
Posts: 35
Rep Power: 0
![]() |
yes ive changed that in my code but it still not ordering the table. its still just displaying the original order.
__________________
this forum rules you guys are great! thanks to all who help piercy |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Post new code.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
As a matter of fact, I whipped this up, and it works. I stayed as close to your code as possible, so fair warning, its ugly. See it here.
EDIT: Oh, you do realize you have "DESC" in there, right? <?php
if ($_POST ['view']) $view = $_POST ['view']; else $view = 'fullname';
require_once ("functions.php");
demoConnect ();
$query = "SELECT * FROM Demonstration ORDER BY $view DESC";
$result = mysql_query ($query) or die (mysql_error () . "query op<br/>");
?>
<!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>Sort Test</title>
</head>
<body>
<?php
echo '<TABLE border="1">';
echo '<TR><TD><B>Full Name</B></td><TD><B>Email Address</B></td><TD><B>Date and time</B></td><TD><B>Message</td></B></TR>';
while($myrow = mysql_fetch_array($result))
{
echo '<TR><TD>';
echo $myrow['fullname'];
echo "<TD>";
echo $myrow['emailaddress'];
echo "<TD>";
echo $myrow['date'];
echo "<TD>";
echo $myrow['message'];
}
echo '</TABLE>';
?>
<form action="sort.php" name="form" method="post">
<select name="view">
<option value="fullname">fullname a-z
<option value="emailaddress"> email a-z
</select>
<input type="submit" name="submit">
</form>
</body>
</html>
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Apr 2006
Posts: 35
Rep Power: 0
![]() |
okay thanks
and yes i realised i had DESC rather than ASC thing is i also want to do this in date/time order. for it to work in date/time it has to be DESC and for it to work on names and emails its gotta be ASC. so any ideas how i cna do this? im guessing it if statements.thanks again
__________________
this forum rules you guys are great! thanks to all who help piercy |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Sure, ifs. Just append what you want to the orderby variable. You can even send it as a separate thangamabobby. Sometimes I make two or more select widgets and offer the HOW as well as the WHAT to sort on. Sometimes I fix it where, when you click on a column heading, it sorts on that column. A second click reverses the ascending/descending modifier. That implies including client-side script, though, and sometimes I don't want to do that.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Apr 2006
Posts: 35
Rep Power: 0
![]() |
yeah i know what you mean but the clicking the top of the column seems a bit complex at the moment. althought while your here help me with my if statements i am sure i am doing it right but the page is not changing when i send the form. it just stays as the original. it wont even change to sort by name or email.
anyways heres the if stament i put together if ($view='date') {
$query = ("SELECT * FROM test.messages ORDER BY $view DESC");
} else {
$query = ("SELECT * FROM test.messages ORDER BY $view ASC");
}
$result = mysql_query ($query) or die (mysql_error () . "query op<br/>");
__________________
this forum rules you guys are great! thanks to all who help piercy |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|