PHP Articles


Daca aveti un website care foloseste baze de date, probabil ca aveti nevoie si de afisarea informatiilor pe mai multe pagini printr-o modalitate simpla si care sa poata fi usor implementata pentru orice caz.

De ce avem nevoie de paginare?

Sa ne inchipuim ca in site-ul nostru avem o sectiune care afiseaza produse din baza de date sub forma unui listing. Din motive de estetica si ergonomie insa, nu vom putea afisa toate produsele pe aceeasi pagina. De aceea, este o idee buna sa impartim rezultatul in mai multe pagini.

Descrierea sistemului

Paginarea reprezinta, de fapt, impartirea unui mare numar de date in mai multe pagini. Navigarea in aceste pagini se va face prin intermediul unor linkuri afisate sub forma:

Pima pag Precedenta 1 2 3 4 5 Urmatoarea Ultima pag

In scop didactic, vom considera ca sistemul nostru trebuie sa genereze paginarea pentru afisarea unui numar foarte mare de produse dintr-o baza de date.

Ganditi-va ca numarul total de pagini este mai mare decat 20. Pentru estetica in afisare, ar fi o idee buna sa afisam doar un numar din acestea in functie de pagina curenta. Astfel, in partea stanga si dreapta de pagina curenta vom avea, sa zicem, maxim 5 linkuri.

Sistemul pe care il voi prezenta in continuare are urmatoarele particularitati:

  • poate fi folosit pentru orice caz de paginare
  • utilizeaza o functie care poate fi portata pentru orice alta aplicatie
  • functia va returna un array pe care il vom putea prelucra ulterior
  • poate fi folosita in cazul in care utilizam un sistem “template engine” (gen Smarty)

Algorim

Pentru inceput sa evidentiem variabilele care intervin in paginare:

  • pagina curenta (de obicei preluata prin $_GET)
  • numarul de produse afisate pe o pagina
  • numarul total de produse
  • numarul total de pagini
  • numarul maxim pe linkuri afisate in stanga/dreapta paginii curente

Ne conectam la BD

Efectuam o interogare asupra BD pentru a afla numarul total de produse

Calculam numarul total de pagini in functie de numarul total de produse si numarul de produse care se afiseaza pe pagina.

Construim functia care se va ocupa de generarea propriu zisa a paginarii. Aceasta functie va returna un array care va contine textul si url-ul fiecarui element din paginare.

A popular debate is how to run 2 versions of PHP at the same time. This extract shows two ways to achieve this.

Requirements:

  • Apache 2x installed
  • PHP versions

This assumes that you have followed the steps outlined in the other articles in this section. In this example we will add PHP6 to the existing installation of PHP5.2.x.

(more…)

Introduction
As you may know, clean URLs can be made with Apache’s mod_rewrite, but you are able to make clean URLs with PHP only.This tutorial requires minimum knowledge of PHP.

Contents
This tutorial is split up into the following parts:

  • Introduction
    Method one
    Implementing method one
    Method two
    Implementing method two
    Conclusion

  • Method one

    The second method will take /users/2 and it’s purpose is to go to the users page on page 2.

    $array = explode('/',$_SERVER['PATH_INFO']);
    array_shift($array);
    ?>

    PHP contains several predefined variables, one of them is $_SERVER[’PATH_INFO’]. To know what the first line does you must know the explode function. The explode function takes two arguments. The first is the seperator and the second is the string. The explode function takes the string and splits it up where the seperator is.

    array_shift removes the first key of an array.

    Implementing method one
    First you should put the above snippet in a file called something like clean_urls.php and include it on the top of your pages (or if you only have one page, just put it in the top).

    Here is how you could use it:


    include 'clean_urls.php'; // Include the snippet
    $modules = array( // Define our array of valid modules
    'home',
    'users',
    'login',
    'register',
    'admin',
    );

    $module = $array[0]; // The first key contains what we will use as our module
    $page = $array[1]; // The second key contains what we will use as the page number

    if(empty($module)) // We need a default module
    {
    $module = "home";
    }

    if(in_array($module,$modules)) // Does the requested module exist?
    {
    include "./modules/{$module}.php"; // If yes: Include it
    }
    else {
    die("Failed to load module '{$module}'"); // If no: Error message
    }
    ?>

    Then you would use the page variable for something useful in your module page.


    Method two

    The second method will convert /func/users/page/2 into what ?func=users&page=2 would have done.

    Quick Code


    $array = explode('/',$_SERVER['PATH_INFO']);
    for($i=1; $i<=count($array)-1; $i+=2)
    {
    $_GET[$array[$i]] = $array[$i+1];
    }
    ?>

    Here the first line is the exact same line as in method one.

    Next we have a for loop. It starts by setting $i to 1, then it will run as longs as the second statement ($i<=count($array)-1) evaluates to true and by the end of each iteration it will add 2 to $i. Inside the loop we add to the $_GET variable.

    Implementing method two
    Method two is easy to implement. Just put the above snippet in a file called something like clean_urls.php and include it on each page.

    Conclusion
    As you see, clean URLs are very easy to make with PHP only. With only 2-4 lines, you can have clean URLs.