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."
 
Here it is:
Code:
<?php require_once('Connections/Pages.php');

mysql_select_db($database_Pages, $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
<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
?>

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
that cannot be the code you are using on the link you provided.

the code is very simple it says, in pseudo code:

1. if there is no PAGEID provided in the query string, do the displaylist function, otherwise do the displaycontent function,

2. the displaylist function, we know, is working.
3. the displaycontent function does not appear to be working.

however in an earlier post that you made, you had somehow copy and pasted the body of the display list function into the body of displaycontent. which would cause the results of the two functions to mimic each other.

please make absolutely sure that the content of the displaycontent function is only the following.

Code:
[red]
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;
}
[/red]
 
I found the problem. It wasn't your code. It was the way I set up the database field. I found it by trying to enter a second record to test and it cam up with a duplicate primary key error. That prompted me back to the database and I found that my primary key was not auto-incremented.

So, Huzzah! your code works a treat, I'm really impressed by it and I have learnt so much alreay.(I am actually understanding the code...scary) I've placed a star against you original post as useful. I did notice though that there were some quotes missing in the original.

But I'm only half way to my objective. Now I'd like to place this code into a meaningful page. Does this mean having echo the entire HTML code or can I place the function and section of the code into the place where I want it to display?

Also, I f I still want the menu to appear, I'd just make and adjustment to the if then statement?

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
the objective was just to build up your knowledge of how the functions worked. I'm glad we are there now!

in a 'meaningful page' you might do something like this

Code:
//lots of html doctype stuff
<html>
<head>
//some css
</head>
<body>
<div id="mainPageContent">
<?php include 'jpadieCode.php'; ?>
</div>
</body>
</html>

the code itself handles whether to display a list or a record.

and yes - if you want the list of clickable links and the selected record to appear you would change the conditional. either you would change it so that you also displayList() or you would take the displayList() out of the conditional altogether. either works fine.

good luck! post back if you need or start a new thread if the issue is 'new'
 
After much trail and error, I figured out how to integrate it into my page and I must say I am VERY impressed with this snippet of code.

For all of those search to do this sort of thing on there pages here is what I ended up with

The code the jpadie provided was broken down into three pieces.

1. I place the connection to the database and the name of the table at the top of the page
Code:
<?php require_once('Connections/Pages.php');
mysql_select_db($database_Pages, $Pages);
 $tableName = 'pagesTBL' ; // enter the name of your table
?>

2. I had a stack of CSS and HTML code that is the page design and in the menu DIV tag I placed this code:
Code:
  <?php

 displayList();
  
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
?>

3. Finally I place the content section of the code into the location where I wanted to content displayed
Code:
<?php
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));

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

Notice that I removed the If then statement from the original code as I wanted bot sets of code displayed all of the time. I can now go into the "echo" section of each code and place the id tags where I want to make the text fall into line with the rest of the page.

I'll take this post a place it into the FAQ section as I think it is invaluable for people building there own simple CMS sites.

Thanks again jpadie for your help. Could not have done this without you. The working page is here


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

consider reintegrating the if statement around the displaycontent() as without it you will get an error thrown each time the page runs and there is no query var set.

you might also want to take a look at the CRUD code that I posted both here on tektips and more recently on my website. it basically enhances the functionality of the code above.
you can find it here
 
OK, I'll have a look at the CRUD code. I would be interested in that, but need to get this site started at least for the people who are going to enter the content. Almost there.

I placed the If then statement back into the display content section of the main body. You are right (of course!).

I would also like the first page to display when someone opens the page for the first time. at the moment I just get a blank page with the menu on the left.

Am I right in saying that this should be done in the conditional? Here is what I tried (Didn't work)

Code:
<?php
 if (empty($_GET['PageID'])){
 [b][COLOR=red]$PageID = 0[/color][/b]
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
} else {
 displayContent(mysql_real_escape_string(trim($_GET['PageID'])));
}


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

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
by first page I guess you mean the pageid=0 from the database?

i would do this

Code:
$pageID = empty($_GET['pageID']) ? 0 : mysql_real_escape_string(trim($_GET['pageID']));

//display the list
displaylist();

//display the content
displayContent($pageID);

obviously surround it with whatever html code you want!

this syntax:
Code:
expression ? value if true : value if false;
is called the ternary expression. you can find info on it here
 
Hey, Thanks for that, another useful snippet!

-------------------------------------------------------------
"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 Can see how the code might work but I can't seem to get it to work. It actually stops the content from loading at all.

I tried adding the line above in each of the three sections that I described in my last post. Can you show me where to put it?. Use the code in my previous post

Cheers

-------------------------------------------------------------
"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 said:
3. Finally I place the content section of the code into the location where I wanted to content displayed
Code:
<?php
[red]
$pageID = empty($_GET['PageID']) ?  0 : mysql_real_escape_string(trim($_GET['PageID']));
 displayContent($pageID);
[/red]
function displayContent ($PageID){
 global $tableName;
 $result= mysql_query("Select TxtTitle, Content from $tableName where PageID='$PageID'") or die (mysql_error());
 $row = mysql_fetch_assoc($result);
 
echo <<<HTML
<h1>{$row['TxtTitle']}</h1>
<div id="content">
{$row['Content']}
</div>
HTML;
}
?>
 
Copied the code above and placed in the third area on the page, but it makes no difference. The first record still does not appear when I open the page.

Here is the page:

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
well that's because the pageID of the Home page is set to 1 and not 0. I took your lead on it being zero from your post above.

so if you want the default pageID to be 1 you change the zero in the following code snip

Code:
$pageID = empty($_GET['PageID']) ?  0 : mysql_real_escape_string(trim($_GET['PageID']));
 displayContent($pageID);
 
That's why you get paid the big bucks! It was a zero before. It must have sorted itself out when I fixed up the database problem.

Thanks, your MVP status is well deserved

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
big bucks? i wish... i'm only an impoverished lawyer! i meddle with php for the fun of it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top