• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with returned data from a model and getting output

Error with mini-description is here:


class Linksmodel extends Model {
    function Linksmodel() {

    function get_top() {
        $links = $this->db->query('SELECT link_clicks, link_name, link_id FROM awLinks ORDER BY link_clicks DESC LIMIT 5');
        return $links->result();
    function countup_and_get_link($link_id) {
        if(ctype_digit($link_id)) {
            $this->db->query('UPDATE awLinks SET link_clicks = (link_clicks + 1) WHERE link_id = '.$link_id);
            $link = $this->db->query('SELECT link_url FROM awLinks WHERE link_id = '.$link_id);
            return $link->result();
        } else {
            return FALSE;


And the controller:

class Links extends Controller {
    public $awTitle = 'awpti.org - where geeks roam free! (Link Catalog)';
    public $awHeader = 'awpti.org';
    function Links() {

        $this->view->set('Title', $this->awTitle);
        $this->view->set('Title', $this->awHeader);

        $this->load->model('linksmodel', 'links', TRUE);

        $this->view->part('template_top', 'common/header.php');
        $this->view->part('template_bottom', 'common/footer.php');

    function index() {
        $links = $this->links->get_top();
        foreach($links as $link) {
            echo '<a href="/links/linkout/'.$link->link_id.'">'.$link->link_name.' ('.$link->link_clicks.')</a><br />';

    function linkout() {
        if(ctype_digit($this->uri->segment(3)) ) {
            $link = $this->links->countup_and_get_link($this->uri->segment(3));
            echo '<br />This error is from "print $link->link_url", which looks right above..<br />';
            print $link->link_url;
        } else {


I'm stumped. Do I need to do some goofy loop action for a single returned entry?

[quote author="awpti" date="1185972648"]Do I need to do some goofy loop action for a single returned entry?[/quote]
User Guide: Generating Query Results
[quote author="User Guide"]result()
This function returns the query result as an array of objects, or an empty array on failure.
This function returns a single result row.[/quote]

FWIW, I've been using something like this for functions that should return a single row:
function select_single($discussion_id)
    $this->db->where('discussion_id', $discussion_id);
    $query = $this->db->get('discussions', 1);

    return ($query->num_rows() != 0) ? $query->row() : FALSE;

Sometimes I feel completely stupid. Simplest thing in the world.

On a side note, I didn't like how redirect() doesn't allow you to redirect "offsite", so I dropped in my own little redirect_offsite() function. I suppose I could have just added another case to the switch() statement in redirect(), but I wanted to leave the code vanilla there.

Thanks for the help, Bravo.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.