Jump to content

Buttons in a while loop


msa025

Recommended Posts

I am currently trying to figure out a way to use button links instead of text links on several of my pages because of problems with the font color (and I think buttons are nifty). Anyway, I am essentially reading every user off of the database and reprinting it with a links to either edit or delete the user. I use the individual userID so that the edit_user.php and delete_user.php file know which user specifically to edit or delete.

 

Anyway, I have tried different methods to use buttons, but none have worked. The closest I came was using hidden values, and passing it like you would a form...but it only keeps the last user information on the page. If anyone has any other ideas, I would greatly appreciate hearing them.

 

<?php
  require_once('mysql_connect.php');
  if((!isset($_SESSION['first_name'])) || (($_SESSION['user_type'] != 7) && ($_SESSION['user_type'] != 14) && ($_SESSION['user_type'] != 12))) {
	$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
	if((substr($url,-1) == '/') OR (substr($url,-1) == '\\')) {
		$url = substr($url,0,-1);
	}
	$url .= '/index.php';

	ob_end_clean();
	header("Location: $url");
	exit();
}
  $display=20;
  
  	if(isset($_GET['np'])) {
  		$num_pages = $_GET['np'];
} else {
	$query = "SELECT COUNT(*) FROM user ORDER BY lastName ASC";
	$results = mysql_query($query);
	$row = mysql_fetch_array($results, MYSQL_NUM);
	$num_records = $row[0];

	if ($num_records > $display) {
		$num_pages = ceil($num_records/$display);
	} else {
		$num_pages = 1;
	}
}

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

$link0 = "{$_SERVER['PHP_SELF']}?sort=una";
$link1 = "{$_SERVER['PHP_SELF']}?sort=fna";
$link2 = "{$_SERVER['PHP_SELF']}?sort=lna";
$link3 = "{$_SERVER['PHP_SELF']}?sort=ena";
$link4 = "{$_SERVER['PHP_SELF']}?sort=pna";
$link5 = "{$_SERVER['PHP_SELF']}?sort=tna";

if(isset($_GET['sort'])) {
	switch($_GET['sort']) {
		case 'lna':
			$order_by = 'lastName ASC';
			$link2 = "{$_SERVER['PHP_SELF']}?sort=lnd";
			break;
		case 'lnd':
			$order_by = 'lastName DESC';
			$link2 = "{$_SERVER['PHP_SELF']}?sort=lna";
			break;
		case 'fna':
			$order_by = 'firstName ASC';
			$link1 = "{$_SERVER['PHP_SELF']}?sort=fnd";
			break;
		case 'fnd':
			$order_by = 'firstName DESC';
			$link1 = "{$_SERVER['PHP_SELF']}?sort=fna";
			break;
		case 'una':
			$order_by = 'userID ASC';
			$link0 = "{$_SERVER['PHP_SELF']}?sort=und";
			break;
		case 'und':
			$order_by = 'userID DESC';
			$link0 = "{$_SERVER['PHP_SELF']}?sort=una";
			break;
		case 'ena':
			$order_by = 'email ASC';
			$link3 = "{$_SERVER['PHP_SELF']}?sort=end";
			break;
		case 'end':
			$order_by = 'email DESC';
			$link3 = "{$_SERVER['PHP_SELF']}?sort=ena";
			break;
		case 'tna':
			$order_by = 'types.typeName ASC';
			$link5 = "{$_SERVER['PHP_SELF']}?sort=tnd";
			break;
		case 'tnd':
			$order_by = 'types.typeName DESC';
			$link5 = "{$_SERVER['PHP_SELF']}?sort=tna";
			break;
		case 'pna':
			$order_by = 'phone ASC';
			$link4 = "{$_SERVER['PHP_SELF']}?sort=pnd";
			break;
		case 'pnd':
			$order_by = 'phone DESC';
			$link4 = "{$_SERVER['PHP_SELF']}?sort=pna";
			break;
		default:
			$order_by = 'lastName ASC';
			break;
	}

	$sort = $_GET['sort'];
} else {
	$order_by = 'lastName ASC';
	$sort = 'rdd';
}

$query = "SELECT userID, firstName, lastName, email, phone, types.typeName FROM user LEFT JOIN types ON user.userType=types.typeID ORDER BY $order_by LIMIT $start, $display";
$result = mysql_query($query);

echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
	<td align="center"><b>Edit</b></td>
	<td align="center"><b>Delete</b></td>
	<td align="center"><b><a href="' . $link0 .'">User Name</a></b></td>
	<td align="center"><b><a href="' . $link1 .'">First Name</a></b></td>
	<td align="center"><b><a href="' . $link2 .'">Last Name</a></b></td>
	<td align="center"><b><a href="' . $link3 .'">Email</a></b></td>
	<td align="center"><b><a href="' . $link4 .'">Phone Number</a></b></td>
	<td align="center"><b><a href="' . $link5 .'">Position</a></b></td>
</tr>
';

$bg = '#eeeeee';
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$bg =($bg=='#eeeeee' ?'#ffffff' : '#eeeeee');
	echo '<tr bgcolor="' . $bg . '">
		<td align="left"><a href="edit_user.php?id=' . $row['0']. '">Edit</a></td>
		<td align="left"><a href="delete_user.php?id=' . $row['0'] . '">Delete</a></td>
		<td align="left">' . $row['0'] . '</td>
		<td align="left">' . $row['1'] . '</td>
		<td align="left">' . $row['2'] . '</td>
		<td align="left">' . $row['3'] . '</td>
		<td align="left">' . $row['4'] . '</td>
		<td align="left">' . $row['5'] . '</td>
	</tr>
	';
}
echo '</table>';

mysql_free_result($result);
mysql_close();

if($num_pages > 1) {
	echo '<br /><p align="center">';
	$current_page = ($start/$display)+1;
	if($current_page != 1) {
		echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort . '">Previous</a> ';
	}

	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> ';
		}
	}

	if($current_page != $num_pages) {
		echo '<a href="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort . '">Next</a>';
	}

	echo '</p>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/94187-buttons-in-a-while-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.