0

So the code is

$gallery = get_gallery('gallery_id');
print_r ($gallery);

And I get:

Array ( [0] => Array ( [id] => 13 [image] => 0 [user] => 13 [timestamp] => 1366237591 [ext] => png [caption] => Happy smile shi ma? [comment] => ) [1] => Array ( [id] => 14 [image] => 0 [user] => 13 [timestamp] => 1366237954 [ext] => jpg [caption] => Confused [comment] => ) [2] => Array ( [id] => 15 [image] => 0 [user] => 13 [timestamp] => 1366237979 [ext] => jpg [caption] => Facebookerg [comment] => ) [3] => Array ( [id] => 16 [image] => 0 [user] => 13 [timestamp] => 1366377510 [ext] => gif [caption] => lolwut? [comment] => ) [4] => Array ( [id] => 17 [image] => 0 [user] => 13 [timestamp] => 1366380899 [ext] => jpg [caption] => rorwut? [comment] => ) [5] => Array ( [id] => 18 [image] => 0 [user] => 13 [timestamp] => 1366651685 [ext] => jpg [caption] => Notes? [comment] => ) [6] => Array ( [id] => 19 [image] => 0 [user] => 13 [timestamp] => 1366711880 [ext] => jpg [caption] => asd [comment] => ) [7] => Array ( [id] => 20 [image] => 0 [user] => 14 [timestamp] => 1366940983 [ext] => jpg [caption] => Belzelga [comment] => ) )

Which is good, it finally worked. But how do you display a single data/table. Because I am trying to get a single 'id' from these thingies. I tried echoing $gallery['id'] but I got an error. :/

1
  • your problem is not what you're printing - but what your get_gallery function returns - if it suppose to give you just one image - you don't need to return all the data of the others Commented Apr 27, 2013 at 15:34

5 Answers 5

2

You need to access the correct index first:

$gallery[0]['id']
//      ^^^
Sign up to request clarification or add additional context in comments.

Comments

1

Your $gallery variable is now a multi dimentional array.

You have

$gallery[0]['id'];
$gallery[1]['id'];
.....

Now you can either use a foreach to process through the array or a for loop

foreach ($gallery as $anItem ) {
    echo $anItem['id'];
}

OR

for ( $x=0; $x < count($gallery); $x++ ) {
    echo $gallery[$x]['id'];
}

Comments

0

Try this looping code-segment:

foreach( $gallery as $temp ) {
    echo $temp['id'];
}

Your $gallery is a multidimensional array. You need to iterate through it.

1 Comment

foreach loop is the only way to display it? I tried to use foreach loop as you showed but it displayed all the 'id' from the table :'(
0

If you want to only display a particular gallery, you should probably use something like www.yoursite.com/gallery.php?id=1. then in your code to display it

    if(isset($_GET['id']))
    {
         foreach( $gallery as $g ) 
         {
           if($g['id'] == $_GET['id'])
                 echo $g['id'];
         }
    }

Comments

0

You can get the data so. Without being bound to a key.

current($gallery)['id']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.