• I’ve been using the following code to create a list of the photosets on my website. I’ll be integreating this into a WordPress page when my new site design’s finished, but in the mean time I was wondering whether anyone could give me a hand with some little bits of PHP.

    <?php
    $Photoset = array(
    “Party 1” => “Smarch 2003”,
    “Party 2” => “Smarch 2004”,
    );

    foreach ($Photoset as $Party => $Date){
    ?>

    <div class=”widetopbox”></div>
    <div class=”textbox”>
    <img src=”<?php bloginfo(‘stylesheet_directory’); ?>/sunthb.jpg” alt=”image” class=”alignleft” />
    <div class=”setinfo”><h3><? echo $Party; ?></h3><br/>
    <? echo $Date; ?>
    </div>
    <div class=”newset”></div>
    </div>
    <div class=”widebottombox”></div>

    <?
    ;
    }

    ?>

    All of the “widebox” stuff is just to do with the layout on my page.

    At the moment the code echos that all down the page as many times are there are rows in the array. It also changes the $Party and $Date variables according to what’s in that row of the array.

    My problem is that I’d like a 3rd variable. I can’t see any way of doing this with the current code, since I can only have 2 variables in the array. If anyone could tell me a way to do this using arrays or, in fact, any other method, I’d be really grateful.

Viewing 6 replies - 1 through 6 (of 6 total)
  • In the array, just store objects of photo. Here’s how you initialize the the object, I believe.

    $photo = new object();
    $photo->title = "title";
    $photo->date = "date";
    $photo->desc = "description"
    $album[] = $photo;

    or with arrays

    $photo[ "title" ] = "title";
    $photo[ "date" ] = "date";
    $photo[ "desc" ] = "desc";
    $album[] = $photo;

    For the second method, the shorthand (initialiazing without a loop) would be the following.

    $album = array (
    array(
    "title" => "doggie",
    "date" => "christmas eve",
    "desc" => "from my boy"
    ),
    array(
    "title" => "cutie",
    "date" => "first date",
    "desc" => "one of those moments"
    )
    );

    For the first method I believe you have to actually define a class Photo to be able to do something like this.
    $album = array(
    new Photo( "dogie", "christmas eve", "from my boy" ),
    new Photo( "cutie", "first date", "one of those moments" )
    );

    So if you interested with some Object Oriented programming, I'll tell you how to define class Photo; otherwise use the array method.

    Thread Starter greymullet

    (@greymullet)

    *cradles head*

    Forgive me if I’m even denser than usual, I’m stupidly tired. Plus I know very, very little PHP.

    You’ve got an array within an array in the second method shorthand, yes? I’m just clarifying…. if I’m wrong then I’m really not keeping track at all.

    Is there a way to use the second method with a loop?

    Also, is the first method you outlined Object Oriented? If so then I’d love to hear about it, that looks, to me, like a good way of doing it…

    Is there a way to use the second method with a loop?
    To initialize or to access it?
    If to initialize it, from where do you get the values to be assigned?
    To access it would be something like the following.

    // fills up the album with photos; fillAlbum has to be defined
    $album = fillAlbum();

    // access the photos in album
    foreach( $album as $photo ) {
    echo "$photo[ 'title' ], $photo[ 'date' ], $photo[ 'desc' ]";
    }

    Stay tune for some object oriented lesson…

    Okay for the object oriented method, you will define the class Photo. It the best method to put the definition in a separate file, say, photo.php. So, here is the content of photo.php

    <?php
    class Photo {
    var $title;
    var $date;
    var $desc;

    function Photo( $title, $date, $desc ) {
    $this->title = $title;
    $this->date = $date;
    $this->desc = $desc;
    }
    }
    ?>

    And then, wherever you want to instantiate an object of Photo, just include the file photo.php. The following is another file, my-album.php

    <?php
    require_once( "photo.php" );

    $album = array (
    new Photo( "dogie", "christmas eve", "from my boy" ),
    new Photo( "cutie", "first date", "one of those moments" )
    );

    foreach( $album as $photo ) {
    echo "$photo->title, $photo->date, $photo->desc<br />";
    }
    ?>

    Thread Starter greymullet

    (@greymullet)

    Sorry that I didn’t get back to you sooner, I’ve had exams and such. Still do in fact.

    I implemented your object-oriented code, it works brilliantly ?? Thanks very much. I’ll link to it as soon as the page’s up.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PHP Arrays’ is closed to new replies.