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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Detail page 2

Status
Not open for further replies.

jedel

Programmer
Jan 11, 2003
430
AU
Hey all,

I am trying PHP on a project instead of ASP and I'm wondering what is the equivalent of navigating to a detail page in PHP?

I have a MySQL db with four fields, PageID, Link, Title and content.

The link field is the text for the navigation menu and when I click on this link, I want top go to that relevant recordset.

ASP has a Go to Detail page or related page code. What is the code for PHP?

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Code:
//connect to your database here

$tableName = '' ; // enter the name of your table
if (empty($_GET['PageID'])){
 displayList();
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}

function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select Title, Content from tableName where PageID='$PageID') or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 echo <<<HTML
<h1>{$row['Title']}</h1>
<div id="content">
{$row['content']}
</div>
HTML;
}

function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">$row['Link']</a>
</div>
HTML;
 } //end while
} //end function
 
Seems like an awful lot of code for a simple hyperlink.
Maybe mt explaination was not clear enough, sorry.

1. I have a connection to the database in page page already

Code:
<?php require_once('../Connections/Pages.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

mysql_select_db($database_Pages, $Pages);
$query_PgLink = "SELECT * FROM pagesTBL ORDER BY LinkOrder ASC";
$PgLink = mysql_query($query_PgLink, $Pages) or die(mysql_error());
$row_PgLink = mysql_fetch_assoc($PgLink);
$totalRows_PgLink = mysql_num_rows($PgLink);
?>

2. i have a list of data from the database connection displaying the "Link" field already

Code:
<div id="sidebar1">
        <!-- end #sidebar1 -->
        <table width="147" border="0" cellspacing="0" cellpadding="0">
          <?php do { ?>
          <tr>
            <td class="SideBarLink"><?php echo $row_PgLink['Link']; ?></td>
          </tr>
            <?php } while ($row_PgLink = mysql_fetch_assoc($PgLink)); ?>
        </table>
  </div>

What I need is a method of linking to another page to display the recordset of the row I clicked on in the first page.

Sorry if i'm not making myself clear here, did I mention that I'm trying PHP seriously for the first time?



-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
A bit more for you. THe second page has a recordset connection where the record being displayed is equal to a session variable. If I can get this session variable to equal the page ID when I click the link, it should display the correct recordset on the second page. At the moment Im going to a redirect page which gathers a URL Parameter, being the pageID field and then turns that into a session variable and redirects to the detail page.

I hope this explains how I'm traveling with the idea a little better.

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
it's not just a hyperlink. did you try it?

the code i provided lists your records for you in a clickable manner. if you click on a link then you will be taken to what you call a 'detail page'.

the basic premise is simply to pass an ID through the query string, pick it up in the $_GET superglobal and use it as you wish. If you want it to become a session variable then simply assign it to the relevant session key.
 
Definitely not JUST a hyper link.

I did try to assign a sessiomn variable to the PageID, but it didn't work.

Did I mention that I am new to PHP by less that a week?

I'll try to implement your code into my pages but I'm a little unclear on where the detail page comes into the picture. Where is the link to the second Page? or do you have it all coded onto the one page?

Looking at my code, how does your code fit into mine?

Did I mention that I'm really new to PHP? The code above is generated from Dreamweaver.



-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
i haven't really looked at your code and i'm not at all a fan of dreamweaver generated code.

my code will do 'everything' for you: i.e. it will give you a list of records that are clickable links and a single detail record when a link is clicked on. the output is managed by the two functions: displayList and displayContent
 
OK,

I'll give it a go. Is there anywhere specifically where the code needs to be placed?


-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
jedel,

keep it simple for the time being. just put the code in a file on its own, and replace the bits in red with real values. you can work from there

Code:
[red]//connect to your database here[/red]

$tableName = [red]'' ; // enter the name of your table[/red]
if (empty($_GET['PageID'])){
 displayList();
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}

function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select Title, Content from $tableName where PageID='$PageID') or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 echo <<<HTML
<h1>{$row['Title']}</h1>
<div id="content">
{$row['content']}
</div>
HTML;
}

function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from $tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">$row['Link']</a>
</div>
HTML;
 } //end while
} //end function
 
OK,

Placed all of the code above into the body tag of a separate page. here is the link


I get the following error:


Parse error: syntax error, unexpected T_STRING in /home/med49751/public_html/queenslandrangers.org.au/test.php on line 59

I thought it may have been something to do with the CSS id 'content' in this line but nothing changed when I linked the css or made an id property.

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
old boy, for the time being can we dispense with anything else in the file except as shown. no html or anything. i can't tell what line 59 is for you as my code has only 20 odd lines in it.
 
although ... please edit this code block as shown

Code:
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">[red]{[/red]$row['Link'][red]}[/red]</a>
</div>
HTML;
 
OK, but don't I need to connect to the database?

Code:
//connect to your database here

Removed all code except yours,placed your new code above in the spot same error but in line 13

Here's the code:
Code:
<?php $tableName = 'pagesTBL' ; // enter the name of your table
if (empty($_GET['PageID'])){
 displayList();
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}

function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select Title, Content from $tableName where PageID='$PageID') or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">{$row['Link']}</a>
</div>
HTML;
}

function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from $tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">$row['Link']</a>
</div>
HTML;
 } //end while
} //end function
?>

Parse error: syntax error, unexpected T_STRING in /home/med49751/public_html/queenslandrangers.org.au/test.php on line 13

Hey, thanks for your patience in this too.


-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
yes - please include the db connection params at the top.

and put curly braces around $row['Link'] in the heredoc within the second function

Code:
function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from $tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">[red]$row['Link'][/red]</a>
</div>
HTML;
 } //end while
} //end function
 
Here's the code:
Code:
<link href="CSS/FontEnd.css" rel="stylesheet" type="text/css">
<?php require_once('Connections/Pages.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

mysql_select_db($database_Pages, $Pages);
$query_pages = "SELECT * FROM pagesTBL";
$pages = mysql_query($query_pages, $Pages) or die(mysql_error());
$row_pages = mysql_fetch_assoc($pages);
$totalRows_pages = mysql_num_rows($pages);
 $tableName = 'pagesTBL' ; // enter the name of your table
if (empty($_GET['PageID'])){
 displayList();
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}

function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select Title, Content from $tableName where PageID='$PageID') or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">{$row['Link']}</a>
</div>
HTML;
}

function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from $tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
[b][COLOR=red]<div class="contentLink">[/color][/b]
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">$row['Link']</a>
</div>
HTML;
 } //end while
} //end function

mysql_free_result($pages);
?>

Same error but in line 51

Parse error: syntax error, unexpected T_STRING in /home/med49751/public_html/queenslandrangers.org.au/test.php on line 51 (highlighted above)

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
I did put the curly brackets in too

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Code:
[s]<link href="CSS/FontEnd.css" rel="stylesheet" type="text/css">[/s]
<?php require_once('Connections/Pages.php'); [s]?>

<?php[/s]
[s]
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}
[/s]
mysql_select_db($database_Pages, $Pages);
[s]
$query_pages = "SELECT * FROM pagesTBL";
$pages = mysql_query($query_pages, $Pages) or die(mysql_error());
$row_pages = mysql_fetch_assoc($pages);
$totalRows_pages = mysql_num_rows($pages);
[/s]
 $tableName = 'pagesTBL' ; // enter the name of your table
if (empty($_GET['PageID'])){
 displayList();
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}

function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select Title, Content from $tableName where PageID='$PageID') or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 [s]echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">{$row['Link']}</a>
</div>[/s]
[red] echo <<<HTML
<h1>{$row['Title']}</h1>
<div id="content">
{$row['content']}
</div>
HTML;
[/red]
HTML;
}

function displayList(){
 global $tableName;
 $result = mysql_query('Select PageID, Link from $tableName") or die (mysql_error());
 while ($row = mysql_fetch_assoc($result)){
  echo <<<HTML
<div class="contentLink">
<a href="{$_SERVER['PHP_SELF']}?PageID={$row['PageID']}">[red]{[/red]$row['Link'][red]}[/red]</a>
</div>
HTML;
 } //end while
} //end function
[s]
mysql_free_result($pages);
[/s]
?>
 
oops

Code:
 echo <<<HTML
<h1>{$row['Title']}</h1>
<div id="content">
{$row['Content']}
</div>
HTML;
 
The page is displaying the menu (Huzzah!)

But when I click on the link, it takes me to the pageID record in the address bar, but nothing displays. Should be a line of text in the Content field.

Here is the page again


-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
could you post back the actual code you are using?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top