Welcome Guest, Not a member yet? Register   Sign In
Issue with returned data from a model and getting output
#1

[eluser]awpti[/eluser]
Error with mini-description is here:

http://www.awpti.org/links/linkout/3/

Model:
Code:
class Linksmodel extends Model {
    
    function Linksmodel() {
        parent::Model();
    }

    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:

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

        $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));
            print_r($link);
            echo '<br />This error is from "print $link->link_url", which looks right above..<br />';
            print $link->link_url;
            //redirect_outside($link->link_url);
        } else {
            redirect('/errors/linkout/');
        }
    }

}

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

[eluser]BravoAlpha[/eluser]
[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.
...
row()
This function returns a single result row.[/quote]

FWIW, I've been using something like this for functions that should return a single row:
Code:
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;
}
#3

[eluser]awpti[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB