0

I'm new to coding.

CONTEXT I have one table 'online_event' that holds the details to my event page. I have another table 'event_gallery' that holds the photos to the same event page. The primary key in 'online_event' is the foreign key in 'event_gallery'

so one event from 'online_event' can have multiple photos inside the 'event_gallery' table.

I'm able to retrieve one photo in the event_gallery table by using the foreign key (which is just the event id).

My question is, how can I call all pictures? it's only echoing the first of 3 photos i have saved in the table with that id number.

$gallery_table =  Eventgallery::find_by_id($_GET['id']);
        if($gallery_table){

            $images = $gallery_table->image_name;

        }

      print_r($images);

Here is the function that retrieves the data


  public static function find_by_id($id){
    global $database;
    $the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table. " WHERE id = $id");
        return !empty($the_result_array) ? array_shift($the_result_array) : false; //ternary operator

    }

Because the method is returning an array_shift, shouldn't the variable I created ($images) be an array? When I print_r() it comes out as just 1 result, not an array.

Once I have it as an array, i want to loop through it and echo it all out.

Thank you

1
  • 1
    Because you are using array_shift() on the result array. array_shift() removes the first element from a provided array and returns its value. If you want the entire array don't use array_shift() on it. return it as it is. Commented Jul 29, 2019 at 11:03

1 Answer 1

0

The array_shift() function is used to remove the first element from an array, and returns the value of the removed element so your getting only one image. If you want all the data your code should be like this:

public static function find_by_id($id){
    global $database;
    $the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table. " WHERE id = $id");
        return empty($the_result_array) === false ? $the_result_array : [];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Okay great! this brought me one step closer.
Accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.