Jump to content

PLEASE HELP! Drop down SORT BY!


1337hovie

Recommended Posts

Ok so i have a simple tagboard, displaying tags from a db. i just need a drop down selection to pick how i want to sort the entries.

my code is giving me errors, if someone can just please help me. ive been on this project for over 6 hours and just need to get it done and over with.

 

its got sometihng to do with a scope. im new to php. if someone can just re-code this and post back so i can get on with the damn thing. Thanks in advance.

 

cashing in all my 'guy code' credits on this one, fellas...that means u must help me, by guy regulations. :D

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL|E_STRICT); 
$selected = array();
$orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'tag_date';
$orderby_query    = null;
switch ($orderby) {
    case "cid":      
        $orderby_query = "order by tag_cid asc";
        break;
    case "name":
        $orderby_query = "order by tag_name";
        break;
    case "default":
        break;       
    case "date":      
        $orderby_query = "order by tag_date DESC";      
        break;
    }
    // If $orderby was valid set the selected sort option for the form.
    if($orderby) { 
        $selected[$orderby] = "selected";} 
        // Now run your SQL query with the $orderby_query variable.  Ex:
        $query = 'SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard SORT by tag_cid';

// SQL code goes here..

define('HOST', "xx");
define('USER', "xxx");
define('PASS', "xxx");
define('DB', "xxx");

function doTags()
{
global $orderby_query;
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

$stSql = "SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard $orderby";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }

if (mysql_num_rows($arrResult) >= 1)
{

while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>

<table width="100%" height="47" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td width="30%" height="30"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666">  [<? echo date("F j, Y, g:i a", $arrRow['tag_date']); ?>]  </span></td>
<td width="15%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span></td>
<td width="10%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868"><? echo stripslashes($arrRow['tag_cid']); ?></span></td>
<td width="45%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #000000"><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></span></td>
</tr>
<tr>
  <td height="11" >  </td>
  <td></td>
  <td></td>
  <td></td>
</tr>
</table>
<?
}
} else {
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
<?
}
}

function doBoard()
{
?>
<html>
<head>
<title>Control Center Tag board</title>
<style>
body {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF; 
}
td {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
}
.inputform {
BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
}
.inputformsub {
BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
}
</style>
</head>
<body>
<div><div align="center"><br>
<form action="board.php?do=Insert" target="tag_box" method="POST">
<table width="757" height="250" bgcolor="#FFFFFF" style="border: 1px solid #000000">
<tr>
<td width="501" height="150"><IFRAME name="tag_box" height="350" width="751" src="board.php?do=Tags" frameborder="0"></IFRAME></td>
</tr>
<tr>
<td height="100" align="center"><br>
  <table width="326" border="0">
    <tr>
      <td width="39">Name:</td>
      <td width="251"><input type="text" name="__name" size="20" maxlength="55" class="inputform"></td>
      <td width="22"> </td>
    </tr>
    <tr>
      <td>CID:</td>
      <td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
      <td> </td>
    </tr>
    <tr>
      <td>Entry:</td>
      <td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
      <td> </td>
    </tr>
    <tr>
      <td>Sort by:</td>
      <td><a href="sort"><span style="display: inline;">
        <select name=orderby onChange="orderby_form.submit();">
          <option value='tag_name' <?php print $selected[$orderby]; ?>>Name</option>
          <option value="tag_cid">CID</option>
          <option value="tag_date">Date</option>
        </select>
      </span></a></td>
      <td><input type="submit" value="Submit Entry" class="inputformsub"></td>
    </tr>
  </table>
  <br>
  <div id="cid" style="display: block"></div>
<div id="date" style="display: none"></td></tr></table></form></div>
</body>
</html>
<?
}

function doInsert($__name, $__entry, $__cid)
{
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

$__name = addslashes($__name);
$__url = addslashes($__cid); 
$__entry = addslashes($__entry);

if (empty($__name))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} elseif (empty($__entry))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} else {

$stSql = "INSERT INTO tagboard(tagId, tag_name, tag_cid, tag_entry, tag_date) values('', '$__name', '$__cid', '$__entry', ".time().")";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }

if (mysql_affected_rows() == 1)
{
?>
<meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
<?
} else {
echo "Unable to Add Entry to Database";
?>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
}
}
} 

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

switch ($do) {
case "Board":
doBoard();
break;
case "Tags":
doTags();
break;
case "Insert":
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__cid']);
break;
default:
doBoard();
}
?>

my errors:

Notice: Use of undefined constant orderby - assumed 'orderby' in /home/content/t/m/s/tmsolutions/html/board.php on line 6

 

Notice: Undefined index: orderby in /home/content/t/m/s/tmsolutions/html/board.php on line 6

 

Notice: Undefined variable: orderby in /home/content/t/m/s/tmsolutions/html/board.php on line 7

 

Notice: Undefined variable: orderby in /home/content/t/m/s/tmsolutions/html/board.php on line 50

Link to comment
https://forums.phpfreaks.com/topic/191780-please-help-drop-down-sort-by/
Share on other sites

$stSql = "SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard $orderby_query";

 

u defined $orderby_query as global in your function body but u forget to define and $orderby so thats why it's undefined but anyways I think u just need use $orderby_query so...

well. thats what i currently have, and its still not working.

i just need to add a drop down menu to SORT the results.

 

end result:

tagbaord

with drop down sort.

 

what is the best way to do this? or how do i define $orderby? so then the rest will work?

it was changed: this is the updated code:

 

 

 

i just need a tagboard and a drop down button to sort it, Thats it. Sader, if you can just help me re-code this to work i can be done with this project thats has taken over 8 hours.

im new to php, btw.

<?php
error_reporting(E_ALL);// Allowed Order Bys, Map these keys to these field names
$orderMap = array(    'date'     => 'tag_date',
			   'cid'     => 'tag_cid',
			   'name'    => 'tag_name',
			   );

$orderBy = 'date';    // Default Key

if (array_key_exists($_GET['orderby'], $orderMap) {
$orderBy = $_GET['orderby'];
}
$query = "select * from tagboard ORDER BY " . $orderMap[$orderBy]; /* No SQL injection is possible, because we verified it was in our array */
 if($orderby == 'date') {    $orderby_query = "order by tag_date";}else if($orderby == 'cid') {    $orderby_query = "order by tag_cid";}else if($orderby == 'name') {    $orderby_query = "order by tag_name";}else {    $orderby = null;} 

// SQL code goes here..

define('HOST', "xxx");
define('USER', "xx");
define('PASS', "xx");
define('DB', "xx");

function doTags()
{
global $orderby_query;
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

$stSql = "SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard $orderby_query";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }

if (mysql_num_rows($arrResult) >= 1)
{

while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>

<table width="100%" height="47" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td width="30%" height="30"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666">  [<? echo date("F j, Y, g:i a", $arrRow['tag_date']); ?>]  </span></td>
<td width="15%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span></td>
<td width="10%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868"><? echo stripslashes($arrRow['tag_cid']); ?></span></td>
<td width="45%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #000000"><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></span></td>
</tr>
<tr>
  <td height="11" >  </td>
  <td></td>
  <td></td>
  <td></td>
</tr>
</table>
<?
}
} else {
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
<?
}
}

function doBoard()
{
?>
<html>
<head>
<title>Control Center Tag board</title>
<style>
body {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF; 
}
td {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
}
.inputform {
BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
}
.inputformsub {
BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
}
</style>
</head>
<body>
<div><div align="center"><br>
<form action="board.php?do=Insert" target="tag_box" method="POST">
<table width="757" height="250" bgcolor="#FFFFFF" style="border: 1px solid #000000">
<tr>
<td width="501" height="150"><IFRAME name="tag_box" height="350" width="751" src="board.php?do=Tags" frameborder="0"></IFRAME></td>
</tr>
<tr>
<td height="100" align="center"><br>
  <table width="326" border="0">
    <tr>
      <td width="39">Name:</td>
      <td width="251"><input type="text" name="__name" size="20" maxlength="55" class="inputform"></td>
      <td width="22"> </td>
    </tr>
    <tr>
      <td>CID:</td>
      <td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
      <td> </td>
    </tr>
    <tr>
      <td>Entry:</td>
      <td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
      <td><select name="orderby" onChange="orderby_form.submit();">          <option value="name">Name</option>          <option value="cid" <?php if ($orderBy == 'cid') { echo 'selected'; } ?>>CID</option>          <option value="date" <?php if ($orderBy == 'date') { echo 'selected'; } ?>>Date</option>        </select> </td>
    </tr>
    <tr>
      <td>Sort by:</td>
      <td><a href="sort"><span style="display: inline;">
        <select name=orderby onChange="orderby_form.submit();">
          <option value='tag_name' <?php print $selected[$orderby]; ?>>Name</option>
          <option value="tag_cid">CID</option>
          <option value="tag_date">Date</option>
        </select>
      </span></a></td>
      <td><input type="submit" value="Submit Entry" class="inputformsub"></td>
    </tr>
  </table>
  <br>
  <div id="cid" style="display: block"></div>
<div id="date" style="display: none"></td></tr></table></form></div>
</body>
</html>
<?
}

function doInsert($__name, $__entry, $__cid)
{
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

$__name = addslashes($__name);
$__url = addslashes($__cid); 
$__entry = addslashes($__entry);

if (empty($__name))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} elseif (empty($__entry))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} else {

$stSql = "INSERT INTO tagboard(tagId, tag_name, tag_cid, tag_entry, tag_date) values('', '$__name', '$__cid', '$__entry', ".time().")";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }

if (mysql_affected_rows() == 1)
{
?>
<meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
<?
} else {
echo "Unable to Add Entry to Database";
?>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
}
}
} 

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

switch ($do) {
case "Board":
doBoard();
break;
case "Tags":
doTags();
break;
case "Insert":
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__cid']);
break;
default:
doBoard();
}
?>

ok heres what u can try

 

change

 

if (array_key_exists($_GET['orderby'], $orderMap) {	$orderBy = $_GET['orderby'];}

 

to

 

//logic would be if $_GET['orderby'] is not in valid array(possible some hack) then use some default order
if(!array_key_exists($_GET['orderby'], $orderMap)) 
{	
  $orderBy = SOME_DEFAULT_ORDER;//$_GET['orderby'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.