Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2006, 1:18 PM   #1
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
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>';
?>
i did a test to check the variable $view was recieving the right info from the form and it seems fine. i just cant seem to get it displaying the table once it is formated. it just displays it as it was.

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
piercy is offline   Reply With Quote
Old Apr 26th, 2006, 2:03 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 26th, 2006, 5:45 PM   #3
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
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
piercy is offline   Reply With Quote
Old Apr 26th, 2006, 6:02 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 3:41 AM   #5
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
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
piercy is offline   Reply With Quote
Old Apr 27th, 2006, 6:18 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 7:38 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 3:40 PM   #8
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
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
piercy is offline   Reply With Quote
Old Apr 27th, 2006, 3:50 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 4:23 PM   #10
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
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
piercy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC