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

时间:2013-07-17 13:56:32

标签: php smarty

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

如何将变量“$ minta”传递给sql查询以及如何将结果数组返回到php来显示?

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

提前谢谢。

Tuwanbi

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;
}

}
?>

1 个答案:

答案 0 :(得分:0)

声明private $keresoszo;它成为类对象,可以通过它访问 在您的SQL查询中$this->keresoszo您已将值赋给此对象但尚未使用它

<?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);