#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2

    PHP gallery -- add info in an array?


    Hey guys,

    I'm working on a little gallery in PHP, nothing too extreme, just something simple and fun to get familiar with programming in PHP. For this I'm also getting about 6 hours a week private tutoring at work. However, I won't see the man who's helping me until next tuesday and I'd really like to finish this. Could any of you give me an example/explanation on how I could do the following?:

    In this array I've put the filenames of paintings that I want to show in the gallery. I would however like to add a little description too in the style of "the dog, made by, using acrylic paint etc", just a small single line of info. I'm having some difficulty thinking of how to do this and how to put it into practice. All help will be appreciated, however, I prefer examples so please don't provide me with what I want finished code! I'm trying to learn

    PHP Code:
    <?php
    include "includes/config.php";

    //hier alle afbeeldingen voor de gallerij op volgorde invoeren


    $info = array();
    $info[] = "bo";
    $info[] = "minoes";
    $info[] = "gitarist";
    $info[] = "blauwnaakt";
    $info[] = "natascha";
    $info[] = "poes";
    $info[] = "tulp";
    $info[] = "roos";
    $info[] = "rozen";

    //definieer de variabele aantal
    $aantal = (count($info)-1);


    if ( !isset(
    $_GET['img']) || ($_GET['img']<0) || ($_GET['img']> $aantal) || (!is_numeric($_GET['img'])) ){
        
    $_GET['img'] = 0;
    }

    //definieer de variabelen waarmee de verder en terug knop werken
    $next $_GET['img']+1;
    $prev $_GET['img']-1;

    if (
    $_GET['img'] >= $aantal){
        
    $verder " VERDER";
    }else{
        
    $verder "<a href=\"".$_SERVER['PHP_SELF']."?img=".$next."\"> VERDER</a>";
    }

    if (
    $_GET['img'] == 0){
        
    $terug "TERUG ";
    }else{
        
    $terug "<a href=\"".$_SERVER['PHP_SELF']."?img=".$prev."\">TERUG </a>";
    }

    //opbouw visuele pagina
    include "includes/begin.inc.php";

    $page .= "<div id=\"gallerij\">".$terug."<img src=\"img/".$info[$_GET['img']].".jpg\" alt=\"Sandra's Altelier: ".$info[$_GET['img']]."\" />".$verder."</div>\n";

    $page .= "<div id=\"terugnaarhome\"><a href=\"".$root."index.php\">Terug naar de homepage</a></div>\n";

    include 
    "includes/eind.inc.php";

    //einde opbouw visuele pagina

    echo $page;

    ?>
    Also, I learned to put all html into a PHP variable as you can see so that it'll load the entire script of the page in 1 time instead of pinging back and forth between scripts. Apperantly it increases stability on larger projects.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    heard of MULTI_DIMENSIONAL arrays? look that up! i am sure google will provide examples!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    thanks, I think this is what I'm looking for but I'm not sure how I could apply it to my situation. could you perhaps provide me with a relevant example?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    come on, did you really google and come up empty handed? if you say yes, i am sorry i will call you a liar. spend two minutes googling
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    these examples still don't provide me with a good way to make these changes without upsetting my code. mind you; this is my 4th week in PHP.

    I created the array as followed;

    PHP Code:
    $info = array(
        array(
    "bo""schilderij van een hond"),
        array(
    "minoes""schilderij van een kat"),
        array(
    "gitarist""schilderij van een gitarist"),
        array(
    "tulp""schilderij van een tulp"),
        array(
    "rozen""schilderij van rozen"),
    ); 
    But it creates a "Array to string conversion in gallerij3_multiarrays.php" conflict here

    PHP Code:
    $page .= "<div id=\"gallerij\">".$terug."<img src=\"img/".$info[$_GET['img']].".jpg\" alt=\"Sandra's Altelier: ".$info[$_GET['img']]."\" />".$verder."</div>\n"
    This is where I need help.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    need to see more of your code?

    PHP Code:
    $info = array();
    $info[] = array ( "bo""desc");
    $info[] = array ( "bor1""desc1");

    OR 

    $info = array();
    $info[] = array ( image=> "bo"title=>"desc");
    $info[] = array ( image=> "bo1"title=>"desc1"); 
    then you would:

    PHP Code:
    foreach ( $info as $array ){
       echo 
    "$array[0] $array[1]
    }

    OR 

    foreach ( 
    $info as $array ){
       echo "
    $array['image'$array['title']

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    well it'd be easy to do it that way but with the functions I have in my current scripts I don't think I could apply it that way.

    here's the full code, I haven't put the description part in it yet, only in the array, but I want it to work like this first, before I put the description part in it.
    PHP Code:
    <?php
    include "includes/config.php";

    $info = array(
        array(
    "bo""schilderij van een hond"),
        array(
    "minoes""schilderij van een kat"),
        array(
    "gitarist""schilderij van een gitarist"),
        array(
    "tulp""schilderij van een tulp"),
        array(
    "rozen""schilderij van rozen"),
    );

    //definieer de variabele aantal
    $aantal = (count($info)-1);


    if ( !isset(
    $_GET['img']) || ($_GET['img']<0) || ($_GET['img']> $aantal) || (!is_numeric($_GET['img'])) ){
        
    $_GET['img'] = 0;
    }

    //definieer de variabelen waarmee de verder en terug knop werken
    $next $_GET['img']+1;
    $prev $_GET['img']-1;

    if (
    $_GET['img'] >= $aantal){
        
    $verder " VERDER";
    }else{
        
    $verder "<a href=\"".$_SERVER['PHP_SELF']."?img=".$next."\"> VERDER</a>";
    }

    if (
    $_GET['img'] == 0){
        
    $terug "TERUG ";
    }else{
        
    $terug "<a href=\"".$_SERVER['PHP_SELF']."?img=".$prev."\">TERUG </a>";
    }

    //opbouw visuele pagina
    include "includes/begin.inc.php";

    $page .= "<div id=\"gallerij\">".$terug."<img src=\"img/".$info[$_GET['img']].".jpg\" alt=\"Sandra's Altelier: ".$info[$_GET['img']]."\" />".$verder."</div>\n";

    $page .= "<div id=\"terugnaarhome\"><a href=\"".$root."index.php\">Terug naar de homepage</a></div>\n";

    include 
    "includes/eind.inc.php";

    //einde opbouw visuele pagina

    echo $page;

    ?>
    same problem persists when I use this as the array
    PHP Code:
    $info = array(); 
    $info[] = array ( "bo""desc"); 
    $info[] = array ( "minoes""desc1"); 
    $info[] = array ( "gitarist""desc2"); 
    Notice: Array to string conversion in gallerij3_multiarrays.php on line 58

    line 58 is

    PHP Code:
    $page .= "<div id=\"gallerij\">".$terug."<img src=\"img/".$info[$_GET['img']].".jpg\" alt=\"Sandra's Altelier: ".$info[$_GET['img']]."\" />".$verder."</div>\n"
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    could still use some help.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    876
    Rep Power
    276
    would it work if you used an associative array instead of multi-dimensional?
    like this
    PHP Code:
     $info = array(
        
    'bo' => 'schilderij van een hond',
        
    'minoes' => 'schilderij van een kat',
        
    'gitarist' => 'schilderij van een gitarist',
        
    'tulp' => 'schilderij van een tulp',
        
    'rozen' => 'schilderij van rozen'
    ); 
    then to add to the array, (lets say to add 'poes') it would be in the form of
    PHP Code:
    $info['poes'] = 'description for poes'
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    not sure how functions would "complicate" things? yes you may need to change the functions since you are added a description but..

    in the post you coded, you dont seem to be using functions anywhere?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    well guys, I solved it with some help, so here's the final code that I used;

    PHP Code:
    <?php
    include "includes/config.php";

    //hier alle afbeeldingen voor de gallerij op volgorde invoeren
     
    $info = array(); 
    function 
    maakSchilderij($titel$omschr){
        GLOBAL 
    $info;
        
    $info[] = array('naam'=>$titel,'omschr'=>$omschr);
    }
     
    maakSchilderij("bo""schilderij van een hond");
    maakSchilderij("minoes""schilderij van een kat");
    maakSchilderij("gitarist""schilderij van een gitarist");
    maakSchilderij("tulp""schilderij van een tulp"); 
    maakSchilderij("rozen""schilderij van rozen"); 

    //definieer de variabele aantal
    $aantal = (count($info)-1);


    if ( !isset(
    $_GET['img']) || ($_GET['img']<0) || ($_GET['img']> $aantal) || (!is_numeric($_GET['img'])) ){
        
    $_GET['img'] = 0;
    }

    //definieer de variabelen waarmee de verder en terug knop werken
    $next $_GET['img']+1;
    $prev $_GET['img']-1;

    if (
    $_GET['img'] >= $aantal){
        
    $verder " &raquo;";
    }else{
        
    $verder "<a href=\"".$_SERVER['PHP_SELF']."?img=".$next."\"> &raquo;</a>";
    }

    if (
    $_GET['img'] == 0){
        
    $terug "&laquo; ";
    }else{
        
    $terug "<a href=\"".$_SERVER['PHP_SELF']."?img=".$prev."\">&laquo; </a>";
    }

    //opbouw visuele pagina
    include "includes/begin.inc.php";

    $page .= "<div id=\"gallerij\">".$terug."<img src=\"img/".$info[$_GET['img']]['naam'].".jpg\" alt=\"Sandra's Altelier: ".$info[$_GET['img']]['naam']."\" />".$verder."</div>\n";

    $page .= "<div id=\"infobox\">".$info[$_GET['img']]['omschr']."</div>\n";

    include 
    "includes/eind.inc.php";

    //einde opbouw visuele pagina

    echo $page;

    ?>

IMN logo majestic logo threadwatch logo seochat tools logo