Page 1 of 1

Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 8:43 am
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:

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 10:25 am
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 ...


Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 10:35 am
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:

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 10:45 am
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?

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 11:20 am
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....

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 11:49 am
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.

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 12:01 pm
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

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 12:43 pm
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.

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 12:46 pm
by phdatabase
OK. Give me a minute to look over the code.

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 1:09 pm
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:

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 1:22 pm
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';
            }
        }
    }
}

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 2:10 pm
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:

Re: Dynamic PHP, MySQL paging

Posted: Sun May 30, 2010 2:57 pm
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.