...

View Full Version : NEED QUICK HELP with php sort dropdown



1337hovie
02-10-2010, 09:48 PM
ok so i have a tag board, and i need to have it sort by wahtever i select in a drop down. Tag board= functional & perfect. Drop down 'sort by' option = not functional.

no errors pop up.
idk what is wrong

here's my code

<?
define('HOST', "xxxxx");
define('USER', "xxxxxx");
define('PASS', "xxx");
define('DB', "xxxxxx");

function doTags()
{
$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))
{
?>
<?php

$selected = array();

$orderby = $_GET[orderby];
if(!$orderby) { $orderby = 'order by tag_date DESC'; }

if($orderby == 'cid')
{
$orderby_query = "order by tag_cid asc";
}
else if($orderby == 'date')
{
$orderby_query = "order by tag_date DESC";
}
else if($orderby == 'name')
{
$orderby_query = "order by tag_name";
}
else { unset($orderby); }

// 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 * from tagboard $orderby_query";

// SQL code goes here..

?>
<table align="center" width="100%" bgcolor="#FFFFFF">
<tr>
<td><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><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868">(<? echo stripslashes($arrRow['tag_cid']); ?>)</span><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> </td>
</tr>
<tr>
<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">
<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><br> <? echo board.php?do=tags" ?></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">&nbsp;</td>
</tr>
<tr>
<td>CID:</td>
<td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Entry:</td>
<td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Sort by:</td>
<td><a href="sort">NAME</a> | <a href="sort">CID</a> | <a href="sort">DATE</a></td>
<td>&nbsp;</td>
</tr>
</table>
<br><input type="submit" value="Submit Entry" class="inputformsub"><br>


Sort by
<form method=get style="display: inline;" name='orderby_form'>
<select name=orderby onChange="orderby_form.submit();">
<option value='name' <?php print $selected[$orderby]; ?>>Name</option>
<option value="cid">CID</option>
<option value="date">Date</option>
</select>
</form></td>
</tr>

</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();
}
?>


THANK YOU!!
jt

Fumigator
02-10-2010, 10:48 PM
Your code is all messed up. You need to follow it through carefully to see what you're doing wrong.

1. You build a query string
1. You call mysql_query() on this query string and retrieve a resultset out of the tagboard table
2. You loop through the resultset
3. Inside this loop, you evaluate the selected sort option and build another query string (but you never use this query string anywhere)
4. Also inside this loop, you echo the resultset data

You can see that you need to evaluate the selected sort option as part of the construction of the initial query string, rather than putting that inside the resultset loop.

1337hovie
02-10-2010, 10:56 PM
i am very new to php. Can you point me in a direction of what to change/add?
ive tried googling everywhere, and even got a damn book but still nothing. if you can please just show me what to do (or if it isnt too much of a trainwreck, just correct it a post back - i know thats alot to ask, but i had time and knew how..im sure id be glad to help someone who doesnt know out). thanks.

when i select it, it refreshes page like its doing something but still doesnt sort.
i tihnk im like one or two words off or sometyinhg.

updated code:



<?php

$selected = array();
$orderby = $_GET['orderby'];
$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 'default':
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 ' . $orderby_query;

// SQL code goes here..

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

function doTags()
{
$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 align="center" width="100%" bgcolor="#FFFFFF" border="1">
<tr>
<td><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><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span> - <span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868">(<? echo stripslashes($arrRow['tag_cid']); ?>)</span> - <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> </td>
</tr>
<tr>
<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>Sort by
<form method=get style="display: inline;" name='orderby_form'>
</form></td>
</tr>

</form><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">&nbsp;</td>
</tr>
<tr>
<td>CID:</td>
<td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Entry:</td>
<td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Sort by:</td>
<td><a href="sort"><span style="display: inline;">
<select name=orderby onChange="orderby_form.submit();">
<option value='name' <?php print $selected[$orderby]; ?>>Name</option>
<option value="cid">CID</option>
<option value="date">Date</option>
</select>
</span></a></td>
<td><input type="submit" value="Submit Entry" class="inputformsub"></td>
</tr>
</table>
<br>
</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();
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum