Im very new to PHP and mySQL programming languages so im sorry if this is something easy to do and i am simply to much of a novice to see it. I have a row of data im pulling from 1 column of a mySQL database and i want users to be able to click the text that is displayed and send them to a page that would pull more information from the database relevant to the text they clicked on. So i would have;
<?php echo $row_TitleShort['Title']; ?>
and that would display all the information in the title column. How could i make each piece of data from each row in that column redirect the user to a new page that had more details on the clicked text. So if the user clicked on one entry Design, it would bring him to a page with other details about that particular subject but if a user were to click on another one called Mechanic, it would bring them to a page with additional details about mechanics without writing a separate PHP page for both.
//Run the query which selects the column link from table table, if there is an error, kill the program and display it.
$query = mysql_query("SELECT Title,id FROM table") or die("Error: ".mysql_error()."!");
//Check if any results were returned
if(mysql_num_rows($query)>0){
//If so, cycle through the results
while($link = mysql_fetch_array($query)){
//Display the link
echo"<a href=displayPage.php?id=$link[id]>$link[Title]</a><br>";
//End the while
}
//If it no results were found...
}else{
//Tell them
echo"No links found";
}
Here is a quick example, of the summerized page... As for the display page
PHP Code:
//Run the query which selects the column link from table table, if there is an error, kill the program and display it.
$query = mysql_query("SELECT * FROM table WHERE id='".$_GET['id']."'") or die("Error: ".mysql_error()."!");
//Check if any results were returned
if(mysql_num_rows($query)>0){
//If so, display itresults
$row = mysql_fetch_array($query);
//Display the info
echo"$row[column1]<br>
$row[column2]<br>
$row[column3]<br>
$row[column4]<br>
$row[column5]<br>...etc";
//If it no results were found...
}else{
//Tell them
echo"No titles found";
}
The Following User Says Thank You to kfurlong For This Useful Post:
This is a very simple example.
In the first query that loads $output with values, use your own column names in the $row[] variables.
And add as many as necessary.
PHP Code:
<?php // Enter your database info here. $host = "localhost"; // mysql server address $user = "*****"; // mysql user name $password = "*****"; // mysql password $db = "*****"; // Database name $table = "*****"; // Table name
// Make a connection to the server and select a database. mysql_connect($host,$user,$password) or die(mysql_error()); mysql_select_db($db) or dir(mysql_error());
// Query the database when a form is submitted and load the $output variable with the results. if(!empty($_POST["submit"])) { $output = ""; $results = mysql_query("SELECT * FROM $table WHERE Title = '".$_POST["title"]."'"); while($row = mysql_fetch_assoc($results)) { // Load your table elements here. // These are only examples. Use your own. if(!empty($row["name"])) { $output .= $row["name"]."<br />"; $output .= $row["address"]."<br />"; $output .= $row["city"]."<br />"; $output .= $row["state"]."<br />"; } } if($output){$output = "<div style='float:left;margin-left:50px;border:1px solid #000;padding:5px;'>".$output."</div>";} }
// Query the database to create your list and output the list & the results ($output). $results = mysql_query("SELECT Title FROM $table"); echo "<table border=1 cellpadding=5 rules='rows' style='float:left;'>"; while($row_TitleShort = mysql_fetch_assoc($results)) { echo "<tr><td><form action='#' method='POST' style='margin:0px;'><input type='hidden' name='title' value='".$row_TitleShort['Title']."'>". $row_TitleShort['Title'].":</td><td><input type='submit' name='submit' value='GO'></form></td></tr>"; } echo "</table>".$output; ?>
Sorry i tried using both your ways but it still gave me errors. As of now it displays the 10 most current entries in the database. But i still can not figure out how to append the title or id of the entry to the end of the URL so that my JobDetails page can insert the correct information. AS of now i have
I can hyperlink them to the page but after that I am not sure how to add eather the ID value or Title value to the end of the URL so that it would be different for every one. Any idea on how to do this? it is the only part of the website that is not working correctly and i want to be able to hand it in soon! Many thanks to whoever can help me!
Show us the page that displays the 10 most current entries in the database.
Also show us the JobDetails page and tell us the name of the JobDetails page.
This would pass the variable id through the get variables built into php.
to call the variable you need to add this to "page.php":
PHP Code:
$id = $HTTP_GET_VARS['id'];
This tells php to look in the url for the parameter id and store whatever it is equal to, to this variable ($id). $HTTP_GET_VARS can be shortened to $_GET, but if one way doesnt work, try the other.
If both dont work, then please post all of your code, and provide the error messages you are recieving, or a description of what it is doing wrong.
<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
background: #666666;
margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
padding: 0;
text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
color: #000000;
}
.twoColElsLt #container {
width: 46em; /* this width will create a container that will fit in an 800px browser window if text is left at browser default font sizes */
background: #FFFFFF;
margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
border: 1px solid #000000;
text-align: left; /* this overrides the text-align: center on the body element. */
}
.twoColElsLt #sidebar1 {
float: left;
width: 12em; /* since this element is floated, a width must be given */
background: #EBEBEB; /* the background color will be displayed for the length of the content in the column, but no further */
padding: 15px 0; /* top and bottom padding create visual space within this div */
}
.twoColElsLt #sidebar1 h3, .twoColElsLt #sidebar1 p {
margin-left: 10px; /* the left and right margin should be given to every element that will be placed in the side columns */
margin-right: 10px;
}
.twoColElsLt #mainContent {
margin: 0 1.5em 0 13em; /* the right margin can be given in ems or pixels. It creates the space down the right side of the page. */
}
/* Miscellaneous classes for reuse */
.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
float: right;
margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page */
float: left;
margin-right: 8px;
}
.clearfloat { /* this class should be placed on a div or break element and should be the final element before the close of a container that should fully contain a float */
clear:both;
height:0;
font-size: 1px;
line-height: 0px;
}
.twoColElsLt #container #mainContent h1 table tr td {
font-size: 18px;
}
.SmallerText {
font-size: 8px;
}
-->
</style><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.twoColElsLt #sidebar1 { padding-top: 30px; }
.twoColElsLt #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]--></head>
$colname_Recordset1 = "-1";
if (isset($_GET['ID'])) {
$colname_Recordset1 = $_GET['ID'];
}
mysql_select_db($database_Jobs, $Jobs);
$query_Recordset1 = sprintf("SELECT Title, `Long`, HoursDetails, ContactInfo FROM jobs WHERE ID = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $Jobs) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Job Details</title>
<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
background: #666666;
margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
padding: 0;
text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
color: #000000;
}
.oneColElsCtrHdr #container {
width: 46em; /* this width will create a container that will fit in an 800px browser window if text is left at browser default font sizes */
background: #FFFFFF;
margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
border: 1px solid #000000;
text-align: left; /* this overrides the text-align: center on the body element. */
}
.oneColElsCtrHdr #header {
background: #DDDDDD;
padding: 0 10px 0 20px; /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
}
.oneColElsCtrHdr #header h1 {
margin: 0; /* zeroing the margin of the last element in the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
padding: 10px 0; /* using padding instead of margin will allow you to keep the element away from the edges of the div */
}
.oneColElsCtrHdr #mainContent {
padding: 0 20px; /* remember that padding is the space inside the div box and margin is the space outside the div box */
background: #FFFFFF;
}
.oneColElsCtrHdr #footer {
padding: 0 10px; /* this padding matches the left alignment of the elements in the divs that appear above it. */
background:#DDDDDD;
}
.oneColElsCtrHdr #footer p {
margin: 0; /* zeroing the margins of the first element in the footer will avoid the possibility of margin collapse - a space between divs */
padding: 10px 0; /* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
}
-->
</style></head>
I know i need to use the GET function do add the ID number to the end of the URl so that my display details page can pull the correct information from the database but i am unsure how to assign it correctly with the dynamic data so that each link displays the correct Id for the job the users selects
where 'displayPage.php' is the name of your display page. This adds the link to the title with the id of that row in the url so that the GET property can retrieve it later.
Let me know if that works, or if somehting is wrong with it.
The Following User Says Thank You to kfurlong For This Useful Post:
The solution that kfurlong proposes would require that you have an ID column in your database table that contains a unique value for each record.
And if you don't have an ID column then kfurlong's solution won't work.
But kfurlong's solution is the best solution, because the columns Title and Short may not always be unique values for each record.
But there is a solution to the problem if you don't have an ID column in your table.
You can easily add an ID column to your table an set it to auto_increment, that is providing you don't already have an auto_increment field.
Once you add the ID column to the table and set it to auto_increment, it will automatically assign a value for each record.
There is still the possibility that other parts of the program may be effected by the addition of a new column, but that will be determined on how any INSERT or UPDATE queries are written.
If you already have an auto_increment field or any field that has a unique value for each record in your table then use that one instead of id.
Thank you very much kfurlong! your change worked! Now that i see what i had to do and how simple it was i feel quite silly but I am just glad it worked! thank you very much!