Jump to content

Creating A Link From Search Results


RawDog
 Share

Recommended Posts

Does this snippet help?

// Make the query.$query = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users 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>	<td align="left"><b>Edit</b></td>	<td align="left"><b>Delete</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 . '">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=='#eeeeee' ? '#ffffff' : '#eeeeee'); // 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="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>		<td align="left">' . $row['last_name'] . '</td>		<td align="left">' . $row['first_name'] . '</td>		<td align="left">' . $row['dr'] . '</td>	</tr>	';}echo '</table>';

Follow it through and you will see that the delete and edit links are created from the query results.

Link to comment
Share on other sites

The 'while' statement will perform the subsequent piece of code for every entry that matches the query criteria, so there is no need to rewrite it. A simpler example:If I have 1billion entries in a table with first and last names, I can display all of them like this:

//fetch the records$sql = "SELECT first_name, last_name FROM names";$res = mysqli_query($connect_info, $sql) or die(mysqli_error($connect_info));//cycle through themwhile($names = mysqli_fetch_array($res)) {//the following will happen for each row in the table$first_name = $names["first_name"];$last_name = $names["last_name"];echo "$first_name, $last_name";}

If you wanted to turn that into a link, you just echo differently. If you had a page called personnel that fetched the names from the URL and used that for something, you might change the echo statement to:echo "<a href='personnel.php?first_name=$first_name&last_name=$last_name'>$first_name, $last_name</a>";The while loop is your friend.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...