Current location: Hot Scripts Forums » Programming Languages » PHP » Gerating Link From mySQL dynamic data


Gerating Link From mySQL dynamic data

Reply
  #1 (permalink)  
Old 10-22-10, 09:19 PM
SkullShatteringProduction SkullShatteringProduction is offline
New Member
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Gerating Link From mySQL dynamic data

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.
Reply With Quote
  #2 (permalink)  
Old 10-22-10, 11:01 PM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
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 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";

Reply With Quote
The Following User Says Thank You to kfurlong For This Useful Post:
ultimatewarrior (10-25-10)
  #3 (permalink)  
Old 10-22-10, 11:05 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
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;
?>
__________________
Jerry Broughton
Reply With Quote
  #4 (permalink)  
Old 10-23-10, 08:56 PM
SkullShatteringProduction SkullShatteringProduction is offline
New Member
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy

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
Code:
<?php do { ?>
      <tr>
        <td>    <?php echo $row_TitleShort['Title']; ?></td>
        <td>    <?php echo $row_TitleShort['Short']; ?></td>
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!
Reply With Quote
  #5 (permalink)  
Old 10-24-10, 12:42 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
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.
__________________
Jerry Broughton
Reply With Quote
  #6 (permalink)  
Old 10-24-10, 09:40 AM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
get variables

you can do this a variety of different ways, the way I like to do it is through a "get" method.
PHP $_GET Function

It takes data from a url and you can turn it into a variable. the link works in this syntax:

page.php?variable1=value1&varaible2=value2

So you would want to send your id over you would have

page.php?id=$row['id']

in the link, be sure to escape the variabe from the string. so if your echo statement would look like this:
PHP Code:

echo"<a href='page.php?id=".$row['id']."'>$row['Title']</a>"
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.
Reply With Quote
  #7 (permalink)  
Old 10-24-10, 02:23 PM
SkullShatteringProductions SkullShatteringProductions is offline
New Member
 
Join Date: Oct 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Question Sorry

Sorry my account would not let me log into it or re validate it so i made another.

This is my View Page

PHP Code:

<?php

mb_http_input
("utf-8");
mb_http_output("utf-8");
?>
<?php 
require_once('Connections/Jobs.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$maxRows_TitleShort 10;
$pageNum_TitleShort 0;
if (isset(
$_GET['pageNum_TitleShort'])) {
  
$pageNum_TitleShort $_GET['pageNum_TitleShort'];
}
$startRow_TitleShort $pageNum_TitleShort $maxRows_TitleShort;

mysql_select_db($database_Jobs$Jobs);
$query_TitleShort "SELECT Title, Short FROM jobs ORDER BY `Date` ASC";
$query_limit_TitleShort sprintf("%s LIMIT %d, %d"$query_TitleShort$startRow_TitleShort$maxRows_TitleShort);
$TitleShort mysql_query($query_limit_TitleShort$Jobs) or die(mysql_error());
$row_TitleShort mysql_fetch_assoc($TitleShort);

if (isset(
$_GET['totalRows_TitleShort'])) {
  
$totalRows_TitleShort $_GET['totalRows_TitleShort'];
} else {
  
$all_TitleShort mysql_query($query_TitleShort);
  
$totalRows_TitleShort mysql_num_rows($all_TitleShort);
}
$totalPages_TitleShort ceil($totalRows_TitleShort/$maxRows_TitleShort)-1;
?>
<!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>View Jobs</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;
}

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

<body class="twoColElsLt">

<div id="container">
  <div id="sidebar1">
    <h3 align="center">Bolton High School Job Postings</h3>
    <p align="center"><a href="http://boltonpublicschools.com/bhs/">Bolton High Home</a></p>
    <p align="center"><a href="Index.php">Main Page</a></p>
    <p align="center"><a href="PostJob.php">Post A Job</a></p>
    <p align="center"><a href="mailto:boltontechproblems@gmail.com">Report a Technical Problem</a></p>
    <p align="center"><a href="mailto:boltonjobopportunities@gmail.com">Report an inaccuracy</a></p>
    <h3>&nbsp;</h3>
  <!-- end #sidebar1 --></div>
  <div id="mainContent">
  <p>
    <!-- end #mainContent -->
  </p>
  <p align="center">Search Jobs:
    <label>
    </label>
  </p>
  <form id="Keywords" name="Keywords" method="get" action="JobSearch.php">
    <label>
      <div align="center">
        <input type="text" name="Keywords" id="Keywords" />
        <label>
          <input type="submit" name="GO" id="GO" value="Search" />
        </label>
      </div>
    </label>
  </form>
  <table border="1">
    <tr>
      <td><div align="center">Title of Job</div></td>
      <td><div align="center">Short Description</div></td>
    </tr>
    <?php do { ?>
      <tr>
        <td>    <?php echo $row_TitleShort['Title']; ?></td>
        <td>    <?php echo $row_TitleShort['Short']; ?></td>
      </tr>
      <?php } while ($row_TitleShort mysql_fetch_assoc($TitleShort)); ?>
  </table>
</div>
    <br class="clearfloat" />
<!-- end #container --></div>
</body>
</html>
<?php
mysql_free_result
($TitleShort);
?>
and the display details page

PHP Code:

<?php require_once('Connections/Jobs.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$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>

<body class="oneColElsCtrHdr">

<div id="container">
  <div id="header">
    <h1 align="center">Details about selected Job</h1>
  <!-- end #header --></div>
  <div id="mainContent">
    <div align="center" class="oneColElsCtrHdr">
      <!-- end #mainContent -->
      <table width="500" border="1" cellpadding="1" cellspacing="1">
        <tr>
          <td><div align="center">Title</div></td>
          <td><div align="center">Description</div></td>
          <td><div align="center">Hours/Details</div></td>
          <td><div align="center">Contact Info</div></td>
        </tr>
        <?php do { ?>
          <tr>
            <td><?php echo $row_Recordset1['Title']; ?></td>
            <td><?php echo $row_Recordset1['Long']; ?></td>
            <td><?php echo $row_Recordset1['HoursDetails']; ?></td>
            <td><?php echo $row_Recordset1['ContactInfo']; ?></td>
          </tr>
          <?php } while ($row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>
      </table>
    </div>
  </div>
  <div id="footer">
    <p>Footer</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
<?php
mysql_free_result
($Recordset1);
?>
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
Reply With Quote
  #8 (permalink)  
Old 10-24-10, 09:46 PM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
Change this:
PHP Code:

$query_TitleShort "SELECT Title, Short FROM jobs ORDER BY `Date` ASC"
to :

PHP Code:

$query_TitleShort "SELECT ID,Title, Short FROM jobs ORDER BY `Date` ASC"
That adds the ID field to the query. the column/ field name is case sensitive, so make sure a.) You have a column ID and b.) it is all caps.

Then change this:

PHP Code:

<td>    <?php echo $row_TitleShort['Title']; ?></td>
to this:
PHP Code:

<td>    <?php echo "<a href='displayPage.php?ID=".$row_TitleShort['ID']."'>".$row_TitleShort['Title']."</a>"?></td>
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.
Reply With Quote
The Following User Says Thank You to kfurlong For This Useful Post:
  #9 (permalink)  
Old 10-24-10, 11:19 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Hello SkullShatteringProductions,

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.
__________________
Jerry Broughton

Last edited by job0107; 10-24-10 at 11:22 PM.
Reply With Quote
  #10 (permalink)  
Old 10-25-10, 07:05 AM
SkullShatteringProductions SkullShatteringProductions is offline
New Member
 
Join Date: Oct 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
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!
Reply With Quote
Reply

Bookmarks

Tags
link, mysql, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
data to mysql using .sql file and php acorna Script Requests 0 12-04-09 03:19 AM
Help constructing dynamic(3 dynamic parts) html page from mysql data method PHP 0 04-09-07 09:22 AM
How to recive mysql data using javascript ajax? method JavaScript 15 03-23-07 11:38 AM
dynamic display of mysql data?? daneastmond PHP 1 02-07-06 08:45 AM


All times are GMT -5. The time now is 08:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.