Bunu nasıl sayfalandırmaya bir filtre butonu eklerim?

2 Cevap php

Hey, ben tıklandığında sayfalama sonuçları filtre edeceğini, bir düğme (bağlantı) eklemek istiyorum.

Ben php (ve genel programlama) için yeni ve 'Otomotiv' ve tıklandığında burada gördük benim Sayfalandırması komut 2 mysql sorguları, güncellemeleri gibi bir düğme eklemek istiyorum:

Gördüğünüz gibi, otomotiv kategorisinde ben dinamik olmak istiyorum, kodlanmış, bu yüzden bir bağlantı tıklandığında o id veya sınıf sorgu kategorisi kısmında ne olursa olsun yerleştirir.

1:

$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'"));

2:

$get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page");

Bu ben kullanıyorum tüm güncel php sayfalama script:

<?php

    //connecting to the database
    $error = "Could not connect to the database";
    mysql_connect('localhost','root','root') or die($error);
    mysql_select_db('ajax_demo') or die($error);

    //max displayed per page
    $per_page = 2;

    //get start variable
    $start = $_GET['start'];

    //count records
    $record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'"));

    //count max pages
    $max_pages = $record_count / $per_page; //may come out as decimal

    if (!$start)
       $start = 0;

    //display data
    $get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page");
    while ($row = mysql_fetch_assoc($get))
    {
     // get data
     $name = $row['id'];
     $age = $row['site_name'];

     echo $name." (".$age.")<br />";

    }

    //setup prev and next variables
    $prev = $start - $per_page;
    $next = $start + $per_page;

    //show prev button
    if (!($start<=0))
           echo "<a href='pagi_test.php?start=$prev'>Prev</a> ";

    //show page numbers

    //set variable for first page
    $i=1;

    for ($x=0;$x<$record_count;$x=$x+$per_page)
    {
     if ($start!=$x)
        echo " <a href='pagi_test.php?start=$x'>$i</a> ";
     else
        echo " <a href='pagi_test.php?start=$x'><b>$i</b></a> ";
     $i++;
    }

    //show next button
    if (!($start>=$record_count-$per_page))
           echo " <a href='pagi_test.php?start=$next'>Next</a>";

    ?>

2 Cevap

2 bağlantılar / kategorileri ile Örnek:

<a href='script.php?category=automotive'>automotive</a> <a href='script.php?category=sports'>sports</a>

Script.php İçinde:

$category = mysql_real_escape_string($_GET['category']);
$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='$category'"));
...
$get = mysql_query("SELECT * FROM explore WHERE category='$category' LIMIT $start, $per_page");

Düzenleme: Ben sadece o programlama yeni olduğunu belirten OP karşılık bu cevabı yayınlanmıştır. İyi hijyen çok kolay başlangıç ​​olur ve alışkanlık öğrendi, ya da çok daha sonra ve alışkanlık haline getirmek çok zor olur.

http://us2.php.net/manual/en/security.database.sql-injection.php

Ayrıca veritabanları için güvenlik ile ilgili bu makaleyi okuyun lütfen. Bu çok sorguları olarak yazmak için geliştirilmiş olacaktır:

$start = mysql_real_escape_string($_GET['start']);
settype($start, 'integer');
$category = mysql_real_escape_string($_GET['category']);
$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='%s'", $category));
...
$get = mysql_query("SELECT * FROM explore WHERE category='%s' LIMIT %d, %d", $category, $start, $per_page);

Bu ekstra güvenli kod yazmak için her zaman değer, hatta sadece uygulama için eğer. Veritabanları için temel güvenlik kuralları şunlardır: güven Asla kullanıcı girişi, her zaman üretilen çıkışınızı kontrol edin. Girdi bir sorgu dizesi olduğunu ve veritabanına karşı çalıştırmak alır bu yana, filtrelenmesi gerekmektedir.