It is currently Sat Feb 04, 2012 3:34 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Need Help Sorting Table Columns with Database Fields
PostPosted: Sun Nov 07, 2004 8:55 am 
Offline

Joined: Sat May 08, 2004 7:54 pm
Posts: 108
Location: Seattle
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:
<?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>

?>

_________________
Ethical Web Design Matters


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 07, 2004 10:52 pm 
Offline

Joined: Sat Jul 17, 2004 12:01 am
Posts: 637
Location: California, US
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.

_________________
DB_DataObject_FormBuilder - The database at your fingertips


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 08, 2004 12:50 am 
Offline

Joined: Sat May 08, 2004 7:54 pm
Posts: 108
Location: Seattle
papercrane wrote:
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.


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

Thanks for the tip!

_________________
Ethical Web Design Matters


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  
cron