将php变量发送到一个类,并使用Smarty运行mysql查询


Send php variable to a class and run mysql query with Smarty

我想向运行mysql查询的类发送一个php变量。这是典型的通过html表单进行搜索。我必须使用Smarty。

如何将变量"$minta"传递给sql查询,以及如何将结果数组返回到php进行显示?

Smarty tpl文件正常(lista_keres.tpl带有ingatlank变量)。

提前谢谢。

图万比

php:

if (isset($_POST['keresoszo'])){
  include_once "classes/ingatlan.class.php";
  include_once "classes/main.class.php";
  include_once "classes/felhasznalo.class.php";
  $ingatlan = new Ingatlan();
  $felhasznalo = new Felhasznalo();
  $minta = $_POST['keresoszo'];
  $kereses = new Main();
  $kereses->getKeresIngatlan($minta);
        $smarty->assign("kapcsolattartok", $ingatlan->getKapcsolattartok());
        $smarty->assign("ingatlank", $main->getKeresIngatlan());
        $smarty->assign("include_file", lista_keres);
 echo $minta;

}

类别:

<?php
class Main{
private $keresoszo;
...
public function getKeresIngatlan($minta){
    $this->keresoszo=$minta;
    $ret = array();
    $sql="SELECT id FROM table WHERE id LIKE '% ".$keresoszo." %'";
    $ret = $this->db->GetArray($sql);
    return $ret;
}
}
?>

声明private $keresoszo;时,它成为类对象,可以通过$this->keresoszo在您的sql查询中,您已经为此对象分配了值,但尚未使用它

<?php
class Main{
private $keresoszo;
...
public function getKeresIngatlan($minta){
    $this->keresoszo=$minta;
    $ret = array();
    $sql="SELECT id FROM table WHERE id LIKE '% ".$this->keresoszo." %'";
    $ret = $this->db->GetArray($sql);
    return $ret;
}
}
?>

以下是如何返回结果

  $kereses = new Main();
  $results_set=$kereses->getKeresIngatlan($minta);
  var_dump($results_set);// for testing only 
        $smarty->assign("my_results_set", $results_set);