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

是否可以将所有这些组合在一个功能中?

谢谢

4

2 回答 2

-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 回答
-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 回答