View Full Version : reordering content
roddyinnovations.com/?action=resources&cat=81
hello, I was wondering how to make that. Where the visitor is able to reorder the content posted and the content is like that in those boxes. Can anyone please help me!! Please and thank you!:) Also how would I be able to reorder from a star rater that I have?
BLiZZaRD
12-30-2007, 08:31 PM
Easiest way is to use PHP/SQL mix. When an item is added you supply the SQL with name, date, star rating, whatever, in a table(s).
Then use php to run a search on the SQL when a link is clicked. The php will tell the page (post, self) to reload with the new data. Sort by date/name/rating etc. asc or desc.
Blizzard you are a genious! Ok, now where do I start? Please and thank you!
BLiZZaRD
12-31-2007, 01:36 AM
After a bit of research, we can do this in pure PHP if you like.
Read up on the php sort() functions here (http://us.php.net/manual/en/function.sort.php) with particular attention to msort() and ksort().
:D easier!
it appears that msort is now know as array_multisort()
Ok, I have read that link and also array_multisort() and I think I get the idea, so now what?
BLiZZaRD
12-31-2007, 08:27 PM
well, now you set up your page. Forget the php for right now. make the links in regular html like click here for by date, click here for by name, etc.
Once you have that you choose the best method of sort() and put the php code in. Then you can go back to your html links and add the php code with all the variables so it will know what to do. :)
http://www.athletes4excellence.com/sortexample.php
I am not sure how to do the links, and also on "Ajax Help" from your amazing help, I figured out how to fix the star raters! Thanks a lot!
BLiZZaRD
01-02-2008, 02:34 AM
Okay, where you have this:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Sorth by: A to Z | Z to A | Top Rated to Least Rated | Least Rated to Top Rated </td>
</tr>
</table>
Ignoring the typos and the fact you are using tables...
<td>Sort by: <a href="http://www.athletes4excellence.com/sortexample.php#"> A to Z</a> | <a href="http://www.athletes4excellence.com/sortexample.php#">Z to A</a> |
etc etc..
</td>
That will make them basic links for now. Then you can work on the php coding for the sorting.
http://www.athletes4excellence.com/sortexample.php
Something unrelated to the sorting and reordering. Would you know why everytime I add the rater, there is a horizontal line under it?
BLiZZaRD
01-02-2008, 03:43 AM
inthe rating.css the first part is this:
.ratingblock {
display:block;
border-bottom:1px solid #999;
padding-bottom:8px;
margin-bottom:8px;
}
the border-bottom line is the culprit.
If you don't want it remove that whole line.
Thanks! Wow, you truly are incredible! Now back to the reordering/sorting.
Leafy
01-02-2008, 09:36 AM
Thanks! Wow, you truly are incredible! Now back to the reordering/sorting.
Assuming you have fetched all of the required data, it looks like this, assuming "Title" is the name of the column you store the name of the photo.
<?php
$link = mysql_connect("localhost",$user,$pass);
mysql_select_db("pictures");
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
The new links would be
<a href="sortexample.php?sorttype=1&sortname=Name">Z to A</a>
<a href="sortexample.php?sorttype=0&sortname=Name">A to Z</a>
<a href="sortexample.php?sorttype=1&sortname=Rating">Top rated to Least</a>
<a href="sortexample.php?sorttype=0&sortname=Rating">Least rated to Top</a>
So I need to set up a database? And do I put that php code anywhere in that page? Thanks Leafy.
BLiZZaRD
01-05-2008, 05:28 PM
Yup, set up a DB. (Or amend the one you already have for the star rater ;) )Change the things in the script to match (username, password, etc.)
You should place the code ABOVE the <html>
so it would look something like:
<?php
$link = mysql_connect("localhost",$user,$pass);
mysql_select_db("pictures");
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<?php
$link = mysql_connect("localhost",$DATABASE USER,$DATABASE PASSWORD);
mysql_select_db("DATABASE");
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
this is what I changed is this right?
BLiZZaRD
01-05-2008, 09:02 PM
One of 2 ways...
Either define $user and $pass and $database with the terms:
$user = "username"
etc..
Or, just like you have it above just remove the $'s for those 3 spots.
These are the errors that I am getting
"
Warning: mysql_connect() [function.mysql-connect]: Access denied for user: 'nobody@localhost' (Using password: NO) in /home/athletes/public_html/sortexample.php on line 2
Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user: 'nobody@localhost' (Using password: NO) in /home/athletes/public_html/sortexample.php on line 3
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/athletes/public_html/sortexample.php on line 3
"
BLiZZaRD
01-06-2008, 09:14 AM
Okay, well first.. check your connections.
Is your SQL server set up to be accessed as "localhost"? Some are, a lot aren't.
Next, check your username and password are exactly correct.
Lastly, check that the user has access to do the required functions for that DB.
Let's say that my password, username, database and everything is test. Is the code right if it is like this? And also the connections are right.
<?php
$link = mysql_connect("localhost",$test_test,$test);
mysql_select_db("test_test");
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
<?php require('_drawrating.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>
<script type="text/javascript" language="javascript" src="js/behavior.js"></script>
<script type="text/javascript" language="javascript" src="js/rating.js"></script>
<link rel="stylesheet" type="text/css" href="css/rating.css" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Sorth by: <a href="sortexample.php?sorttype=1&sortname=Name">Z to A</a>
<a href="sortexample.php?sorttype=0&sortname=Name">A to Z</a>
<a href="sortexample.php?sorttype=1&sortname=Rating">Top rated to Least</a>
<a href="sortexample.php?sorttype=0&sortname=Rating">Least rated to Top</a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%"><p>funny picture </p>
<p>CONTENT</p></td>
<td width="86%">Rating: <?php echo rating_bar('8xxa','5'); ?>
<p> </p></td>
</tr>
<tr>
<td><p>cool picture </p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8x41','5'); ?></td>
</tr>
<tr>
<td><p>test picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('84854','5'); ?></td>
</tr>
<tr>
<td><p>test 1 picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8454','5'); ?></td>
</tr>
<tr>
<td><p>5 cool picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8454654a','5'); ?></td>
</tr>
<tr>
<td><p>number test</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('154545','5'); ?></td>
</tr>
<tr>
<td># character testCONTENT</td>
<td>Rating: <?php echo rating_bar('8xxa','5'); ?></td>
</tr>
<tr>
<td><p>hello test </p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('815454','5'); ?></td>
</tr>
<tr>
<td><p>1 test</p>
<p> CONTENT</p></td>
<td>Rating: <?php echo rating_bar('822a','5'); ?></td>
</tr>
<tr>
<td><p>z test</p>
<p> CONTENT</p></td>
<td>Rating: <?php echo rating_bar('454xa','5'); ?></td>
</tr>
</table>
</body>
</html>
So? Is that code right or wrong?
BLiZZaRD
01-13-2008, 12:30 PM
No, you need to define them somewhere outside the function.
$db_user = "test_test";
$db_pass = "test";
$db_table = "test";
Then use $db_user instead of $test_test, etc...
Sorry, but I'm not understanding... For the spots where it says $db_user, $db_pass do I put my database info? Also what's $db_table? Please and thank you.
BLiZZaRD
01-13-2008, 05:23 PM
this:
$link = mysql_connect("localhost",$test_test,$test);
mysql_select_db("test_test");
Is saying variable "link" is equal to connecting to SQL database where database location is localhost, user on database is $test-test, and password is $test. open the database named test_test.
Anything with the $ in front is a variable. If the variable isn't defined, it doesn't know what to put there. So you have to define them before it will work.
My database info does not contain anything with "$" so would I still have to put a $ infront of it?
BLiZZaRD
01-13-2008, 05:57 PM
okay, allow me to try again...
in PHP there are variables. Variables are defined by a preceding $.
So If I say:
$name = "BLiZZ";
I am telling php that anytime from now on in this script that is sees "$name" to use "BLiZZ" instead.
So If I write:
echo' $name;
The screen will show:
BLiZZ
If I write:
Echo' 'My name is $name. I like my name $name. My mother gave me the name $name';
I would show on the page:
My name is BLiZZ. I like my name BLiZZ. My mother gave me the name BLiZZ
So, in your case...
$link = mysql_connect("localhost",$test_test,$test);
It is looking for what exactly $test_test and $test are = to. You haven't defined $test_test so there is nothing it is = to.
If your database username is "Mike" and the password is "house" the you would do seomthing like this:
$test_test = "Mike";
$test = "house";
$link = mysql_connect("localhost",$test_test,$test);
THEN the script can have something to replace the variable with.
Cant I just get rid of the "$" and just type in the exact database information?
Master_script_maker
01-13-2008, 06:28 PM
yes you can
$link = mysql_connect("host","user","pass");
BLiZZaRD
01-13-2008, 09:45 PM
Of course I only said that about 11 posts ago.. :rolleyes:
One of 2 ways...
Either define $user and $pass and $database with the terms:
$user = "username"
etc..
Or, just like you have it above just remove the $'s for those 3 spots.
Can you tell me why it doesn't work please? Thanks.
thetestingsite
01-25-2008, 05:09 PM
Define "doesn't work". Personally, I think you should go and look over http://php.net and http://www.php-mysql-tutorial.com so that you can learn PHP/MySQL coding. If you need help on a specific item, come back and ask about that issue; but don't have us do all the work for you. Just my two cents.
When I click on the "A-Z" or "Z-A" or the others, it does not reorder.
thetestingsite
01-25-2008, 07:02 PM
In the code you posted, I don't see anywhere that you called a query to the database (other than assigning $q as the query to be sent). The content will not sort the way you have the page set up because you have it hardcoded into the page instead of pulling that data from the db.
Hope this helps.
Umm, well I changed it. And I put quotes around them. Here's the code. I changed the actual database access just for security.
<?php
$link = mysql_connect("localhost","USERNAME_USERNAME","PASSWORD");
mysql_select_db("USERNAME_DATABASE);
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
<?php require('_drawrating.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>
<script type="text/javascript" language="javascript" src="js/behavior.js"></script>
<script type="text/javascript" language="javascript" src="js/rating.js"></script>
<link rel="stylesheet" type="text/css" href="css/rating.css" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Sorth by: <a href="sortexample.php?sorttype=1&sortname=Name">Z to A</a>
<a href="sortexample.php?sorttype=0&sortname=Name">A to Z</a>
<a href="sortexample.php?sorttype=1&sortname=Rating">Top rated to Least</a>
<a href="sortexample.php?sorttype=0&sortname=Rating">Least rated to Top</a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%"><p>funny picture </p>
<p>CONTENT</p></td>
<td width="86%">Rating: <?php echo rating_bar('8xxa','5'); ?>
<p> </p></td>
</tr>
<tr>
<td><p>cool picture </p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8x41','5'); ?></td>
</tr>
<tr>
<td><p>test picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('84854','5'); ?></td>
</tr>
<tr>
<td><p>test 1 picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8454','5'); ?></td>
</tr>
<tr>
<td><p>5 cool picture</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('8454654a','5'); ?></td>
</tr>
<tr>
<td><p>number test</p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('154545','5'); ?></td>
</tr>
<tr>
<td># character testCONTENT</td>
<td>Rating: <?php echo rating_bar('8xxa','5'); ?></td>
</tr>
<tr>
<td><p>hello test </p>
<p>CONTENT</p></td>
<td>Rating: <?php echo rating_bar('815454','5'); ?></td>
</tr>
<tr>
<td><p>1 test</p>
<p> CONTENT</p></td>
<td>Rating: <?php echo rating_bar('822a','5'); ?></td>
</tr>
<tr>
<td><p>z test</p>
<p> CONTENT</p></td>
<td>Rating: <?php echo rating_bar('454xa','5'); ?></td>
</tr>
</table>
</body>
</html>
thetestingsite
01-25-2008, 07:26 PM
Ok, let me try to explain again. You are logging into the database, but you are not getting any content from that database. Your code should look something like this instead of what you have above:
<?php
$link = mysql_connect("localhost","USERNAME_USERNAME","PASSWORD");
mysql_select_db("USERNAME_DATABASE);
$type = ((isset($_GET["sorttype"])?$_GET["sorttype"]:"1") == "1")?"DESC":"ASC";
/*
this is really the equivelant of
$type = "";
if(isset($_GET["sorttype"]))
$type = $_GET["sorttype"];
else
$type = "1";
$type_cmp = $type;
if($type_cmp == "1")
$type = "DESC";
else
$type = "ASC";
unset($type_cmp);
*/
$col = ($_GET["sortname"]);
$q = "SELECT * FROM records ORDER BY `$col` $type";
?>
<?php require('_drawrating.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>
<script type="text/javascript" language="javascript" src="js/behavior.js"></script>
<script type="text/javascript" language="javascript" src="js/rating.js"></script>
<link rel="stylesheet" type="text/css" href="css/rating.css" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Sorth by: <a href="sortexample.php?sorttype=1&sortname=Name">Z to A</a>
<a href="sortexample.php?sorttype=0&sortname=Name">A to Z</a>
<a href="sortexample.php?sorttype=1&sortname=Rating">Top rated to Least</a>
<a href="sortexample.php?sorttype=0&sortname=Rating">Least rated to Top</a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$result = mysql_query($q);
if (!mysql_num_rows($result)) {
?>
<tr>
<td colspan="2"><h4> No Results Found! </h4></td>
</tr>
<?php
}
else {
while ($info = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['Name'];?></td>
<td><?php echo $info['Rating'];?></td>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
Hope this helps.
Thanks, I get this now. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/athletes/public_html/sortexample.php on line 48
No Results Found!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.