CodeIgniter:发送到模型的变量问题

时间:2011-03-01 15:52:10

标签: variables codeigniter model

我正在继承其他人的项目,我正在努力让自己熟悉它。我对CI的经验很少。

在其中一个视图中有一个下拉表单,在更改时调用JS函数:

$(document).ready(function()
{
    // admin contorller drop down ajax
    $("#catagoryDropDownList").change(function()
    {
        getCatagoriesItems();
    });

    // initiate table sort
    TableSorter.prepareTable($("#dataResultsTable"));
});

// ajax request triggered by catagory drop down menu selection
function getCatagoriesItems()
{

    blockPage();

    // get base url of current site
    var baseurl = $("#site_url_for_ajax").val();

    // get adminType
    var adminType = $("#admin_type").val();

    // get catagory id
    var catId = $("#catagoryDropDownList option:selected").attr("id");

    var queryString = baseurl + "home/ajaxCatagorySelection/" + catId + "/" + adminType;

    $.get(queryString, function(data)
    {
        var obj = jQuery.parseJSON(data);

        // dump data into table when request is successful
        $("#dataResultsTable tbody").html(JSONParser.parseHomeDropDownSelectedJSON(obj));

        // unblock page when done
        $.unblockUI();
    });
}

我记录了两个值catID和adminType,它们都是整数,catID将介于1-10和adminType = 1之间。这两个值都引用了数据库中的int值。 catID引用标题为“categoryID”的字段。 catID 6 =全部。数据库中的所有条目都没有6作为它们的值,因此确保如果您过滤了不等于6,那么您将得到所有条目。它们被传递给控制器​​文件home.php中名为ajaxCatagorySelection的函数。到现在为止还挺好。这是函数:

public function ajaxCatagorySelection($tableName, $id)
    {
        $vars = new DatabaseRetriever($id);

        $resultsArray = $vars->getDataForSpecifiedTable($tableName, $id);

        echo json_encode($resultsArray);
    }

并且该函数本身引用了一个模型(database_retriever.php)和类DatabaseRetriever,我假设将变量传递给函数getDataForSpecifiedTable。我说假设因为变量名从catID到$ tableName和adminType到$ id显着变化。这是getDataForSpecifiedTable:

public function getDataForSpecifiedTable($catagoryInfo, $databaseID)
    {


        // connect to database
        $sql = $this->connect($databaseID);

        if ($catagoryInfo != 6) { 
            // build SQL Query and query the database
            $result = $sql->query("SELECT fileId, fileTitle, filePath, fileTypeExt, fileDescription, fileModed from admin_files where catagoryId = '" . $catagoryInfo . "' and adminId = '" . $databaseID . "'");
        } else { 
            $result = $sql->query("SELECT fileId, fileTitle, filePath, fileTypeExt, fileDescription, fileModed from admin_files where catagoryId = '" . $catagoryInfo . "' and adminId = '" . $databaseID . "'");
        }


        // declare array
        $items = array();

        // retriever rows from database and build array
        while ($row = $result->fetch_row())
        {
            array_push($items, $row);
        }

        // disconnect from database
        $this->disconnect();

        // return data in array
        return $items;
    }

变量名称再次发生了变化,但你可以告诉他们通过查看查询来做我上面写的。这是问题所在。我添加了条件“if($ catagoryInfo!= 6)...”,如果我没有把else放在那里,那么CI会抛出没有数据返回的警告错误。我返回$ categoryInfo并在FireBug控制台中获得正确的整数。我已经尝试将条件作为整数和两个都失败的字符串。任何想法可能会发生在这里?

1 个答案:

答案 0 :(得分:2)

如果database_retriever.php是模型,您应该这样称呼它:

$this->load->model('database_retriever');
$resultsArray = $this->Database_retriever->getDataForSpecifiedTable($tableName, $id);

另外,请确保您的模型extends Model(或CodeIgniter 2中的extends CI_Model)。

注意:$.getJSON会自动为您解析JSON,因此您无需致电parseJSON