Welcome Guest, Not a member yet? Register   Sign In
Arrays, tables and headache
#1

[eluser]coldKingdom[/eluser]
Hello all you pro's out there!

I'm having a bit of a problem that I've been dealing with today and a result of that I'm on painkillers..no, lets go to the problem Smile

I am doing a picture gallery for users and i have three databases one user table, one for the pictures names and one album table..and the code looks like this

Code:
<?= heading("Bildgalleri", 2); ?>
<hr />
&lt;?php
    $tmpl = array('table_open' => '<table cellspacing="0" cellpadding="0" style="float:left;">');
    $this->table->set_template($tmpl);

    $header = '';
    $gallery = '';
    
    foreach ($data["gallery"] as $item):
        $img = array(
            'src' => '/uploads/pictures/small/'.$item["gallery_picture"],
            'alt' => '',
            'style' => "margin-right:3px;"
        );
        $link_attr = array(
            'rel' => "shadowbox[".$item[']."]",
            'title' => ""
        );
        
        if ($header != $item["gallery_album"]) {
            $header = $item["gallery_album"];
            print heading($item["album_name"], 3);
            echo anchor('/uploads/pictures/large/'.$item["gallery_picture"], img($img), $link_attr);
        }
        else {
            echo anchor('/uploads/pictures/large/'.$item["gallery_picture"], img($img), $link_attr);;
        }

    endforeach;
?&gt;

With this piece of code i wanna to achieve this:

Quote:Album name 1
Picture Picture Picture Picture
Picture Picture Picture Picture
Picture Picture Picture Picture

Album name 2
Picture Picture Picture Picture
Picture Picture Picture Picture


Something like this code wise

Quote:<h3>Album name 1</h3>
<table>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
</table>

<h3>Album name 2</h3>
<table>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
<tr><td>Picture</td><td>Picture</td><td>Picture</td><td>Picture</td></tr>
</table>
It's working right now without tables, now to the question.
How do i bring tables to this code, $this->table->make_columns($gallery, 4) seems great but it breaks every-time i try it. I need some fresh ideas!

I appreciate all help i can get!

Thanks!
#2

[eluser]Michael Wales[/eluser]
Don't use the tables helper - go straight HTML with it.

Psuedo-code, not your vars:
Code:
&lt;? foreach($albums as $album): ?&gt;
  <h3>&lt;?= $album->name; ?&gt;</h3>
  <table>
    <tr>
    &lt;? $count = 0; ?&gt;
    &lt;? foreach ($album->picture as $picture): ?&gt;
      &lt;? $count++; ?&gt;
      <td><img src="&lt;?= $picture->url; ?&gt;" /></td>
      &lt;? if (($count % 4) == 0): ?&gt;
        </tr><tr>
      &lt;? endif; ?&gt;
    &lt;? endforeach; ?&gt;
    </tr>
  </table>

&lt;? endforeach; ?&gt;
#3

[eluser]coldKingdom[/eluser]
Thanks for your fast reply!

Code:
&lt;? foreach($data["albums"] as $album): ?&gt;
  <h3>&lt;?= $album["album_name"]; ?&gt;</h3>
  <table>
    <tr>
    &lt;? $count = 0; ?&gt;
    &lt;? foreach ($data["gallery"] as $picture): ?&gt;
      &lt;? $count++; ?&gt;
      <td>&lt;?= img('/uploads/pictures/small/'.$picture["gallery_picture"]); ?&gt;</td>
      &lt;? if (($count % 4) == 0): ?&gt;
        </tr><tr>
      &lt;? endif; ?&gt;
    &lt;? endforeach; ?&gt;
    </tr>
  </table>
&lt;? endforeach; ?&gt;

I don't know what I'm doing wrong here, i get the same pictures in both albums. Can you see anything that's wrong?

Thanks again!
#4

[eluser]Michael Wales[/eluser]
It's because you are looping over the same $data['gallery'] - you need some sort of unique identifier to make it loop through that particular albums gallery.

In my example above, I have made a gallery a child-object of album object. You will probably just want to create a multi-dimensional array where each album has a gallery array, and underneath that, each gallery has a unique ID (if you are supporting multiple galleries per album).
#5

[eluser]coldKingdom[/eluser]
I have now come to a solution and i would like to share some with you all Smile

Model:
Code:
function get_gallery($user) {
        $this->db->from("tbl_gallery_albums");
        $this->db->where("album_user", $user);
        
        $query = $this->db->get();
        
        $albums = $query->result_array();
        
        foreach ($albums as $album):
        
            $this->db->from("tbl_gallery");
            $this->db->where("gallery_album", $album["album_id"]);
            
            $query = $this->db->get();
            
            $data[$album["album_name"]] = $query->result_array();

        endforeach;
        
        return $data;
    }

View
Code:
&lt;?php
    foreach ($data["gallery"] as $key => $index):
        echo heading($key, 3);
        echo '<table cellspacing="0" cellpadding="0" style="float:left;"><tr>';

        $count = 0;

        foreach ($index as $item):
            $img_attr = array(
                'src' => '/uploads/pictures/small/'.$item["gallery_picture"],
                'alt' => '',
                'style' => "margin-right:3px;"
            );
            $link_attr = array(
                'rel' => "shadowbox[".$item['gallery_album']."]",
                'title' => ""
            );
            
            $count++;
            
            print "<td>";
            print anchor('/uploads/pictures/large/'.$item["gallery_picture"], img($img_attr), $link_attr);
            print "</td>\n";
            
            if (($count % 4) == 0):
                echo "</tr><tr>";
                $count = 0;
            endif;

        endforeach;
        
        echo "</table>\n";
    endforeach;
?&gt;

Thank you for a good forum!




Theme © iAndrew 2016 - Forum software by © MyBB