/    Sign up×
Community /Pin to ProfileBookmark

Sortable Table Images

So I have a php/mySQL sortable table, but to the right of the links that sort the table i want little arrow graphics that show the user whether they are sorting the list by ASC or DESC where and how do i put the code for the graphics

thanks kevin

here is the php code:

<?php

// This script retrieves all the records from the model_list table.
// This script paginates the query results.
// This script sorts the query results.

require_once (); // Connect to the db.

// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET[‘np’])) { // Already been determined.

$num_pages = $_GET[‘np’];

} else { // Need to determine.

// Count the number of records
$query = “SELECT COUNT(*) FROM model_list ORDER BY model_name ASC”;
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET[‘s’])) {
$start = $_
GET[‘s’];
} else {
$start = 0;
}

//Default column links.
$link1 = “{$_SERVER[‘PHP_SELF’]}?sort=mna”;
$link2 = “{$_
SERVER[‘PHP_SELF’]}?sort=beda”;
$link3 = “{$_SERVER[‘PHP_SELF’]}?sort=batha”;
$link4 = “{$_
SERVER[‘PHP_SELF’]}?sort=sfa”;

//Determine the sorting order
if (isset($_GET[‘sort’])) {

//Use existing sorting order.

switch ($_GET[‘sort’]) {

case ‘mna’:
$order_by = ‘model_name ASC’;
$link1 = “{$_SERVER[‘PHP_SELF’]}?sort=mnd”;
break;
case ‘mnd’:
$order_by = ‘model_name DESC’;
$link1 = “{$_SERVER[‘PHP_SELF’]}?sort=mna”;
break;
case ‘beda’:
$order_by = ‘bedrooms ASC’;
$link2 = “{$_SERVER[‘PHP_SELF’]}?sort=bedd”;
break;
case ‘bedd’:
$order_by = ‘bedrooms DESC’;
$link2 = “{$_SERVER[‘PHP_SELF’]}?sort=beda”;
break;
case ‘batha’:
$order_by = ‘baths ASC’;
$link3 = “{$_SERVER[‘PHP_SELF’]}?sort=bathd”;
break;
case ‘bathd’:
$order_by = ‘baths DESC’;
$link3 = “{$_SERVER[‘PHP_SELF’]}?sort=batha”;
break;
case ‘sfa’:
$order_by = ‘square_footage ASC’;
$link4 = “{$_SERVER[‘PHP_SELF’]}?sort=sfd”;
break;
case ‘sfd’:
$order_by = ‘square_footage DESC’;
$link4 = “{$_SERVER[‘PHP_SELF’]}?sort=sfa”;
break;

}

//Sort will be appended to the pagination links
$sort = $_GET[‘sort’];

} else { //Use the default sorting order.
$order_by = ‘model_name ASC’;
$sort = ‘mna’;
}

// Make the query.
$query = “SELECT model_id, model_url, model_name, bedrooms, baths, square_footage, model_pic FROM model_list ORDER BY $order_by LIMIT $start, $display”;
$result = mysql_query ($query); // Run the query.

// Table header.
echo'<table id=”model_table” cellspacing=”0″>
<tr>
<th scope=”col”><b></b></th>
<th scope=”col”><b><a href=”‘ . $link1 . ‘”>Model Name</a></b></th>
<th scope=”col”><b><a href=”‘ . $link2 . ‘”>Bedrooms</a></b></th>
<th scope=”col”><b><a href=”‘ . $link3 . ‘”>Baths</a></b></th>
<th scope=”col”><b><a href=”‘ . $link4 . ‘”>Square Feet</a></b></th>
<th scope=”col”><b></b></th>
</tr>
‘;

// Fetch and print all the records.
$bg = ‘#eeeeee’; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg==’#CDD2C8′ ? ‘#BBC2B4’ : ‘#CDD2C8’); // Switch the background color.
echo'<tr bgcolor=”‘ . $bg . ‘”>
<td>’ . $row[‘model_pic’] . ‘</td>
<td>’ . $row[‘model_name’] . ‘</td>
<td>’ . $row[‘bedrooms’] . ‘</td>
<td>’ . $row[‘baths’] . ‘</td>
<td>’ . $row[‘square_footage’] . ‘</td>
<td><a href=”http://www.reserve.com/‘ . $row[‘model_url’] . ‘”>View Model</a></td>
</tr>
‘;
}

echo'</table>’;

mysql_free_result ($result); // Free up the resources.

mysql_close(); // Close the database connection.

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo ‘<br /><p>’;
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it’s not the first page, make a Previous button.
if ($current_page != 1) {
echo ‘<a href=”ModelHomes.php?s=’ . ($start – $display) . ‘&np=’ . $num_pages . ‘&sort=’ . $sort . ‘”>Previous</a> ‘;
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo ‘<a href=”ModelHomes.php?s=’ . (($display * ($i – 1))) . ‘&np=’ . $num_pages . ‘&sort=’ . $sort . ‘”>’ . $i . ‘</a> ‘;
} else {
echo $i . ‘ ‘;
}
}

// If it’s not the last page, make a Next button.
if ($current_page != $num_pages) {
echo ‘<a href=”ModelHomes.php?s=’ . ($start + $display) . ‘&np=’ . $num_pages . ‘&sort=’ . $sort . ‘”>Next</a>’;
}

echo ‘</p>’;

} // End of links section.

?>

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@TyreeDec 22.2006 — First of all...please please please use php or code tags around your code. Makes it so much friendlier! ?

Here's what I'd do...I assume this part is the headings for your columns:
[code=php]
$arrowDn = '<img src="blah">';
$arrowUp = '<img src="blah">';

// Table header.
echo'<table id="model_table" cellspacing="0">
<tr>
<th scope="col"><b><a href="' . $link1 . '">Model Name</a></b>' .
(($order_by == "model_name ASC";)? $arrowUp : "" ) .
(($order_by == "model_name DESC";)? $arrowDn : "" ) . '</th>
</tr>';
[/code]


Just do that for each heading.
Copy linkTweet thisAlerts:
@kevinguillauthorDec 22.2006 — Thanks Tyree works great. although the column jumps now when column is clicked so can i hide an image in the same place so columns dont jump
Copy linkTweet thisAlerts:
@kevinguillauthorDec 22.2006 — [CODE]code[/CODE]
Copy linkTweet thisAlerts:
@TyreeDec 22.2006 — Yeah, just make some transparent gifs that get displayed in those places all the time.
[code=php]
$transGif = '<img src="blah">';
(($order_by == "model_name ASC";)? $arrowUp : $transGif )
[/code]
Copy linkTweet thisAlerts:
@kevinguillauthorDec 23.2006 — thanks again Tyree exactly what i was looking for. - kevin
Copy linkTweet thisAlerts:
@TyreeDec 23.2006 — You got it! No problem at all! ?
Copy linkTweet thisAlerts:
@kevinguillauthorDec 23.2006 — no problem at all i actually used this statement

[code=php]
<th scope="col"><b><a href="' . $link1 . '">Model</a></b>' .
(($order_by == "model_name ASC")? $arrowUp : (($order_by == "model_name DESC")? $arrowDn : $transGif )) . '</th>
[/code]
Copy linkTweet thisAlerts:
@TyreeDec 23.2006 — Yep yep...more concise...I like it!
×

Success!

Help @kevinguill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...