Im starting to devellop a pagination system, and so I´ll need to get some numbers in my URL, but Im having one problem.
I have a project root folder "project", inside this folder I have:
- 1 .htaccess file
- 1 index.php file, that is where I call my getHome() function, to include the correct page and is where I import css, javascripts files, etc
- 1 folder with name "tpl", and inside it, I have my other index.php file that is my homepage and my other php files (categories.php, contacts.php,...)
To acess my homepage, Im using this url: localhost/project/
And now Im trying to get the numbers I pass in URL with this code:
$page = $url[1];
$page = ($page ? $page : 1);
echo '<h1>'.$page.'</h1>';
The problem is,
If I use this code to get number that I pass in URL in my "categories.php" file, like this: "htttp://localhost/projet/categories/2" -> it's working fine, I get echo of "2" and I have my categories.php file included, but wih one problem, I have some images im my categories.php file and if I use localhost/project/categories I have my images included correctly, but If I use localhost/project/categories/test-1 I can get value I pass in my url and my categories page is included but my images dont appear, images just appear in localhost/project/categories.
If I use this code to get number that I pass in URL in my "index.php" file, like this "htttp://localhost/project/2" Im getting my page 404 error "tpl/404.php", that I include in my getHome() function.
Do you see some way, using my function getHome(), how I can get the number I pass in url, using for example localhost/project/3, and have my index.php file included normally, and dont have my 404 page tpl/404.php' included? And also how I can my solve my images problem with my categories page?
This is my function getHome()
function getHome(){
$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('tpl/'.$url[0].'.php'))
{
require_once('tpl/'.$url[0].'.php');
}
else
{
require_once('tpl/404.php');
}
}
This is my .htaccess file:
RewriteEngine OnRewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Also, If I use index in my URL, like this: htttp://localhost/projet/index/2, it works, I can get my url value of "2" and I have my home page included correctly. But I´m trying to have just my htttp://localhost/project/2 and get the value I pass, in this case "2" with my homepage and not my 404 error page.