Clickable Link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cogdev
Forum Newbie
Posts: 4
Joined: Thu Aug 19, 2004 2:29 pm

Clickable Link

Post by cogdev »

I am displaying the values in a database and I want to make one of the field clickable. For example, if I have 1995 displayed, I want the users to click on it and it reurrns all the data for 1995.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll need a script that can handle taking some information from the url via the $_GET superglobal that'd tell it which field to filter and requery for.. the following thread has a lot of these details:
viewtopic.php?t=25082&highlight=%2Aquery
cogdev
Forum Newbie
Posts: 4
Joined: Thu Aug 19, 2004 2:29 pm

Post by cogdev »

Hi,

Thanks for the link. I am very new to this, can you tell me where to make the changes?


Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>



</HEAD>

<BODY>



<?php

if ($selCrsOrDept == "G" and $selGender == "S")
{
echo "Please selecet a select gender";
}
else {
$db = mysql_connect("server","test","pass")
						or die("001 - Connection Failed");
					mysql_select_db("tetdb", $db);
				 
// start table

echo '<center><table border="1">';
echo '<tr>';
echo '<center><strong>Gender:</strong> ' . $selGender . '<br></center>';
echo '<center><strong>College:</strong> ' . $filterCollege . '  <br></center>';


echo '<tr>';
echo '<center><table border="1">';
echo '<tr>';
echo '</tr>';
echo '<tr>';
echo '<th>Gender</th>';
echo '<th>Major</th>';
echo '<th>Head_Count</th>';
echo '<th>Career</th>';
echo '<th>Faculty</th>';
echo '<th>College</th>';
echo '<th>Attendance</th>';
echo '<th>System Of Study</th>';
echo '<th>Term</th>';
echo '</tr>';

// query database
$result = mysql_query("select * from tbl_regist_cube_term_heads where gender = '$selGender' and college= '$filterCollege'group by major , career", $db);

// loop through records, printing new row for each
while ($myrow = mysql_fetch_array($result)) {
    printf("<tr><td>%s</td>\n",$myrow['gender']);
    printf("<td>%s</td>\n",$myrow['major']);
	printf("<td>%s</td>\n",$myrow['head_count']);
	printf("<td>%s</td>\n",$myrow['career']);
	printf("<td>%s</td>\n",$myrow['faculty']);
	printf("<td>%s</td>\n",$myrow['college']);
	printf("<td>%s</td>\n",$myrow['attendance']);
	printf("<td>%s</td>\n",$myrow['system_of_study']);
	

	printf("<td>%s</td></tr>\n",$myrow['univ_term']);

// close table
}
echo '</table></center>';

}
?>

</Body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$result = mysql_query("select * from tbl_regist_cube_term_heads where gender = '$selGender' and college= '$filterCollege'group by major , career", $db);
mostly, you need to adjust this to tack on an 'ORDER BY' directive. Make sure the columns you ask for are accepted for the page, if they aren't default to their id, or something..
cogdev
Forum Newbie
Posts: 4
Joined: Thu Aug 19, 2004 2:29 pm

Post by cogdev »

I got this sample taken from the web to work, but how to a make the actual data clickable?


This has clickable columns...

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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');

/* 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 ('host,'user,'pass');
mysql_select_db ('db');

/* 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="{$_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>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

# do u  have some errors ??
# change the following line
#echo "<a href="{$_SERVER['PHP_SELF']}?order=$heading">$heading</a>"; 
#into
echo "<a href="?order=$heading">$heading</a>";
cogdev
Forum Newbie
Posts: 4
Joined: Thu Aug 19, 2004 2:29 pm

Post by cogdev »

No I do not get any error.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

whats with the heading part.
cuase you could simply doing something like this.

Code: Select all

while ($row = mysql_fetch_assoc ($result)) { 
// rest of code
echo "<a href="?order=$row['heading']">$row['heading']</a>";
Post Reply