PDA

View Full Version : sort data and reload on same page



gnossos
12-18-2003, 07:47 AM
it's been some time since I've mucked around with php and I've totally forgotten how to go about doing this. I have a page that will pull data from a mysql table. I want to provide 2 links to the user to allow them to change the way the data is sorted but I can't really remember how to go about doing that so that it is reloaded onto the same page.
any instruction/suggestions would be helpful, thanks!

--on another note, currently I'm just using root and admin password to access my mysql stuff, would it be more advisable security wise to add another user and use them (I'm planning on putting the user/host/pw into a text file with only local permissions and just reading from that shortly, but haven't gotten to that yet).--
thanks much.

jalal
12-18-2003, 08:00 AM
create two links on the page:

href="mypage.php?sort=date"
href="mypage.php?sort=name"

then when the page is being created by PHP, you can have PHP sort it according to what the GET values are.

HTH

gnossos
12-18-2003, 08:19 AM
so when the user first gets to the page and there is no sort order defined would I just create a default value for the variable that would hold that get information or...? (sorry, it really has been a long time), e.g.,
user will get to page through a normal hyper link and the data will be displayed in a default order, there will be those two links that will allow the user to change that order back and forth, therefore, I'd have to assign a default value to whatever variable I was going to use to define the sort order so something like ...~
if(!$_GET[sort]){
$variable = value;
}
else{
$variable=$_GET[sort];
}

?
thanks much.

wildjokerdesign
12-18-2003, 09:15 AM
You might want to install or grab the code for this board and take a look at how they do it. They use it both on the search and each page which is why you see the drop down menu at the bottom of a post.


if(!$_GET[sort]){
$variable = value;
}
else{
$variable=$_GET[sort];
}

Seems like that should work. You could do the reverse and set $variable first and then look to see if there is a $_GET[sort] that should change it.

The board uses $HTTP_GET_VARS['sort'] when checking. I am pretty new to php so not sure why the diffrence in methods. I know they are sticklers for not takeing short cuts but I belive that is because so many work on the project and they want to keep it clean and understandable. I have been using thier code along with my books to learn. It is a big help to me to be able to see a working example and the logic behind it.

wildjokerdesign
12-18-2003, 09:42 AM
Just thought I would add the link to the php manual. It has been a big help to me to keep it bookmarked for easy refrence.

http://www.php.net/manual/en/index.php

jalal
12-18-2003, 01:02 PM
Try:


$sortorder = "date";
if( isset( $_GET['sort' ) ) {
$sortorder = $_GET['sort'];
}

gnossos
12-18-2003, 01:16 PM
ok,
since I don't remember didly, perhaps someone's eyes are better then mine, I've whipped up rather quickly a little page her to do what I want it to, however when I navigate to the page I get absolutely nothing - I would think that the first echo would at least show up. If anyone sees something obviously off about it I would really appreciate it.
ha, jalal, just saw your above post after I posted this, that snippet is exactly what I ended up doing.

note - since my if($_get['sort']) might be off, I did try commenting that out but still just got a blank page when I navigate to the page.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Filing Schedule</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<div>
<div style="float: left"><a href="statefiling.php?sort=date">Sort by date</a></div>
<div style="float: right"><a href="statefiling?sort=state">Sort by state</a></div>
<table>
<tr><th>Due Date</th><th>State</th><th>Filing Period</th></tr>

<?php
$sort = "state";

if(isset($_GET['sort'])){
$sort = $_GET['sort'];
}

echo "script opened";

$db_name = "xyz";
$table_name="xyz";
$host="localhost";
$dbuser="xyz";
$dbpass="xyz";

$connection = @mysql_connect($host, $dbuser, $dbpass) or die(myssql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

echo ="connected successfully";

$sql = "SELECT * FROM $table_name ORDER BY $sort ASC";
$result = @mysql_query($sql, $connection) or die(mysql_error());

echo "queried successfully";

if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>$row['date']</td><td>$row['state']</td><td>$row['period']</td>"
echo "</tr>";
}
} else {
echo "<h1>there was no data</h1>";
}

/* free result set */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
?>

</table>
</div>
</body>
</html>



thanks!

wildjokerdesign
12-18-2003, 01:41 PM
Easy one start with this:

<?php

You simply have
<?

You forgot the php.

gnossos
12-18-2003, 01:47 PM
wild joker,
thanks, but I've tried both ways, technically you should be able to use either <? or <?php
or <script language="php">

almost forgot, also have other scripts in another folder that start with <?
only and work fine.

it seems like there is almost something wrong with the file itself or...? I changed it around so that there was straight html up until the end of the table header declarations and I still get nothing when I load up the page - since php is an interpreted language I should in theory whatever data up until the error takes place correct?, therefore I should be getting at least that bit of html?? a bit confused now.

--well I guess it's not the actual file either since I opened a new file using pico via ssh and retyped everything and saved it as filename.php (after deleting old one) to no avail, still nothing which completly baffels me---

wildjokerdesign
12-18-2003, 02:37 PM
Like you I am not real good at this. The only thing I can see is in this part

echo "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css"/>
<html>
<head>";


You have done an echo and enclosed it with quotes but with in that you have quotes. Do you somehow need to escape those quotes withing the echo quotes? Maybe with a \ ? or try using a single quote instead. Is that echo all on one line?

gnossos
12-18-2003, 02:41 PM
joker,
yeah I should have escaped that, I'll edit the post with the code in it as I've changed it around a bit so some html comes first and it still does nothing but give me a blank page when I load it up.

wildjokerdesign
12-18-2003, 02:44 PM
Example

echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML .1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>";
echo "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>";
echo "<head>";
echo "<title>test layout</title>";
echo "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />";
echo "<meta http-equiv='Content-Style-Type' content='text/css'/>";
echo "<html>";
echo "<head>";


Each echo is on seprate line.

gnossos
12-18-2003, 02:49 PM
joker, thanks, yeah those were off (part of doing in pico and then opening it in notepad to paste in),
the code I posted above has been changed to reflect the current state of things and I'm even more confused as to why the above doesn't spit out at least something.

wildjokerdesign
12-18-2003, 02:49 PM
I am real bad when it comes to DOCTYPES could you simply use? ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Could the Doctype be throwing things off?

gnossos
12-18-2003, 03:00 PM
joker, I thought about that but then I have that same exact doctype included in another page and it works just fine.

wildjokerdesign
12-18-2003, 03:54 PM
Just been testing it out on my site with some modifications. I was taking it step by step and got to:

$sql = "SELECT * FROM $table_name ORDER BY $sort ASC";
$result = @mysql_query($sql, $connection) or die(@mysql_error('result'));

it still displayed the first to echos of "script opened and connected successfully but hit a bump on quiried successfully. That couls be that I changed some of the stuff to work with one of my existing databases but thought I would let you know. Still working through it.

wildjokerdesign
12-18-2003, 04:41 PM
Ok the code below is how I got it to work on mine. It may have be simply this line which is corrected here:

echo "<td>" . $row['post_id'] . "</td><td>" . $row['post_time'] . "</td><td>" . $row['forum_id'] . "</td>";

Originallay it was missing the ; on the end plus I added in the quotes and space dot space for the values.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Filing Schedule</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<div style="float: left"><a href="statefiling.php?sort=post_id">Sort by date</a></div>
<div style="float: right"><a href="statefiling?sort=forum_id">Sort by state</a></div>
<table>
<tr><th>Due Date</th><th>State</th><th>Filing Period</th></tr>

<?php
$host = 'localhost';

$dbname = 'xxx';
$dbuser = 'xxx;
$dbpass = 'xxx;
$table_name = xxx';

$sort = 'xxx';
if(isset($_GET['xxx'])){
$sort = $_GET['xxx'];
}
echo"script opened";

$connection = @mysql_connect($host, $dbuser, $dbpass) or die(@mysql_error('connection'));
$db = @mysql_select_db($dbname, $connection) or die(@mysql_error('db'));
echo"connected successfully";

$sql = "SELECT * FROM $table_name ORDER BY $sort ASC";
$result = @mysql_query($sql, $connection);
if ($result){echo"queried successfully<br />";}
else {echo"queried failed";}

if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['xxx'] . "</td><td>" . $row['xxx'] . "</td><td>" . $row['xxx'] . "</td>";
echo "</tr>";
}
} else {
echo "<h1>there was no data</h1>";
}

/* free result set */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);

?>


</table>
</body>
</html>

I think I also removed one of your very first <div> but do not think that had anything to do with it. I had got rid of the die after $result and used an if instead because that kept the page from showing blank. I also got a blank page when the echo was wrong. I had my table name set wrong for a long time which kept killing things at the $sql when I fixed that I got a success on the $result and no blanks. That is when I found the echo problem. I hope all this makes sense and helps.[/code]

gnossos
12-18-2003, 07:50 PM
wow joker,
thanks much I really appreciate that, I wonder why the or die.... didn't work on the query?, I also wonder why correcting stuff inside the <? ?> brackets would change the viewing as in my mind, since there was a bit of html that came first, something should have displayed even if the php was completely bollocks'd up.
thanks for all that work! Again I reiterate that westhost ought to bloody well pay you or at least give you free hosting. I'll give that fix a go in the morning. Thanks much!

wildjokerdesign
12-18-2003, 08:28 PM
Your welcome :wink:

squidier
12-19-2003, 02:53 AM
It might help to edit your php.ini file (in /ftp/usr/lib) and change display_errors to on.
That way you'll get php error reporting instead of a blank page, then you'll know exactly where the problem is.
display_errors on used to be default, it isn't in 2.0.
Remember to turn display_errors off when you go live.

gnossos
12-19-2003, 05:46 AM
squidier, thanks I turned php_errors on in php.ini, and joker, I used the code you posted above to test and I still get a blank page when navigating to the page and that's with the code above and php_errors turned on??

--changed a few things here and there and sort of got things working - minus the sort (that's doing some funky stuff) --

I think I noticed that the zend optimizer is installed (thought i saw that in one of the files somewhere), my previous experience with php was that, as it is an interpreted language, you get output up to the error but that doesn't seem to be the case here?

squidier
12-19-2003, 06:15 AM
The first time I tried to turn on the errors I got it wrong. There's a 'display_errors off' in the comments before the real one - did you change that instead.
You can test it out by putting something like
<?PHP
echo "a
?>
in a file on its own - that will definitely give an error if you've turned the errors on correctly.

wildjokerdesign
12-19-2003, 08:53 AM
gnossos,
In the other php you have running on the site are you accessing a mysql database?

The only thing I can think that may be wrong is that maybe when you transfered my code to your editor that you may have gotten some extra returns the most obvious culprit of this would be this line:

echo "<td>" . $row['xxx'] . "</td><td>" . $row['xxx'] . "</td><td>" . $row['xxx'] . "</td>"; Not sure if that should affect or not I know that something like the $sql can be on more then one line but wonder if an echo can. I could not find a "rule" for this but in the examples I have looked at they have held to that format. In the same line also make sure you have space dot space in between each quoted bit and variable.

I guess you could try to change the $db and $connection to something other then the die so that the script continues and echos a fail instead like I did on the $result just incase for some reason there is something in those that is not working.

gnossos
12-19-2003, 10:07 AM
joker,
thanks much, I've managed to get things straightened out now. One more question (never ending). I'm trying to set it up so that I read the user and pass from a text file with local read permission only and I've successfully read the user and pass from the file into the appropriate variables, however, those aren't being accepted by the connection, does explode add any extraneous characters or slashs or what have you?
thanks!

--stripslahes worked for me--

another one on top of this, related to this page: if you go here: http://69.36.167.110/test/statefiling2.php

at least in firebird, when you first hit the page, you can see the page flashing as the rows are being built and this strikes me as quite off - php shouldn't be so slow that it's actually visible as it builds those rows from the query (at least in my past experiences it hasn't been!?!) has anyone else noticed a behaviour similar to this?

well then...I guess my file that I wanted to be only accessible to local, eg rest of the world can't touch it, isn't working - just tested it by loading up the txt file in a browser window and it loaded up the text file. I have read and write enabled only to owner but I guess I need to chmod it to something else. Anyone have any suggestions? (I've forgotten what it should be to allow only local files access to the file).
thanks much.

just to provide exmaple: my permissions on that file look like:
-rw --- ---

wildjokerdesign
12-19-2003, 12:56 PM
I think it sounds like you answered your first question.

On the permissions thing.... could you name .htsomething? In your httpd.conf any file that starts with .ht is not readable by the public and was wondering if that would be the "easy" way to do it. I may not be following what you are doing. Why isn't your username passwords in a mysql database? If it is a matter of getting them in the database you can use phpMyAdmin to read a text file into your database.

I did not see what you where talking about on the "flashing" but I was viewing with IE 6 on windows.

gnossos
12-19-2003, 12:59 PM
joker,
I'm talking about for my php scripts, i.e., the user and password needed to connect to the database. I'll give it a try using ht styled name. Joker,
I tried that using a file named .htetc., etc., and that did the trick. I'm still curious why one can't do that with a normal txt file as giving other's and users 0 permissions should result in the same thing really.

wildjokerdesign
12-19-2003, 01:12 PM
The more I think of it the settings are such that anything that starts with a "dot" is not readable by the public but not sure on that. Glad it worked for you. I have always bee a bit confused on the permissions thing. I bet one of the users like jalal, torrin, squidier or fayez would know. I know enough to get myself in trouble as a rule :).

torrin
12-19-2003, 03:47 PM
well then...I guess my file that I wanted to be only accessible to local, eg rest of the world can't touch it, isn't working - just tested it by loading up the txt file in a browser window and it loaded up the text file. I have read and write enabled only to owner but I guess I need to chmod it to something else. Anyone have any suggestions? (I've forgotten what it should be to allow only local files access to the file).
thanks much.

just to provide exmaple: my permissions on that file look like:
-rw --- ---


Glad it worked for you. I have always bee a bit confused on the permissions thing. I bet one of the users like jalal, torrin, squidier or fayez would know. I know enough to get myself in trouble as a rule .

Hey everybody. Correct me if I'm wrong here, but here goes.

Here are a couple of questions.
1. You say the permission is 600. Who owns the file?
2. Is the owner of the file the same user that is running the apache process?
3. Is the file in your website directory?

If 1 and 2 are the same user and the answer to 3 is yes, then the file is probably world readable. When a user browses your website, they are effectively the user(there are restrictions) that apache is running as. That is one of the reasons why there are installations where they have apache running from user "nobody" or "www" or some other user that doesn't have access to the complete filesystem. So my suggestion is the put any file that you don't want to be world readable in some other directory.

The .ht works because in the apache config file there is a rule that tells apache to not anyone download the .ht file.

I've been reluctant to answer because I'm still on Westhost 1.0. So I don't know the specifics about how everything is setup.

wildjokerdesign
12-19-2003, 05:04 PM
I think you are right no the money torrin. I wish they would get you switched to 2.0. :) In 2.0 when you look at ownership of files there is root then root and also username then vuser. The second it what occures when we seem to upload or modify something. Don't know if that explains anything or not.