Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
CLick on Title to Sort Ascending - Descending
Old 07-30-2008, 11:34 AM CLick on Title to Sort Ascending - Descending
Junior Talker

Posts: 2
Trades: 0
This php works and displays a table. What I want to do here is that if i click the title of the column it should do ascending and descending order with evry click.

Please help me work with my code.???

PHP Code:
 <?php 
$host 
"localhost"
$user "xxxx"
$pass "xxxx"
$dbname "xxx"

$connection mysql_connect("localhost""root""") or die("Cannot connect to MySQL server: " mysql_error()); 
$db_selected mysql_select_db('xxx, $connection); 

$dt= DATE('
Y-m-d'); 
$data = mysql_query("SELECT * FROM xxx where date = '
$dt'") 
or die(mysql_error()); 
Print "<table border=0 cellspacing=0 cellpadding=0 style='
width:100%'>"; 


$info= mysql_query($query); 
$num_data = mysql_num_rows($data); 

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

$row = mysql_fetch_array($data); 

$rows[$i]['
BSCID'] = "<td style='text-align:left'>" . $row['BSCID'] . "</td>"; 

$rows[$i]['
Cellid'] = "<td style='text-align:left'>" . $row['Cellid'] . "</td>"; 

$rows[$i]['
Seiz'] = "<td>" . $row['Seiz'] . "</td>"; 
$rows[$i]['
Drps'] = "<td>" . $row['Drps'] . "</td>"; 
$rows[$i]['
RF'] = "<td>" . $row['RF'] . "</td>"; 
$rows[$i]['
Oth'] = "<td>" . $row['Oth'] . "</td>"; 
$rows[$i]['
Traff'] = "<td>" . $row['Traff'] . "</td>"; 
$rows[$i]['
Total'] = "<td>"."(" . $row['Total'] .")". "</td>"; 

$rows[$i]['
DIT'] = "<td>" . $row['DIT'] . "</td>"; 

$rows[$i]['
IHFL'] = "<td>" . $row['IHFL'] . "</td>"; 
$rows[$i]['
TCD'] = "<td>" . $row['TCD'] . "</td>"; 
$rows[$i]['
TCA'] = "<td>" . $row['TCA'] . "</td>"; 
$rows[$i]['
SDD'] = "<td>" . $row['SDD'] . "</td>"; 
$rows[$i]['
SDA'] = "<td>" . $row['SDA'] . "</td>"; 
$rows[$i]['
TAVL'] = "<td>" . $row['TAVL'] ."%". "</td>"; 
$rows[$i]['
Drate'] = "<td>" . $row['Drate'] . "</td>"; 

if($row['
dropflag']) 
$rows[$i]['
Drps'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066">',$rows[$i]['Drps']); 

if($row['
trafflag']) 
$rows[$i]['
Traff'] = str_replace('<td>','<td style="font-weight:bold;color: #CC0066">',$rows[$i]['Traff']); 

if($row['
ihfflag']) 
$rows[$i]['
IHFL'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066">',$rows[$i]['IHFL']); 

if($row['
tchaflag']) 
$rows[$i]['
TAVL'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066 ">',$rows[$i]['TAVL']); 



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

if($i % 2) 

print '
<TR bgcolor="#ffffff">'; 

else 

print '
<TR bgcolor="#CCCCCC">'; 


foreach($rows[$i] as $cell) 
echo $cell; 


print "</TR>\n"; 

?>
jadi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-31-2008, 11:07 AM Re: CLick on Title to Sort Ascending - Descending
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Just noticed a missing ',
PHP Code:
<?php 
$host 
"localhost"
$user "xxxx"
$pass "xxxx"
$dbname "xxx"

$connection mysql_connect("localhost""root""") or die("Cannot connect to MySQL server: " mysql_error()); 
$db_selected mysql_select_db('xxx'$connection); 

$dtDATE('Y-m-d'); 
$data mysql_query("SELECT * FROM xxx where date = '$dt'"
or die(
mysql_error()); 
Print 
"<table border=0 cellspacing=0 cellpadding=0 style='width:100%'>"


$infomysql_query($query); 
$num_data mysql_num_rows($data); 

for(
$i=0$i $num_data$i++) { 

$row mysql_fetch_array($data); 

$rows[$i]['BSCID'] = "<td style='text-align:left'>" $row['BSCID'] . "</td>"

$rows[$i]['Cellid'] = "<td style='text-align:left'>" $row['Cellid'] . "</td>"

$rows[$i]['Seiz'] = "<td>" $row['Seiz'] . "</td>"
$rows[$i]['Drps'] = "<td>" $row['Drps'] . "</td>"
$rows[$i]['RF'] = "<td>" $row['RF'] . "</td>"
$rows[$i]['Oth'] = "<td>" $row['Oth'] . "</td>"
$rows[$i]['Traff'] = "<td>" $row['Traff'] . "</td>"
$rows[$i]['Total'] = "<td>"."(" $row['Total'] .")""</td>"

$rows[$i]['DIT'] = "<td>" $row['DIT'] . "</td>"

$rows[$i]['IHFL'] = "<td>" $row['IHFL'] . "</td>"
$rows[$i]['TCD'] = "<td>" $row['TCD'] . "</td>"
$rows[$i]['TCA'] = "<td>" $row['TCA'] . "</td>"
$rows[$i]['SDD'] = "<td>" $row['SDD'] . "</td>"
$rows[$i]['SDA'] = "<td>" $row['SDA'] . "</td>"
$rows[$i]['TAVL'] = "<td>" $row['TAVL'] ."%""</td>"
$rows[$i]['Drate'] = "<td>" $row['Drate'] . "</td>"

if(
$row['dropflag']) 
$rows[$i]['Drps'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066">',$rows[$i]['Drps']); 

if(
$row['trafflag']) 
$rows[$i]['Traff'] = str_replace('<td>','<td style="font-weight:bold;color: #CC0066">',$rows[$i]['Traff']); 

if(
$row['ihfflag']) 
$rows[$i]['IHFL'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066">',$rows[$i]['IHFL']); 

if(
$row['tchaflag']) 
$rows[$i]['TAVL'] = str_replace('<td>','<td style="font-weight:bold;color:#CC0066 ">',$rows[$i]['TAVL']); 



for(
$i=0$i $num_data$i++) { 

if(
$i 2

print 
'<TR bgcolor="#ffffff">'

else 

print 
'<TR bgcolor="#CCCCCC">; 


foreach($rows[$i] as $cell) 
echo $cell; 


print "</TR>\n"; 

?>
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 08-11-2008, 09:48 AM Re: CLick on Title to Sort Ascending - Descending
Skilled Talker

Posts: 61
Trades: 0
Just have a field to keep the column you want to order by and one for the current order status (ascending or descending).

Then when you click on the link do something like this (ordering by title in my example):
if($column == 'title')
{
$asc_desc = 'ASC'
} else {
$asc_desc = 'DESC'
}

$query = sprintf("SELECT * FROM table
ORDER BY %s %s", $column, $asc_desc);

Play around a bit with it and I'm sure you can figure it out. Let me know if you have more questions.

Pod.
just_a_pod is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to CLick on Title to Sort Ascending - Descending
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13304 seconds with 12 queries