phpcodeignitervariablesmodel

CodeIgniter: Problem with variable sent to model


I'm inheriting someone else's project and I am trying to familiarize myself with it. I have little experience with CI.

On one of the views there's is a drop down form, on change calls a JS function:

$(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();
    });
}

I've logged the two values, catID and adminType, they're both integers, catID will be between 1-10 and adminType = 1. There both references to int values in a database. catID is referencing a field titled 'categoryID'. catID 6 = all. None of the entries in the db have 6 as their value, thus ensuring if you filtered for not equaling 6 you'd get all. They get passed to a function called ajaxCatagorySelection in the controller file home.php. So far, so good. Here's that function:

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

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

        echo json_encode($resultsArray);
    }

and that function itself is referencing a model (database_retriever.php) and the class DatabaseRetriever and I'm assuming passing the variables along to the function getDataForSpecifiedTable. I say assuming because the variable names have changed significantly from catID to $tableName and adminType to $id. Here is 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;
    }

the variable names have changed again but you can tell they are suppose to do what I wrote above by looking at the query. Here's the problem. I added the conditional "if ($catagoryInfo != 6)...", if I don't put the else in there then CI throws out warning errors that no data is being returned. I return $categoryInfo and in the FireBug console I get the correct integer. I've tried the conditional as an integer and a string with both failing. Any ideas what might be happening here?


Solution

  • If database_retriever.php is a model, you should call it like so:

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

    Also, make sure your model extends Model (or extends CI_Model in CodeIgniter 2).

    NOTE: $.getJSON, will auto-parse JSON for you, so you don't need to call parseJSON.