Welcome Guest, Not a member yet? Register   Sign In
Next button giving error when on last page.
#1

[eluser]jim-_-[/eluser]
I'm learning so I'm making a page for Images, where i upload images and input info to the Database, the urls are made to look like direct image links, The 3rd segment is the filename I just redirect the <img src=""> tag to the right directory then do a DB call to get the image.

I collect the ID also + 1 and make another DB call to get a thumbnail for the Next button, I have made a IF query to check if the DB call returns anything, if it doesn't I send the user to the first image instead...

here is the problem when I'm on this last page it still gives me the

Code:
Severity: Notice
Message: Undefined offset: 0
Filename: models/gallery_model.php
Line Number: 112

To clarify, this does not happen on any of the other image links.

The IF statement
Code:
$nextimg = $data['img']->id + 1;
   if ($this->gallery_model->get_next_image_by_id($nextimg))
        { $data['next'] = $this->gallery_model->get_next_image_by_id($nextimg); }
   else { $data['next'] = $this->gallery_model->get_next_image_by_id('1'); }

The database call from the model
Code:
function get_next_image_by_id($id) {
$this->db->where('id', $id);
$query = $this->db->get('img');
$image = $query->result();
return $image[0];
}

PS: I'm not 100% in any of the languages I'm only able to learn by doing.

Second question,
I know $image[0] gets the first image from the query, there should only be one image, why is it an array (I know its my coding that makes it an array I just need to know how I can do it different.) on the other hand this saves the site from crashing if I have a duplicate answer to the query. (This ID field is not Auto Increment, I will only give sequential IDs to "approved" images)
#2

[eluser]Aken[/eluser]
When you're performing the query, you're looking for an image that doesn't exist. So there are no results. $image[0] doesn't exist because $image is an empty array ($query->result() will always return an array).

Here's how I would do it:

Code:
// MODEL

function get_next_image($current_id)
{
$next_id = $current_id + 1;

$query = $this->db->where('id', $next_id)->get('img');

if ($query->num_rows() === 1)
{
  // Next image found.
  $result = $query->result();
  return $result[0];
}
else
{
  // No image found - go back to the first.
  $f_query = $this->db->where('id', 1)->get('img');
  
  if ($f_query->num_rows() === 1)
  {
   $f_result = $f_query->result();
   return $f_result[0];
  }
  
  // No first image found.
  return FALSE;
}

// In case everything above breaks.
return FALSE;
}

// CONTROLLER

$data['next'] = $this->gallery_model->get_next_image($data['img']->id);

Then, in your view, just check if $next is false, and if it isn't, display the link/image.
#3

[eluser]jim-_-[/eluser]
Nice, worked like a charm. Now I see how I have to write a lot of code differently.

Thank you very much Smile
#4

[eluser]Aken[/eluser]
Wasn't good enough for me. Here's even better:

Code:
// MODEL

public function get_image_by_id($id)
{
$query = $this->db->where('id', $id)->get('img');

if ($query->num_rows() === 1)
{
  return $query->row();
}

return FALSE;
}

public function get_next_image($current_id)
{
if (($nextimg = $this->get_image_by_id($current_id + 1)) === FALSE)
{
  $nextimg = $this->get_image_by_id(1);
}

return $nextimg;
}

// CONTROLLER

$data['next'] = $this->gallery_model->get_next_image($data['img']->id);
#5

[eluser]jim-_-[/eluser]
Now I'm confused, you split it into two different functions for the model ?

The get_image_by_id, does this override the model-function I am fetching the image with or is it an additional function?

(the single image page, gets the Image and the next image...)
#6

[eluser]Aken[/eluser]
The get_image_by_id() method probably does something very similar to the method you're using to fetch the image. It depends on how your model is laid out and what information you're retrieving.




Theme © iAndrew 2016 - Forum software by © MyBB