1

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.

OzzC
  • 815
  • 4
  • 20
  • 36
  • What does your .htaccess look like in the /categories/ directory? – Nebez Briefkani May 26 '14 at 01:21
  • http://stackoverflow.com/questions/3033407/how-can-i-create-friendly-urls-with-htaccess?rq=1 – The Alpha May 26 '14 at 01:29
  • Thanks for your answer Nebez, my .htaccess is exactly like in my question and its the same for my whole project. It´s my function getHome that handle url, my function get my pages according to what I write in url. My variable $url gets url, that is the parameter which I put in my .htaccess, and then if we dont pass any value, it will include index, else it will include the value (page) passed. Then if value (page) we pass in url exits it will include that file, else I will include my page "404". – OzzC May 26 '14 at 01:40
  • simply explode the url on '/' then check entry for a simple numeric. That entry will be your'2'. – user3629249 May 26 '14 at 01:41
  • Thanks for the reply. But could you be more specific? Because I didnt understand correctly your solution, because I'm already doing explode on "/". – OzzC May 26 '14 at 01:57

1 Answers1

1

Try using array_pop to get the last value of url then check is_numeric

 function getHome(){
   $url = (isset($_GET['url'])) ? $_GET['url'] : $_SERVER['REQUEST_URI'];
   $url = explode('/', $url);
   $template = $url[0] == NULL ? 'index' : $url[0];
   $last = array_pop($url);
   $page = (is_numerica($last)) ? $last : 1;

   if ($template == 'index') {
     return $page;
   }

   if(file_exists("tpl/$template.php")) {
     require_once("tpl/$template.php");
   } else {
     require_once('tpl/404.php');
   }
 }

 $page = getHome(); // $page is used in index.php
Victory
  • 5,811
  • 2
  • 26
  • 45
  • Thanks for your answer Victory. But its not working Im getting an error: array_pop() expects parameter 1 to be array, string given. I think its because, when I acess my index.php in localhost/project/ my $url[0] it is "index", that´s a string. – OzzC May 26 '14 at 02:07
  • Thanks Victory, now I dont get errors, but still dont work! And I think my mod_rewrite is working. Only dont works on my url localhost/project/, because in my function I say that if we pass some value that dont exist it will include my 404 error page. I just dont see how to change my function to get numbers I pass also in my index.php, for example localhost/project/3. Because if it is in "categories.php", like localhost/categories/3 its working I get my "3". – OzzC May 26 '14 at 13:51
  • @OzzC - i have updated, you would need to return the `$page` and use that in the rest of your `index.php` file. – Victory May 26 '14 at 16:03
  • Thanks for all help Victory, but still does not work :/ – OzzC May 26 '14 at 21:20