Jump to content

Allow user to sort data?


wikedawsum

Recommended Posts

I have created a blog with an Editor's section to allow editors to view, edit, and publish blogs. I am looking for a way to allow the editor to sort stories by section, or published date. Currently, my query orders the results by published date.

 

This is the code for the page I would like to be able to sort:

<?php

// include function files for this application
require_once('include_fns.php'); 

session_start();

if (isset($_SESSION['auth_user'])){

$conn = mysql_connect("*****", "*****", "*****") 
   or die($msg_no_connect);
   mysql_select_db("*****") 
   or die(mysql_error());
   
   check_valid_user('');

  // Run query
$sql = "SELECT * FROM stories order by published";
    $res = mysql_query($sql);
    if(!$res){
     $err=mysql_error();
     echo $err;
     exit();
    }
}
else {
echo 'You are not authorized to view this page.';
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>The Pink Compass</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../css/style.css">
<!---if IE---->
<link rel="stylesheet" type="text/css" href="../../css/ie.css">
<!---end IF---->

<script type="text/javascript"><!--//--><![CDATA[//><!--

startList = function() {
if (document.all&&document.getElementById) {
	navRoot = document.getElementById("nav");
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {
				this.className+=" over";
			}
			node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
			}
		}
	}
}
}
window.onload=startList;

//--><!]]></script>

</head>

<body>

<div id="top_bar"></div>

<div id="wrapper">

<div id="container">

	<div id="header">

		<div id="menu">
			<ul id="nav">
				<li><div><a href=""><img src="../../images/menu_top.png"></a></div>
					<ul>
						<li><a href="../../index.php">Home Page</a></li>
						<li><a href="../../pages/about.php">About Us</a></li>
						<li><a href="../../pages/charity.php">Charity Corner</a></li>
						<li><a href="../../pages/events.php">Upcoming Events</a></li>
						<img src="../../images/menu_hr.png">
						<li><a href="../../pages/blogs/beauty.php">Beauty</a></li>
						<li><a href="../../pages/blogs/business.php">Business & Finance</a></li>
						<li><a href="../../pages/blogs/destinations.php">Destinations</a></li>
						<li><a href="../../pages/blogs/fashion.php">Fashion</a></li>
						<li><a href="../../pages/blogs/health.php">Health & Fitness</a></li>
						<li><a href="../../pages/blogs/home.php">Home & Gourmet</a></li>
						<li><a href="../../pages/blogs/women.php">Women of the World</a></li>
						<li><a href="../../pages/blogs/relationships.php">Relationships</a></li>
						<li><a href="../../pages/blogs/entrepreneur.php">Entrepreneur Experts</a></li>
						<li><a href="../../pages/blogs/spirit.php">Spirit & Balance</a></li>
						<li><a href="../../pages/blogs/workplace.php">The Workplace</a></li>
					</ul>
				</li>
			</ul>
		</div>

	</div>

	<div id="content">

		<div id="content_bottom">
		<h1>Welcome <?php echo $_SESSION['auth_user']; ?></h1>
		<h2>Editor Administration</h2>
			<p><?php include("adminmenu.php"); ?></p>
			<p><?php
    			if (mysql_num_rows($res) > 0) {
    			echo '<table border="0" width="755" cellpadding="5">';
    			echo '<tr><th><h2>Headline</h2></th><th><h2>Page</h2></th>';
      			echo '<th><h2>Author</h2></th><th><h2>Created</h2></th><th><h2>Last modified</h2></th><th><h2>Publish Date</h2></th></tr>';
			while ($row = mysql_fetch_assoc($res)) {
			echo '<tr><td>';
  			echo "{$row['headline']}";
			echo '</td><td>';
        		echo "{$row['page']}";
			echo '</td><td>';
			echo "{$row['writer']}";
			echo '</td><td>';
        		echo date('M d, h:i a', $row['created']);
			echo '</td><td>';
        		echo date('M d, h:i a', $row['modified']);
			echo '</td><td>';
			if ($row['published'])
        		{
          		echo ' [Published '.date('M d, h:i a', $row['published']).'] ';
			echo '[<a href="unpublish.php?story='.$row['id'].'">unpublish</a>]<br />';
        		}
        		else 
        		{
			echo "{$row['month']} {$row['year']}<br />";
          		echo ' [<a href="publish.php?story='.$row['id'].'">publish</a>] ';          		
        		}
			echo ' [<a href="edit_story.php?story='.$row['id'].'">edit</a>] ';
			echo '[<a href="delete_story.php?story='.$row['id'].'">delete</a>] ';
        		echo '[<a href="view_story.php?story='.$row['id'].'">view</a>]';
        		echo '</td></tr>';
   				}
   				echo '</table>';
			}
			else
   				echo 'You have not written any stories.';
			?></p>
		</div>

	</div>

	<div id="right">

		<div id="top_right">

		</div>

	</div>

</div>

<div id="footer">
	<?php include("../../footertext.php"); ?>
</div>

</div>



</body>
</html>

 

What I would like to do is add two links under the adminmenu that say "Sort by Section" and "Sort by Published Date". Is there a way to do this so that when the editor clicks on one of those links, it resorts the page? I have found several examples of sorting data, but cannot figure out how to modify them in order to accomplish this. Any help or suggestions is greatly appreciated.

 

Thank you,

wikedawsum

Link to comment
Share on other sites

What I do is in your Table headers

 

echo '<tr><th><h2>Headline</h2></th><th><h2>Page</h2></th>';

echo '<th><h2>Author</h2></th><th><h2>Created</h2></th><th><h2>Last modified</h2></th><th><h2>Publish Date</h2></th></tr>';

 

I create links there like page.php?sortby=author and in my page I use $_GET to get a sortby order if one is set.

Link to comment
Share on other sites

You would use the GET to find out how to sort:

 

<?php

$sortby = $_GET['sortby'];
//Don't forget to clean the variable!
$okay_values = array("publisher", "title", "author", "copyrightdate");
//Check to make sure value is okay & legal.
if (in_array($sortby, $okay_values)) {
//if its in the array, great! we can continue
$sql = "SELECT * FROM `stories` ORDER BY ".$sortby;
//You could also do more with ASC/DESC keys
}
?>

 

Just a rough outline of what you would do :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.