0

Dear All,

I am a new web, I would like all of you kindly help me with code of sorting column heading.


Thanks in advance!


Kindly regards,

Bunhok

2
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Bunhok
0

Dear All,

I am a new web, I would like all of you kindly help me with code of sorting column heading.


Thanks in advance!


Kindly regards,

Bunhok

I would start out with something like this. You might find some of this a little confusing, let me know if you any questions.

<? include("connection.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>
</head>
<?
$searchquery = ""; //if you intend to add search to this page

// columns that you can order by
$orderbycols = array();
$orderbycols["fname"] = "first name";
$orderbycols["lname"] = "last name";
$orderbycols["phone"] = "phone";
$orderbycols["username"] = "user name";
$orderbycols["address"] = "address";

// columns that are dificult to order by
$nonorderbycols = array();
$nonorderbycols["hobbies"] = "favorite hobbies";
$nonorderbycols["signature"] = "signature";

// merge the column names for the query
$querysel = array_merge($orderbycols, $nonorderbycols);

// declare order by variable
$orderby = "";
if(isset($_GET['order']) && in_array(strtolower($_GET['order']), array_keys($orderbycols)))
{
	$orderby .= " order by '" . strtolower($_GET['order']) . "'";
	// if we are ordering by desc values
	if(isset($_GET['desc']) && $_GET['desc'] == "true")
	{
		$orderby .= " desc";
	}
}

// main query
$query = "select " . implode(", ", array_keys($querysel)) . " from table inner join other_table using(colname_pk)" . $searchquery . $orderby;
$result = mysql_query($query);
?>

<body>
	<table>
		<tr>
			<?
			// columns that can be ordered
			foreach($orderbycols as $key=>$value)
			{
			?>
				<td><?
					$hreforder = "";
					if(trim($orderby) != "" && strtolower($_GET['order']) == $key)
					{
						$hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
					}
				?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
			<?
			}
			?>
			<?
			// columns that cannot be ordered
			foreach($nonorderbycols as $key=>$value)
			{
			?>
				<td><? echo ucwords($value); ?></td>
			<?
			}
			?>
		</tr>
		<?
			// display database loop here
		?>
	</table>
</body>
</html>
<?
mysql_close();
?>
0

Dear sir,

It still has an error with this code.

It appears like this.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\working\sorting.php on line 74

What should we use loop? with while right?

<?
			
// display database loop here
while($item = mysql_fetch_array($result))		?>

<tr><td><?=$item['Jobid']?>"></td></tr>

Thanks so much for supporting me.

Kindly regards,

Bunhok

0

Dear sir,

I modify ready but not sorting the databse now it display connectly

<? require("Dbconnect/db_connect.php"); ?>
<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>
</head>
<?
$searchquery = ""; //if you intend to add search to this page
// columns that you can order by
$orderbycols = array();
$orderbycols["Jobtitle"] = "Job Title";
$orderbycols["Jobcompany"] = "Jobcompany";
// declare order by variable
$orderby = "";
if(isset($_GET) && in_array(strtolower($_GET), array_keys($orderbycols)))
{
$orderby .= " order by '" . strtolower($_GET) . "'";
// if we are ordering by desc values
if(isset($_GET) && $_GET == "true")
{
$orderby .= " desc";
}
}
// main query
$query = "select " . implode(", ", array_keys($orderbycols)) . " from tbljob" . $searchquery . $orderby;
$result = mysql_query($query);
?>
<body>
<table>
<tr>
<?
// columns that can be ordered
foreach($orderbycols as $key=>$value)
{
?>
<td><?
$hreforder = "";
if(trim($orderby) != "" && strtolower($_GET) == $key)
{
$hreforder = isset($_GET) && $_GET == "true"?"":"&desc=true";
}
?><a href="<? echo basename($_SERVER); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
<?
}
?>
</tr>
<?
$sql = "SELECT * FROM tbljob  order by Jobid desc";
$result = mysql_query($sql);
while($item = mysql_fetch_array($result))
{
$bgc=($bgc=="#F7F7F7")?"#ffffff" : "#F7F7F7";

?>

<tr><td>
<?=$item?>
</tr>
<? }
?>

</body>
</html>
<?
mysql_close();
?>

Edited by happygeek: fixed formatting

0

This works, didn't realize that you would have capitals in your column names.

<?
if($dbconn = mysql_connect('servername', 'username', 'pass'))
{
	mysql_select_db('dbname', $dbconn);
}
?>
<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>
</head>
<?
$searchquery = ""; //if you intend to add search to this page

// columns that you can order by
$orderbycols = array();
$orderbycols["Jobtitle"] = "Job Title";
$orderbycols["Jobcompany"] = "Jobcompany";

// declare order by variable
$orderby = "";
if(isset($_GET['order']) && in_array($_GET['order'], array_keys($orderbycols)))
{
	$orderby .= " order by " . strtolower($_GET['order']);
	// if we are ordering by desc values
	if(isset($_GET['desc']) && $_GET['desc'] == "true")
	{
		$orderby .= " desc";
	}
}

// main query
$query = "select " . implode(", ", array_keys($orderbycols)) . " from tbljob" . $searchquery . $orderby;
echo $query . "<br />";
$result = mysql_query($query);
?>

<body>
<table>
<tr>
<?
// columns that can be ordered
foreach($orderbycols as $key=>$value)
{
?>
	<td><?
	$hreforder = "";
	if(trim($orderby) != "" && $_GET['order'] == $key)
	{
		$hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
	}
	?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
	<?
}
?>

</tr>
<?
$bgc = "";
while($item = mysql_fetch_array($result))
{
	$bgc=($bgc=="#F7F7F7")?"#ffffff" : "#F7F7F7";	
	?>	
	<tr>
	<td><?=$item['Jobtitle']?></td>
	<td><?=$item['Jobcompany']?></td>	
	</tr>
	<? 
}

?>
</body>
</html>
<?
mysql_close();
?>
0

Dear Mr. R0bb0b,

Thanks so much for your kind support me all the time. Now it works fine.

Thanks you som much from the bottom of my heart.


Kindly regards,

Bunhok

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.