Dynamic PHP, MySQL paging

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
User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

Have had a long browse through the net for this but whatever I type I always get the wrong tutorials.
Basically I am looking for a half decent tutorial that show me the rough outlines in which I would need to have dynamic pages ie"index.php?page=animals" and that page pull information from a database.

Sorry for the real nooby questions :lol:
Last edited by CONFUSIONUK on Sat Jun 05, 2010 5:58 am, edited 1 time in total.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

The key here is reading the URL query string ($_GET array) and setting a series of conditional statements.

Code: Select all

<html>
<body>
<?php if( array_key_exists( 'page', $_GET) && ($_GET['page'] = 'animals')) {
    include'includes/animals.php';  //
} else if ...

User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Re: Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

hmmm, I forgot to post the script I am using already

Code: Select all

<?php

$sExpressie = "(http:|ftp:|shttp:|www.|.php|.pl|.cgi|.asp|index.php)";

if(isset($_GET['p'])){
if(preg_match($sExpressie,$_GET['p'])){
header('Location: ?p=error');
}else{
if(file_exists($_GET['p'].'.php')){
include $_GET['p'].'.php';
}else{
header('Location: ?p=error');
}
}
}else{
header('Location: ?p=home');
}
					
?>
This does all the paging and error handling, its just when it comes to grabing data from the database such as "index.php?p=animal&id=1"

Any ideas or have I lost you? :drunk:
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

The way I read it, on line 10 it calls the query line page from includes. I would go to the include page(s) and preface the page with a SELECT query. Or, is your quest how do you write a SELECT query?
User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Re: Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

I'm really bad at asking questions and being clear sorry, I know how to select and echo tables from the database but like when you see the post i want it to have "read more" at the end of the post when it has been shortend and then clicking the link go to the full post ie "?p=animals&id=1" id being the row in the table....
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

Hmmm, I hate being dense. The way the referred link works is an HTML page is created with limited content. After the content is the ...<a href='page.php?animal=tiger&id=27'>Read More</a> which when clicked loads a second page which reads the $_GET array and retrieves the requested content.
User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Re: Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

That sounds exactly like what I need would you be able to point me to a tut or a few lines to get me started :roll:

This is the code and source that is working so far, just the "?p=home&id=1" bit to do
Image
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

Your main page needs to be something like this.

Code: Select all

<?php
//  get limited content from wherever
?>
<html>
<head>
<title></title>
</head>
<body>
    <p>Limited content goes here.  Get it here however and if you want to ...<a href='page.php?page=animals&id=27'>Read More</a></p>
</body>
</html>
Then your second page needs to be something like this

Code: Select all

<?php
if( array_key_exists( 'animals', $_GET)) {
    if( array_key_exists( 'id', $_GET) && ( $_GET['id'] = '27')) {
        //  load TIGER data
    } else if( array_key_exists( 'id', $_GET) && ( $_GET['id'] = '71')) {
        //  load ELEPHANT data
    }
} else if( array_key_exists( 'mineral', $_GET)) {
    //  load GOLD data
}
?>
<html>
<head>
<title></title>
</head>
<body>
    <p> this is where you display the results.</p>
</body>
</html>

There are a thousand ways of doing this. This is just one way. You can also forgo the second page and incorporate it into the main page using includes or iframes. This should give you a frame to work from.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

OK. Give me a minute to look over the code.
User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Re: Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

Cheers for looking into this you are a legend!

The row that has the contents of the post is just called 'content' if you needed to know, this seems to be the only bit that has ever puzzled me and is about time I knew how to :lol:
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

OK, try this. Basically it adds another if statement to see if the 'id' element is present and if it is it loads that page instead of the 'p' page. This is rough - you will need to polish some.

Code: Select all

if( isset( $_GET['p'])) {
    if( preg_match($sExpressie, $_GET['p'])) {
        header( 'Location:?p=error');
    } else {
        if( file_exists( $_GET['p'] . '.php'')) {
            if( isset( $_GET['id'])) {
                if( file_exists( $_GET['id']) . '.php') {
                    include $_GET['id'] . '.php';
                } else {
                    include $_GET['p'] . '.php';
                }
            } else {
                include $_GET['p'] . '.php';
            }
        }
    }
}
User avatar
CONFUSIONUK
Forum Newbie
Posts: 17
Joined: Thu Sep 24, 2009 5:15 pm
Location: Winchester, UK

Re: Dynamic PHP, MySQL paging

Post by CONFUSIONUK »

:D that works nicely BUT :lol:
where it includes say "?p=home&id=1" it comes up with "Warning: include(1.php) [function.include]: failed to open stream:" but thats because there is no such file i know that, how can i get it to read that post row in the table instead of a file?

Once again thanks so much It would have taken so long to even get that far :crazy:
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Dynamic PHP, MySQL paging

Post by phdatabase »

Well, I assume that the page that isn't there :lol: is going to only handle generic read more stuff, so it one page can be used to display many id=x. Given this I would replace

include $_GET['id'] . '.php';

with

include 'details.php?id=' . $_GET['id'];

which will call a page called 'details.php' with a query string 'id=x'. Create the 'details.php' page to check the $_GET array and query the database based on the id element. Then it will load a page with the required details.
Post Reply