PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



Need Help Sorting Table Columns with Database Fields

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
Dave 5



Joined: 08 May 2004
Posts: 108
Location: Seattle

PostPosted: Sun Nov 07, 2004 7:55 am    Post subject: Need Help Sorting Table Columns with Database Fields Reply with quote

If you look at my page at http://www.geoworld.org/reference/people/ you'll see a table with a PHP script that gives it sortable columns. Suppose I modified this table so that only the FIRST column was displayed. Could the PHP script be modified so that you could still choose "Population" and the table would be rearranged by population, even though the Population column isn't visible? In other words, I'd like to be able to sort by database table fields, whether those fields are displayed as columns on my page or not.

If so, how should I modify the script below? Thanks.

[code:1]
<?php
$mycode = 'peo';
include (DATABASE CONNECTION);
echo '<head>';
include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/head/ref.php");
echo '</head>';
?>
<body>
<div class="formdiv">
<form action="index.php" method="GET">
<select name="order">
<option value="1">Country, etc.</option>
<option value="2">Population</option>
<option value="3">Nationality</option>
<option value="4">Nationality: Plural</option>
<option value="5">Nationality: Adjective</option>
</select>
<input type="radio" name="direction" value="0">
Ascending
<input type="radio" name="direction" value="1">
Descending
<input type="submit" name="submit" value="Submit">
</form>
</div>
<?php
$colors = array( '#eee', '', '#ff9', '', '#cff', '', '#cfc', '' );
$n=0;
$size=count($colors);

$result = mysql_query('select count(*) from cia_people');
if (($result) && (mysql_result ($result , 0) > 0)) {
// continue here with the code that starts
//$res = mysql_query ("SELECT * FROM type.....
} else {
die('Invalid query: ' . mysql_error());
}
{

$order = isset($_REQUEST['order']) ? intval($_REQUEST['order']) : 0;
switch($order)
{
case 1:
$order = 'Name';
break;
case 2:
$order = 'Pop';
break;
case 3:
$order = 'Nationality';
break;
case 4:
$order = 'NationalityPlural';
break;
case 5:
$order = 'NationalityAdjective';
break;
case 6:
default:
$order = 'Name';
break;
}
if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
// if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
$direction = ' DESC';
} else {
$direction = '';
}

$res = mysql_query('SELECT IDArea, Name, Pop, Nationality, NationalityPlural, NationalityAdjective
FROM cia_people as c
WHERE c.Nationality is not null
ORDER BY c.' . $order . $direction);

echo '<table class="sortphp" id="tab_cia_people_peo">
<thead>
<tr><th>Country</th><th class="gray">Population</th><th>Nationality</th>
<th class="gray">Nat. (Plural)</th><th>Nat. (Adjective)</th></tr>
</thead>
<tbody>';
//<!-- BeginDynamicTable -->
$rowcounter=0;
while ($row = mysql_fetch_array ($res)) {
$c=$colors[$rowcounter++%$size];
echo "<tr style=\"background-color:$c\"><". $_SERVER['PHP_SELF'] .'?id='. $row['IDArea'] ."><td>". $row['Name'] ."</td>
<td>" . number_format($row['Pop']) . "</td>
<td>". $row['Nationality'] ."</td>
<td>". $row['NationalityPlural'] ."</td>
<td>". $row['NationalityAdjective'] ."</td></tr>\n";
}
}
?>
</tr>
</tbody>

</table>

</body>
</html>

?>
[/code:1]
Back to top
View user's profile Send private message Visit poster's website
papercrane



Joined: 16 Jul 2004
Posts: 637
Location: California, US

PostPosted: Sun Nov 07, 2004 9:52 pm    Post subject: Reply with quote

Just don't display the other columns.....I don't see why you're having a problem with this. Remove them from the SELECT and remove their TD entries.
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Dave 5



Joined: 08 May 2004
Posts: 108
Location: Seattle

PostPosted: Sun Nov 07, 2004 11:50 pm    Post subject: Reply with quote

[quote="papercrane"]Just don't display the other columns.....I don't see why you're having a problem with this. Remove them from the SELECT and remove their TD entries.[/quote]

OK, problem solved. I had tried your suggestion earlier without success, but I just discovered a stupid mistake I made.

Thanks for the tip!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum