Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clickable link

Status
Not open for further replies.

cogdev

MIS
Nov 30, 2001
85
US
I am displaying data based on an initial user selection screen.
The dats gets displayed in a table...one of the fields is a year field, ex. 1995 1996 etc.
Is there a way to make the dates into a hyperlink, so that the user can further refine the data displayed. That is, when they click on a year, the data returned is filtered by that date.


Thanks
 
Yes.

One way is to do it with the GET variables. GET variables are passed to a page through the hyperlink.

You could potentially echo this in the original table:

[tt]
echo '<td><a href="page.php?key=year&val=' . $myrow['year'] . ">' . $myrow['year'] . '</a></td>';
[/tt]

Where key = the name of your table field and val = the value you want to sort by.

Then, in the page that opens (page.php in my example), when you build your SQL query, you take the passed values as:

$_GET['key']
$_GET['val']

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Can somebody show me how to make the actual data clickable, as opposed to the column headings?
I would like to click on a datavalue, and return the filtered result....please....this code I found on the web.



Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
/* set the allowed order by columns */
$default_sort = 'major';
$allowed_order = array ('faculty', 'college','attendance','gender');

/* if order is not set, or it is not in the allowed
*list, then set it to a default value. Otherwise, 
* set it to what was passed in. */
if (!isset ($_GET['order']) || 
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}

/* connect to db */
mysql_connect (db','usr','zy');
mysql_select_db ('test');

/* construct and run our query */
$query = "SELECT * FROM tbl_regist_cube_term_heads ORDER BY $order";
$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
    /* check if the heading is in our allowed_order
     * array. If it is, hyperlink it so that we can
     * order by this column */
    echo "<TD><b>";
    if (in_array ($heading, $allowed_order)) {
       
	   //echo "<a href=\"?order=$heading\">$heading</a>";
	   
	    echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
    } else {
        echo $heading;
    }                
    echo "</b></TD>\n";
}
echo "</TR>\n";

/* reset the $result set back to the first row and 
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $column) {
   
		
		echo "<TD>$column</TD>\n";
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";
?>
</body>
</html>
 
See what's happening in this line?

echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";


Do something similar here:

echo "<TD>$column</TD>\n";


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Any ideas on how to make the clicked value the filter. Example, I click on 1999, and the query gets sent with select * from...where year ='1999'?

Thanks for your help.
 
[red]UNDER NO CIRCUMSTANCES SHOULD YOU BE PASSING ENTIRE QUERIES BACK AND FORTH BETWEEN A BROWSER AND THE SERVER.[/red]

It is insecure in the extreme. All a user has to do is look at the URL or the source of an HTML page to figure out how to pass a query like "DELETE FROM tablename" to your script.

Your script will need a little information to build or select the correct query to send to the database server. Send no more than that little bit of information. It should be nothing that can give a hostile entity the means to figure out how your database works.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top