Jump to content

Switching background colors in tables


alexinjamestown

Recommended Posts

I have paginated this script so that it displays 10 records per page and that works just fine.  I have tried afterwards to mingle a switch background code, and I just can't get it to run with this script.  I don't know what the heck I am missing here.  Can somebody please help me with this?  What I'll show you first is the script that works with both page pagination and background switch, and then the script below that will be the script that is not working.  Thanks if anyone can help here...

 

 
----------->start first script<---------------
<?php # Script  - view_files.php
// This page displays the files uploaded to the server.

// Set the page title and include the HTML header.
$page_title = 'View Files';
include ('./includes/header.html');

require_once ('../mysql_connect.php'); // Connect to the database.
// Number of records to show per page:
$display = 5;

// 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 uploads ORDER BY date_entered 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.


$first = TRUE; // Initialize the variable.

if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

// Query the database.
$query = "SELECT upload_id, file_name, lastname, firstname, phone, citizenship, location, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM uploads ORDER BY date_entered ASC LIMIT $start, $display";
$result = mysql_query ($query);

// Display all the URLs.

// If this is the first record, create the table header.
if ($first) {

	echo '<table border="0" width="100%" cellspacing="4" cellpadding="2" align="center">
<tr>
	<td align="left" width="10%"><font size="+1">File Name</font></td>
	<td align="left" width="10%"><font size="+1">Last Name</font></td>
	<td align="left" width="10%"><font size="+1">First Name</font></td>
	<td align="left" width="10%"><font size="+1">Telephone</font></td>
	<td align="left" width="10%"><font size="+1">Citizenship</font></td>
	<td align="left" width="10%"><font size="+1">Register Date</font></td>
	<td align="left" width="10%"><font size="+1">Location</font></td>

</tr>';


	$first = FALSE; // One record has been returned.

} // End of $first IF.


// Display each record.

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align=\"left\"><a href=\"download_file.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td>
			<td align=\"left\">" . stripslashes($row['lastname']) . "</td>
	<td align=\"left\">" . stripslashes($row['firstname']) . "</td>
	<td align=\"left\">{$row['phone']}</td>
	<td align=\"left\">{$row['citizenship']}</td>
	<td align=\"left\">{$row['d']}</td>
			<td align=\"left\">{$row['location']}</td>

</tr>\n";


} // End of while loop.

// If no records were displayed...
if ($first) {
echo '<div align="center">There are currently no files to be viewed.</div>';
} else {
echo '</table>'; // Close the table.
}

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="view_files_colors_fix.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="view_files_colors_fix.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="view_files_colors_fix.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
}

echo '</p>';

} // End of links section.

include ('./includes/footer.html');

?>

-----------------------end first script (one that works)

------->start second script<--------------------------

<?php # Script 8.6 - view_users.php 
# (5th version after Scripts 7.4, 7.6, 8.2 & 8.5)

// This script retrieves all the records from the captains table.
// This new version allows the results to be sorted in different ways.

$page_title = 'View the Current Users';
include ('./includes/header.html');

// Page header.
echo '<p> </p><p> </p><p> </p>
<h1 id="mainhead"> </h1>
<h1>Registered Users</h1>';

require_once ('../mysql_connect.php'); // Connect to the db.

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

// 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 captains ORDER BY registration_date 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=lna";
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
$link3 = "{$_SERVER['PHP_SELF']}?sort=sxa";
$link4 = "{$_SERVER['PHP_SELF']}?sort=na";
$link5 = "{$_SERVER['PHP_SELF']}?sort=poa";
$link6 = "{$_SERVER['PHP_SELF']}?sort=dra";

// Determine the sorting order.
if (isset($_GET['sort'])) {

// Use existing sorting order.
switch ($_GET['sort']) {
	case 'lna':
		$order_by = 'last_name ASC';
		$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
		break;
	case 'lnd':
		$order_by = 'last_name DESC';
		$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
		break;
	case 'fna':
		$order_by = 'first_name ASC';
		$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
		break;
	case 'fnd':
		$order_by = 'first_name DESC';
		$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
		break;
	case 'sxa':
		$order_by = 'sex ASC';
		$link3 = "{$_SERVER['PHP_SELF']}?sort=sxd";
		break;
	case 'sxd':
		$order_by = 'sex DESC';
		$link3 = "{$_SERVER['PHP_SELF']}?sort=sxa";
		break;

	case 'na':
		$order_by = 'nationality ASC';
		$link4 = "{$_SERVER['PHP_SELF']}?sort=nd";
		break;
	case 'nd':
		$order_by = 'nationality DESC';
		$link4 = "{$_SERVER['PHP_SELF']}?sort=na";
		break;
	case 'poa':
		$order_by = 'position ASC';
		$link5 = "{$_SERVER['PHP_SELF']}?sort=pod";
		break;
	case 'pod':
		$order_by = 'position DESC';
		$link5 = "{$_SERVER['PHP_SELF']}?sort=poa";
		break;

	case 'dra':
		$order_by = 'registration_date ASC';
		$link6 = "{$_SERVER['PHP_SELF']}?sort=drd";
		break;
	case 'drd':
		$order_by = 'registration_date DESC';
		$link6 = "{$_SERVER['PHP_SELF']}?sort=dra";
		break;
	default:
		$order_by = 'registration_date DESC';
		break;
}

// $sort will be appended to the pagination links.
$sort = $_GET['sort'];

} else { // Use the default sorting order.
$order_by = 'registration_date DESC';
$sort = 'drd';
}

// Make the query.
$query = "SELECT last_name, first_name, sex, nationality, position, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM captains ORDER BY $order_by LIMIT $start, $display";		
$result = @mysql_query ($query); // Run the query.

// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<p> </p>
<p>
<td align="left"><b>Edit</b></td>
<td align="left"><b>View</b></td>
<td align="left"><b><a href="' . $link1 . '">Last Name</a></b></td>
<td align="left"><b><a href="' . $link2 . '">First Name</a></b></td>
<td align="left"><b><a href="' . $link3 . '">Gender</a></b></td>
<td align="left"><b><a href="' . $link4 . '">Nationality</a></b></td>
<td align="left"><b><a href="' . $link5 . '">Position</a></b></td>
<td align="left"><b><a href="' . $link6 . '">Date Registered</a></b></td>


</tr>';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#99CCFF' ? '#ffffff' : '#99CCFF'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
	<td align="left"><a href="view_yachtie.php?id=' . $row['user_id'] . '">View</a></td>
	<td align="left">' . $row['last_name'] . '</td>
	<td align="left">' . $row['first_name'] . '</td>
			<td align="left">' . $row['sex'] . '</td>
	<td align="left">' . $row['nationality'] . '</td>
	<td align="left">' . $row['position'] . '</td>
	<td align="left">' . $row['dr'] . '</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="view_users.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="view_users.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="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
}

echo '</p>';

} // End of links section.

include ('./includes/footer.html'); // Include the HTML footer.
?>
<html>
<body>
<style type="text/css">A:link {
COLOR: #000000; TEXT-DECORATION: underline
}
A:visited {
COLOR: #02119B; TEXT-DECORATION: underline
}
A:active {
COLOR: #0314AF; TEXT-DECORATION: none
}
A:hover {
COLOR: #D12916; TEXT-DECORATION: none
}
</style>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.