繁体   English   中英

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

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

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

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

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

先感谢您。

图万比

的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值分配给该对象但尚未使用它的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM