0

繰り返さないように関数を作りたいです。これは私の現在のコードです

   <?php

$targetpage = "index.php";  
$limit = 20;
    $sql1 = $db->prepare("SELECT * FROM classified ORDER BY date DESC");
/*** fetch Number of results ***/
$total_pages =$sql1->rowCount();
    $stages = 3;
$page = ($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }
    $sql = $db->prepare("SELECT * FROM classified ORDER BY date DESC LIMIT $start, 
   $limit  ")or die(print_r($sql->errorInfo(), true));
$sql->execute();
$result = $sql->fetchAll();
//Include pagination
 require_once("pagination.php");
 // pagination
echo $paginate;
foreach($result as $row){
$id = htmlentities($row['id'], ENT_QUOTES);
$id_city = htmlentities($row['id_city'], ENT_QUOTES);
$title = htmlentities($row['title'], ENT_QUOTES ,'utf-8');
 $querya = $db->prepare("SELECT * FROM city WHERE id = :id_city");
/*** bind the paramaters ***/
$querya->bindParam(':id_city', $id_city, PDO::PARAM_INT);
/*** execute the prepared statement ***/
$querya->execute();
/*** fetch the results ***/
$resultya = $querya->fetchAll();
    foreach($resultya as $rowa)
{
 $city_name = htmlentities($rowa['city'], ENT_QUOTES, 'utf-8');
 }
} 
?>

これで、データベースからデータを取得するときの条件があることを除いて、同じコードを使用する別のファイルがあります。したがって、代わりに:

  $sql1 = $db->prepare("SELECT * FROM classified ORDER BY date DESC");
  $sql = $db->prepare("SELECT * FROM classified ORDER BY date DESC LIMIT $start, 
  $limit  ")or die(print_r($sql->errorInfo(), true));

他のファイル:

 $sql1 = $db->prepare("SELECT * FROM classified where type = '1' ORDER BY date DESC");
 $sql = $db->prepare("SELECT * FROM classified where type = '1' ORDER BY date DESC 
 LIMIT $start, $limit  ")or die(print_r($sql->errorInfo(), true));

違いは、type=1の場合です。

これらすべてを1つの機能に組み合わせることができますか?

ありがとう

4

2 に答える 2

-1

はい。1つのファイルを用意し、そのファイルにPOSTまたはGETを送信して、type==1かどうかをページに通知します。

if ($_GET['t']==1){ $type = "WHERE type = '1'"; }else{ $type = ""; }


$sql1 = $db->prepare("SELECT * FROM classified " . $type . " ORDER BY date DESC");
$sql = $db->prepare("SELECT * FROM classified " . $type . " ORDER BY date DESC 
LIMIT $start, $limit  ")or die(print_r($sql->errorInfo(), true));

これがpage.php?t = 1でリストされているページを呼び出すと、whereステートメントがそこに配置されます。page.phpを呼び出すだけでは、何も配置されません。

于 2013-02-05T21:29:07.770 に答える
-1

コードにいくつかのメモを追加しましたが、これは意味がありますか?下記参照:

<?php
/*
    $db - PDO object 
    $limit - number of listings to show on the page
    $page - page to show
    $where - the additional where condition to pass in
*/
function getItems($db=null,$limit=20,$page='',$where='') {
    // check if $db exists before doing anything
    if ($db) {
        $sql1 = $db->prepare("SELECT * FROM classified ORDER BY date DESC");
        /*** fetch Number of results ***/
        $total_pages =$sql1->rowCount();
            $stages = 3;
        if($page){
            $start = ($page - 1) * $limit; 
        }else{
            $start = 0; 
            }
            $sql = $db->prepare("SELECT * FROM classified $where ORDER BY date DESC LIMIT $start, 
           $limit  ")or die(print_r($sql->errorInfo(), true));
        $sql->execute();
        $result = $sql->fetchAll();
    } else {
        // I return an empty array here so that if something should fail $result will still be populated with an array
        return array();
    }
}


$targetpage = "index.php";  

$result = getItems($db,20,$_GET['page'],'where type = "1"');

//Include pagination
 require_once("pagination.php");
 // pagination
echo $paginate;

foreach($result as $row){
    $id = htmlentities($row['id'], ENT_QUOTES);
    $id_city = htmlentities($row['id_city'], ENT_QUOTES);
    $title = htmlentities($row['title'], ENT_QUOTES ,'utf-8');
     $querya = $db->prepare("SELECT * FROM city WHERE id = :id_city");
    /*** bind the paramaters ***/
    $querya->bindParam(':id_city', $id_city, PDO::PARAM_INT);
    /*** execute the prepared statement ***/
    $querya->execute();
    /*** fetch the results ***/
    $resultya = $querya->fetchAll();
     foreach ($resultya as $rowa) {
        $city_name = htmlentities($rowa['city'], ENT_QUOTES, 'utf-8');
     }
} 
?>
于 2013-02-05T22:40:26.203 に答える