Problems outputting to a 3 column table in order to crate an image gallery

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ste

    Problems outputting to a 3 column table in order to crate an image gallery

    Hi there,

    I'm trying to query a MySQL database (containing image data) and to output
    the results in a HTML table of 3 columns wide (and however many rows it
    takes) in order to create a basic thumbnail gallery.

    I can query and output the data as a single column table, but I'm having
    problems filling up a 3 column table (with different images, looping
    continuously until the end).

    I would appreciate it if someone could take a look at the code below and
    point me in the right direction or suggest any extra lines of code which
    would finish the job?

    The code below puts an image on each row, instead of filling up 3 columns on
    each row with a different image.

    I open this gallery by going to
    mywebsite.com/gallery.php?ima getype=landscap e (i.e. to access the landscape
    gallery).

    Thank you for any help,

    Ste


    <?php
    include("my_db_ login.inc");

    $connection = mysql_connect($ host,$user,$pas sword) or die ("couldn't connect
    to server");
    $db = mysql_select_db ($database,$con nection) or die ("Couldn't select
    database");

    $title = "Gallery";
    $query = "SELECT * FROM mydatabase WHERE imagetype =
    \"{$_GET['imagetype']}\"";
    $result = mysql_query($qu ery) or die ("Couldn't execute query.");

    echo "<h2>$title </h2>";
    echo "<table>";
    echo "<tbody>";

    while ($row = mysql_fetch_arr ay($result))

    {
    extract($row);
    echo "<tr>";
    echo "<td><img src=\"$imageloc ation\" />\n
    </td>\n";
    }

    echo "</tr>";
    echo "</table>\n";
    ?>


  • Rik

    #2
    Re: Problems outputting to a 3 column table in order to crate an image gallery

    Why don't I see the bumber three anywhere in the code? Have you tried making
    it 3 rows? How?


    But I'm in a generous mood tonight:
    echo "\n<table>" ;
    $i = 3;
    while ($row = mysql_fetch_ass oc($result))
    {
    if($i==3) echo "\n\t<tr>";
    echo "\n\t\t<td> <img src=\".$row['imagelocation']." /></td>";
    $i--;
    if($i==0) {
    echo "\n\t<tr>";
    $i = 3;
    }
    }
    if($i!=3) echo "\n\t\t<td colspan=\"$i\"> </td>\n\t</tr>";
    echo "\n</table>";

    Grtz,
    --
    Rik Wasmus


    Comment

    • ste

      #3
      Re: Problems outputting to a 3 column table in order to crate an image gallery


      "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
      news:e2ukl5$gft $1@netlx020.civ .utwente.nl...[color=blue]
      > Why don't I see the bumber three anywhere in the code? Have you tried
      > making
      > it 3 rows? How?
      >
      >
      > But I'm in a generous mood tonight:
      > echo "\n<table>" ;
      > $i = 3;
      > while ($row = mysql_fetch_ass oc($result))
      > {
      > if($i==3) echo "\n\t<tr>";
      > echo "\n\t\t<td> <img src=\".$row['imagelocation']." /></td>";
      > $i--;
      > if($i==0) {
      > echo "\n\t<tr>";
      > $i = 3;
      > }
      > }
      > if($i!=3) echo "\n\t\t<td colspan=\"$i\"> </td>\n\t</tr>";
      > echo "\n</table>";
      >
      > Grtz,
      > --
      > Rik Wasmus[/color]

      Hi Rik,

      I had two main outcomes when trying to write the code for this - the one you
      saw in my post (which posted 1 image per table row), and another one which
      did post 3 images per row, but each row contain identical images (i.e. it
      was just duplicating code, as opposed to inserting the next record/image).
      As I've not yet grasped enough skills with PHP yet, and in my frustration of
      nearly seeing what I was hoping for, I completely overlooked the fact that
      the number 3 should be in my code at some point! :-S

      Thank you for being in such a generous mood and posting the above code, it's
      appreciated! :-)

      When I first ran it, I had a message saying "Parse error: parse error,
      unexpected T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or T_VARIABLE or
      T_NUM_STRING" on the line containing the code 'imagelocation' above.

      I googled this message and found the solution was to remove the apostrophe's
      surrounding ['imagelocation'], and this now works great! I also inserted a
      \ in front of the quotation mark after imagelocation, but I'm not sure if
      this made any difference at all.

      Thanks again, I'd have *never* have gotten there with this assistance.

      Ste


      Comment

      • Rik

        #4
        Re: Problems outputting to a 3 column table in order to crate an image gallery

        ste wrote:[color=blue][color=green]
        >> echo "\n\t\t<td> <img src=\".$row['imagelocation']." /></td>";[/color]
        > When I first ran it, I had a message saying "Parse error: parse error,
        > unexpected T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or
        > T_VARIABLE or T_NUM_STRING" on the line containing the code
        > 'imagelocation' above.[/color]


        It should offcourse be:
        echo "\n\t\t<td> <img src=\"".$row['imagelocation']."\" /></td>";

        --
        Rik Wasmus


        Comment

        • ste

          #5
          Re: Problems outputting to a 3 column table in order to crate an image gallery


          "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
          news:e3134t$3bg $1@netlx020.civ .utwente.nl...[color=blue]
          > ste wrote:[color=green][color=darkred]
          >>> echo "\n\t\t<td> <img src=\".$row['imagelocation']." /></td>";[/color]
          >> When I first ran it, I had a message saying "Parse error: parse error,
          >> unexpected T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or
          >> T_VARIABLE or T_NUM_STRING" on the line containing the code
          >> 'imagelocation' above.[/color]
          >
          >
          > It should offcourse be:
          > echo "\n\t\t<td> <img src=\"".$row['imagelocation']."\" /></td>";
          >
          > --
          > Rik Wasmus[/color]


          Thanks Rik, I've now updated it.

          Now when I open this page: mywebsite.com/gallery.php?ima getype=landscap e
          it opens all the images from my database which are tagged with the category
          of landscape.

          Likewise, going to the url: mywebsite.com/gallery.php?ima getype=portrait
          it opens all the images from my database which are tagged with the category
          of portrait.

          Is there a way to open the gallery showing ALL pictures, without having to
          re-write the sql query? i.e. is there a tag I can enter in a URL which
          displays images where the imagetype could literally be anything?
          mywebsite.com/gallery.php?ima getype=anything

          If there is such a term, I haven't been able to find it, but thought I'd ask
          anyway in case you knew otherwise.

          Thanks,

          Ste


          Comment

          • Rik

            #6
            Re: Problems outputting to a 3 column table in order to crate an image gallery

            ste wrote:[color=blue]
            > Likewise, going to the url:
            > mywebsite.com/gallery.php?ima getype=portrait
            > it opens all the images from my database which are tagged with the
            > category of portrait.
            >
            > Is there a way to open the gallery showing ALL pictures, without
            > having to re-write the sql query? i.e. is there a tag I can enter in
            > a URL which displays images where the imagetype could literally be
            > anything? mywebsite.com/gallery.php?ima getype=anything[/color]


            just don't set the imagetype, so the url would be mywebsite.com/gallery.php,
            and use this code:

            //create database $connection

            if(get_magic_qu otes_gpc()){
            $_GET['imagetype'] = stripslashes($_ GET['imagetype']);
            }
            $imagetype = mysql_real_esca pe_string( $_GET['imagetype'], $connection);
            $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
            '".$_GET['imagetype']."'" : '';
            $query = "SELECT * FROM mydatabase ".$where;

            Grtz,
            --
            Rik Wasmus


            Comment

            • Rik

              #7
              Re: Problems outputting to a 3 column table in order to crate an image gallery

              Rik wrote:[color=blue]
              > $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =[/color]
              '".$_GET['imagetype']."'" : '';

              Should be:
              $where = (isset($_GET['imagetype'])) ? "WHERE imagetype = '".$imagetype." '"
              : '';
              --
              Rik Wasmus


              Comment

              • ste

                #8
                Re: Problems outputting to a 3 column table in order to crate an image gallery


                "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
                news:e329sc$r4j $1@netlx020.civ .utwente.nl...[color=blue]
                > Rik wrote:[color=green]
                >> $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =[/color]
                > '".$_GET['imagetype']."'" : '';
                >
                > Should be:
                > $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
                > '".$imagetype." '"
                > : '';
                > --
                > Rik Wasmus[/color]


                Thanks for this Rik. If I'm understanding this correctly, adding the above
                code to the existing gallery script will allow all images in the database to
                be displayed when a user opens the gallery.php (as it was currently
                displaying a blank page), or, it can still be used to open specific
                galleries when it is used with the extension of
                gallery.php?ima getype=landscap e

                I have added your code to the existing code, and also removed the previous
                line of code which contained the old $query line.

                Unfortunately (and I've probably done something wrong here), the code below
                (which is how I've incorporated your latest piece of code into the existing
                code) doesn't quite do this - when I open gallery.php, it's not opening all
                the images, but is instead opening all images which have a blank value for
                imagetype (which would normally be none, but I've just added an image to the
                database to test, which is why I know this is what's happening). When I
                open gallery.php?ima getype=landscap e, this works as it did previously so no
                problems here. Have I added it correctly?

                <?php
                include("my_db_ login.inc");

                $connection = mysql_connect($ host,$user,$pas sword) or die ("couldn't connect
                to server");
                $db = mysql_select_db ($database,$con nection) or die ("Couldn't select
                database");

                if(get_magic_qu otes_gpc()){
                $_GET['imagetype'] = stripslashes($_ GET['imagetype']);
                }

                $imagetype = mysql_real_esca pe_string( $_GET['imagetype'], $connection);
                $where = (isset($_GET['imagetype'])) ? "WHERE imagetype = '".$imagetype." '":
                '';
                $query = "SELECT * FROM mydatabase ".$where;
                $result = mysql_query($qu ery) or die ("Couldn't execute query.");

                echo "\n<div id=\"content\"> ";
                echo "\n<h2>Samp le Gallery</h2>";
                echo "\n<table id=\"thumbgalle ry\" cellSpacing=\"0 \" cellPadding=\"1 0\"
                width=\"400\" border=\"0\">";
                echo "\n<tbody>" ;
                $i = 3;
                while ($row = mysql_fetch_ass oc($result))
                {
                if($i==3) echo "\n\t<tr>";
                echo "\n\t\t<td valign=\"top\" width=\"113\" id=\"thumbs\">< img
                src=\"".$row['imagelocation']."\" width=\"113\" border=\"0\" /><p
                class=\"caption ref\">Ref: ".$row['imageid']."</p><p
                class=\"caption \">".$row['imagecaption']."</p></td>";
                $i--;
                if($i==0) {
                echo "\n\t<tr>";
                $i = 3;
                }
                }
                if($i!=3) echo "\n\t\t<td colspan=\"$i\"> </td>\n\t</tr>";
                echo "\n</tbody>";
                echo "\n</table>";

                ?>


                Thanks,

                Ste


                Comment

                • Rik

                  #9
                  Re: Problems outputting to a 3 column table in order to crate an image gallery

                  The only thing I can think of is that the following code sets
                  $_GET['imagetype'], allthough it didn't previsously exist.

                  if(get_magic_qu otes_gpc()){
                  $_GET['imagetype'] = stripslashes($_ GET['imagetype']);
                  }

                  Try echoing your query, is should be "SELECT * FROM mydatabase" and nothing
                  more.
                  Else try:

                  $where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
                  imagetype = '".$imagetype." '":'';

                  Grtz,
                  --
                  Rik Wasmus


                  Comment

                  • ste

                    #10
                    Re: Problems outputting to a 3 column table in order to crate an image gallery


                    "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
                    news:e33hpl$o64 $1@netlx020.civ .utwente.nl...[color=blue]
                    > The only thing I can think of is that the following code sets
                    > $_GET['imagetype'], allthough it didn't previsously exist.
                    >
                    > if(get_magic_qu otes_gpc()){
                    > $_GET['imagetype'] = stripslashes($_ GET['imagetype']);
                    > }
                    >
                    > Try echoing your query, is should be "SELECT * FROM mydatabase" and
                    > nothing
                    > more.
                    > Else try:
                    >
                    > $where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
                    > imagetype = '".$imagetype." '":'';
                    >
                    > Grtz,
                    > --
                    > Rik Wasmus[/color]


                    Hi Rik,

                    I replaced the existing $where line with the one above as per your
                    suggestion, and this now works perfectly! :-)

                    Old: $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
                    '".$imagetype." '": '';
                    New: $where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
                    imagetype = '".$imagetype." '":'';

                    I wouldn't have a clue why one works and one doesn't (well, they both worked
                    of course, but 1st one returned only blank fields, and the 2nd one returned
                    all fields which is what I wanted), but I've got a few books to work through
                    now so I'll revisit this post in a few months (if I'm lucky!) and let you
                    know! :-)

                    Thanks for all your help Rik, it's appreciated.

                    Ste


                    Comment

                    • Rik

                      #11
                      Re: Problems outputting to a 3 column table in order to crate an image gallery

                      ste wrote:[color=blue][color=green]
                      >> The only thing I can think of is that the following code sets
                      >> $_GET['imagetype'], allthough it didn't previsously exist.
                      >>
                      >> if(get_magic_qu otes_gpc()){
                      >> $_GET['imagetype'] = stripslashes($_ GET['imagetype']);
                      >> }[/color]
                      > I replaced the existing $where line with the one above as per your
                      > suggestion, and this now works perfectly! :-)
                      > I wouldn't have a clue why one works and one doesn't[/color]

                      Like I said above, $_GET['imagetype'] gets set by the code, even if it
                      doesn't exist, as an empty string.

                      Grtz,
                      --
                      Rik Wasmus


                      Comment

                      Working...