Welcome Guest, Not a member yet? Register   Sign In
Two questions ( max query and displaying images )
#1

[eluser]Brovar[/eluser]
Hi guys!

I'm new to CodeIgniter, and while it's very cool to use, i've ran into some problems.

I am currently making a webcomic/blog. On my main page i want to display latest comic.
Here are my 2 questions regarding that issue:

1. Easiest way to do that would be to just get comic from database with max id. I actually managed to make it work, but it seems to be not optimal.

Here's the code i used in my model for that task:

Code:
public function get_comic
{

  $this->db->select_max('ComicId');
                $query = $this->db->get('comic');
  $max = $query->row_array();
  $maxx = $max['ComicId'];
  $this->db->where('ComicId', $maxx);
  $query = $this->db->get('comic');
  return $query->row_array();
}

Is it ok, or is there a better way to do that?

2. Next i want to post the image i got in my view. I store my images on the server, and i have ComicUrl column in comic table, which has entries like "images/1.jpg". I tried to use img() via html helper, but it looks like you can't use objects from array as url there. ATM i have no idea how to display that comic.

Any help would be appreciated!
#2

[eluser]Aken[/eluser]
If your comics are in order by their ComicId, you can just select the highest one by ordering them by ComicId in descending order, and limit the query to one result.

Code:
// UNTESTED, should be close to what you need at least.
public function get_comic()
{
return $this->db
  ->order_by('ComicId', 'desc')
  ->limit(1)
  ->get('comic')
  ->row_array();
}

// If you call this function like this:
$comic = $this->your_model->get_comic();

// Then you access your column names like:
// $comic['column_name']
$image_url = $comic['ComicUrl'];
#3

[eluser]Brovar[/eluser]
Hey.

Thanks for the answer, someone already posted this in the other topic. I'm gonna do it this way from now on. Also i just decided to show images by using img src and echoing url from database.




Theme © iAndrew 2016 - Forum software by © MyBB