Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the DESC and ASC function with images

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Hi all. I have a table whereby the table columns are sorted according to what table column is clicked. If I click it a second time it will sort those contents in the opposite direction (the usual stuff).

So, I have that bit sorted, and have an image declared according to what direction the content is being sorted at. But the problem is that all the table column contain this image. How can I ensure that only the table column selected to sort contains the arrow pointing up or down (depending on the ACS or DESC function).

Here is my source that does the above but has the image on all table column headers where the img src is called.

Code:
	$orderby = $_GET['o']; 
	

	
	$dir=$_GET['d']; 
	if(!isset($orderby)){ 
		$orderby = "SalesID"; 
	} 
	
	if(!isset($dir)){ 
		$dir = "ASC"; 
	} 
	if(strcmp($dir,"ASC")!=0 and strcmp($dir,"DESC")!=0){ 
		$dir = "DESC"; 
	} 
	
	if($orderby == NULL)
	{
		$order="SalesID";
	}
	$query = "SELECT * FROM leads ORDER BY $orderby $dir"; 
	$result=mysql_query ($query) or die ("Oh oh"); //(mysql_error()); 
	if(strcmp($dir,"ASC")==0){ 
        $ddir = "DESC"; 
        $src="down"; 
    }else{ 
        $ddir = "ASC"; 
        $src="up"; 
    } 
	
	$colourcount=1;

.....

echo "<table class='ex' cellpadding='4' cellspacing='0'>\n";
	echo "<tr><td class='header' align='center' ?> onClick="MyWindow=window.open('reglist.php?o=SalesID&d=<?=$ddir?>','_parent');"><? echo "ID <img src='images/$src.gif'></td>

<td class='header' ?>onClick="MyWindow=window.open('reglist.php?o=CustAddress&d=<?=$ddir?>','_parent');"><? echo "Customer Name <img src='images/$src.gif'></td>

<td class='header' ?> onClick="MyWindow=window.open('reglist.php?o=CustAddress&d=<?=$ddir?>','_parent');"><? echo "Address <img src='images/$src.gif'></td>

<td class='header' ?> onClick="MyWindow=window.open('reglist.php?o=CustTel&d=<?=$ddir?>','_parent');"><? echo "Contact No <img src='images/$src.gif'></td>

<td class='header' ?> onClick="MyWindow=window.open('reglist.php?o=CustStatus&d=<?=$ddir?>','_parent');"><? echo "Status <img src='images/$src.gif'></td>

<td class='header' ?> onClick="MyWindow=window.open('reglist.php?o=CustDescription&d=<?=$ddir?>','_parent');"><? echo "Description <img src='images/$src.gif'></td>

<td class='header' ?> onClick="MyWindow=window.open('reglist.php?o=AgentAssigned&d=<?=$ddir?>','_parent');"><? echo "Agent <img src='images/$src.gif'></td></tr>\n";
 
In the onclick function set a paramater which tells the server which field to set as the sort by

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Yep, got that bit already ;)

You can see the example of the arrows at
What I would like is instead of having the image source on every table header is to only have it appear on the column that was clicked :)

Funnily enough, I dreamt about getting a solution from you guys last night! Sad innit? Dreaming about PHP, of all things ;)
 
its not pretty but it works, just change the echo "sort $dir" for image.
Code:
<?php
// me sh*t
// include('config.inc.php');
mysql_select_db('test'); // change me

    $orderby = $_GET['sort'];
    $ddir=$_GET['ddir'];

    if(!isset($orderby)){
        $orderby = "mod_time"; // change me
    }

    if(!isset($ddir)){
        $dir = "ASC";
    }
   if($ddir==1){
   $dir='ASC';
   $ddir='0';
   }else{
   $dir='DESC';
   $ddir='1';
   }
    if($orderby == NULL)
    {
        $order="SalesID";
    }
// change query again to what you need
    $query = "SELECT * FROM date_test ORDER BY $orderby $dir";
    $result=mysql_query ($query) or die ("Oh oh"); //(mysql_error());

    $colourcount=1;

// my stuff ...
$columns=@mysql_num_fields($result);
$res_rows=mysql_num_rows($result);

	echo "<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">\n";

	$row=0;

// display field names

	for ($i = 0; $i < mysql_num_fields($result); $i++) {

		print "<th><a href=$_SERVER[PHP_SELF]?sort=".mysql_field_name($result,$i)."&ddir=".$ddir.">".mysql_field_name($result,$i);
		if (mysql_field_name($result,$i) == $orderby){
		echo " sort, $dir";
		}
		echo "</a></th>\n";

	}

	while ($myrow = @mysql_fetch_array($result)){

// start the results display table

		$row++;

		if($row%2){

			$color="red";

		}else{

			$color="green";

		}

		echo "<tr>";

		for ($i = 0; $i < ($columns); $i++) {

			echo "<td bgcolor=\"$color\"> $myrow[$i] </td>\n";

		}

		echo "</tr>\n";

	}

	mysql_free_result($result);

?>

hopefully it'll set a spark of inspiration off for you!

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Excellent, cheers dude. I'm sure I can change it slightly to work with mine.

Thanks again, I really appreciate everyones help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top