【问题标题】:Send php variable to a class and run mysql query with Smarty将 php 变量发送到一个类并使用 Smarty 运行 mysql 查询
【发布时间】:2013-07-17 13:56:31
【问题描述】:

我想将一个 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;
}

}
?>

【问题讨论】:

  • 你遇到了什么错误?
  • Smarty 模板未加载,我只得到白页。
  • 我没有看到任何 $smarty->render() 或类似的调用。代码中有其他地方吗?

标签: php smarty


【解决方案1】:

声明private $keresoszo;它成为类对象并且可以通过 $this-&gt;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);

【讨论】:

  • 感谢您的帮助。我已经修改了 php 和类,但是出了点问题,它什么也没做,模板文件也没有加载。如果我注释 $results_set=$kereses->getKeresIngatlan($minta); 行,则会加载模板(没有结果)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
相关资源
最近更新 更多