Problems Creating the Code to Open Images Within a Template PHP Page

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

    #1

    Problems Creating the Code to Open Images Within a Template PHP Page

    Hi there,

    I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I
    wondered if someone could help me out with a problem I'm having, or at least
    point me in the right direction? I have setup a MySQL database which
    contains the following, though I would like to expand on this in the future
    with lots of extra fields:

    IMAGES TABLE:
    -------------------
    imageid (primary key)
    imagelocation (url location for image)
    imagecaption (caption for image, not used in the code below, but will be
    used once I suss this out!)

    What I want to do is, from a HTML gallery of thumbnails, be able to open a
    larger version of each thumbnail image in a nice pretty formatted HTML page.
    Each HTML page would be identical, so that's why I only want to create this
    once, as opposed to hard coding them all. As I want to keep this simple and
    one step at a time, I'm prepared to create the image gallery and the
    appropriate image URL's.

    My problem is that I'm unsure of the php/mysql I need to write in order to
    open the appropriate picture in the image template. For example, if I hover
    over and click 'image 1,' I would like the browser to open the page
    www.mywebsite.com/imagetemplate.php?id=1 This would open the image template
    page with image 1 visible within it.

    So when executing the sql query which selects a particular image from the
    database, I would like it to find the record which has an ID equal to the ID
    in the URL above (in this case, 1).

    Now what I've tried isn't working as I've got it all wrong, but here it is
    for interest:

    DODGY CODE:
    ------------------

    <?php

    /* this is the include file for my database passwords */
    include("my_db_ login.inc");

    /* this is the code to make the connection to the database */
    $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");

    /* this query SHOULD be requesting all records from My Database where the
    ImageID is equal to the ImageID listed in the URL as mentioned above */
    $query = "SELECT * FROM my_database WHERE imageid =
    \"{$_POST['imageid']}\"";
    $result = mysql_query($qu ery) or die ("Couldn't execute query.");


    /* the line of code below is actually the code from the table cell which
    SHOULD insert the image location url for the image where the id is equal to
    the one requested in the url above */
    echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";

    ?>

    I'd be grateful for any help with this as I'm obviously doing something (or
    many things!) wrong.

    Thanks,

    Ste


  • Jerry Stuckle

    #2
    Re: Problems Creating the Code to Open Images Within a Template PHPPage

    ste wrote:[color=blue]
    > Hi there,
    >
    > I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I
    > wondered if someone could help me out with a problem I'm having, or at least
    > point me in the right direction? I have setup a MySQL database which
    > contains the following, though I would like to expand on this in the future
    > with lots of extra fields:
    >
    > IMAGES TABLE:
    > -------------------
    > imageid (primary key)
    > imagelocation (url location for image)
    > imagecaption (caption for image, not used in the code below, but will be
    > used once I suss this out!)
    >
    > What I want to do is, from a HTML gallery of thumbnails, be able to open a
    > larger version of each thumbnail image in a nice pretty formatted HTML page.
    > Each HTML page would be identical, so that's why I only want to create this
    > once, as opposed to hard coding them all. As I want to keep this simple and
    > one step at a time, I'm prepared to create the image gallery and the
    > appropriate image URL's.
    >
    > My problem is that I'm unsure of the php/mysql I need to write in order to
    > open the appropriate picture in the image template. For example, if I hover
    > over and click 'image 1,' I would like the browser to open the page
    > www.mywebsite.com/imagetemplate.php?id=1 This would open the image template
    > page with image 1 visible within it.
    >
    > So when executing the sql query which selects a particular image from the
    > database, I would like it to find the record which has an ID equal to the ID
    > in the URL above (in this case, 1).
    >
    > Now what I've tried isn't working as I've got it all wrong, but here it is
    > for interest:
    >
    > DODGY CODE:
    > ------------------
    >
    > <?php
    >
    > /* this is the include file for my database passwords */
    > include("my_db_ login.inc");
    >
    > /* this is the code to make the connection to the database */
    > $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");
    >
    > /* this query SHOULD be requesting all records from My Database where the
    > ImageID is equal to the ImageID listed in the URL as mentioned above */
    > $query = "SELECT * FROM my_database WHERE imageid =
    > \"{$_POST['imageid']}\"";
    > $result = mysql_query($qu ery) or die ("Couldn't execute query.");
    >
    >
    > /* the line of code below is actually the code from the table cell which
    > SHOULD insert the image location url for the image where the id is equal to
    > the one requested in the url above */
    > echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
    >
    > ?>
    >
    > I'd be grateful for any help with this as I'm obviously doing something (or
    > many things!) wrong.
    >
    > Thanks,
    >
    > Ste
    >
    >[/color]

    You're close. mysql_query() returns a result object; you need to retrieve the
    actual data (into $row in your example).

    After the mysql_query() add the following:

    $row = mysql_fetch_arr ay($result);


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • ste

      #3
      Re: Problems Creating the Code to Open Images Within a Template PHP Page


      "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
      news:G5udneifg5 pfZ8zZnZ2dnUVZ_ tGdnZ2d@comcast .com...
      <snip>[color=blue]
      > You're close. mysql_query() returns a result object; you need to retrieve
      > the actual data (into $row in your example).
      >
      > After the mysql_query() add the following:
      >
      > $row = mysql_fetch_arr ay($result);
      >
      >
      > --
      > =============== ===
      > Remove the "x" from my email address
      > Jerry Stuckle
      > JDS Computer Training Corp.
      > jstucklex@attgl obal.net
      > =============== ===[/color]

      Hi Jerry,

      Thanks for getting back to me - I'm close? I'm astonished as I thought I
      would be way out!

      I *think* I've put your line of code in the right place, but this still
      doesn't work I'm afraid. When the page opens, there's no image - if I look
      at the HTML, it is basically returning <img src=""> instead of <img
      src="images/image1/jpg">. Perhaps the query is right but my code to insert
      the field name (which contains the image URL, it's called 'imagelocation' )
      isn't?

      Here's the code again with the extra line - did I put it in the right place?

      <?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");

      $query = "SELECT * FROM my_database WHERE imageid =
      \"{$_POST['imageid']}\"";
      $result = mysql_query($qu ery) or die ("Couldn't execute query.");
      $row = mysql_fetch_arr ay($result);

      echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
      ?>

      Thanks,

      Ste


      Comment

      • Jerry Stuckle

        #4
        Re: Problems Creating the Code to Open Images Within a Template PHPPage

        ste wrote:[color=blue]
        > "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
        > news:G5udneifg5 pfZ8zZnZ2dnUVZ_ tGdnZ2d@comcast .com...
        > <snip>
        >[color=green]
        >>You're close. mysql_query() returns a result object; you need to retrieve
        >>the actual data (into $row in your example).
        >>
        >>After the mysql_query() add the following:
        >>
        >> $row = mysql_fetch_arr ay($result);
        >>
        >>
        >>--
        >>============= =====
        >>Remove the "x" from my email address
        >>Jerry Stuckle
        >>JDS Computer Training Corp.
        >>jstucklex@att global.net
        >>============= =====[/color]
        >
        >
        > Hi Jerry,
        >
        > Thanks for getting back to me - I'm close? I'm astonished as I thought I
        > would be way out!
        >
        > I *think* I've put your line of code in the right place, but this still
        > doesn't work I'm afraid. When the page opens, there's no image - if I look
        > at the HTML, it is basically returning <img src=""> instead of <img
        > src="images/image1/jpg">. Perhaps the query is right but my code to insert
        > the field name (which contains the image URL, it's called 'imagelocation' )
        > isn't?
        >
        > Here's the code again with the extra line - did I put it in the right place?
        >
        > <?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");
        >
        > $query = "SELECT * FROM my_database WHERE imageid =
        > \"{$_POST['imageid']}\"";
        > $result = mysql_query($qu ery) or die ("Couldn't execute query.");
        > $row = mysql_fetch_arr ay($result);
        >
        > echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
        > ?>
        >
        > Thanks,
        >
        > Ste
        >
        >[/color]

        Ste,

        I have no idea if it is 'imagelocation' or not. It's the name of the MySQL
        column containing the information you want.

        What happens if you do:

        echo "<pre>\n";
        print_r($row);
        echo "</pre>\n";

        This will give you the contents (including keys) of $row.


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • ste

          #5
          Re: Problems Creating the Code to Open Images Within a Template PHP Page


          "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
          news:rpSdnaLkQp sIpM_ZRVn-rw@comcast.com. ..
          <snip>[color=blue]
          > Ste,
          >
          > I have no idea if it is 'imagelocation' or not. It's the name of the
          > MySQL column containing the information you want.
          >
          > What happens if you do:
          >
          > echo "<pre>\n";
          > print_r($row);
          > echo "</pre>\n";
          >
          > This will give you the contents (including keys) of $row.
          >
          >
          > --
          > =============== ===
          > Remove the "x" from my email address
          > Jerry Stuckle
          > JDS Computer Training Corp.
          > jstucklex@attgl obal.net
          > =============== ===[/color]


          Hi Jerry,

          There is a field called imagelocation in my database, so this is okay.

          I entered your code above, and like the other piece of code I had, it
          displayed nothing, besides some HTML tags <pre></pre>.

          This made me examine the actual query again and made me wonder if anything
          was being passed from the URL at all. I've got a PHP and MySQL book dor
          dummies (I couldn't find myself a more basic one, so had to settle for this!
          :-)), so out of interest and by chance, I looked up the $_POST command. In
          the same chapter, I came across the $_GET command too, and although I still
          don't know what they mean, the following sentence rang out to me: 'Contains
          all the variables passed from a previous page as part of the URL.' Of
          course, that still means nothing, except for the bit about the URL.

          So I changed $_POST to $_GET, and it worked! I'm amazed, but I wouldn't
          have even got this far without your help on this, so thank you. I'm working
          by trial and error it has to be said, and I've lost count of the number of
          times I've uploaded and tested php pages with my FTP browser recently! :-)

          For info, the following piece of code is the code that works:

          <?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");

          $query = "SELECT * FROM my_database WHERE imageid = \"{$_GET['imageid']}\"";
          $result = mysql_query($qu ery) or die ("Couldn't execute query.");
          $row = mysql_fetch_arr ay($result);

          echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
          ?>

          I've even tested it by adding in other fields from the database, and that
          works great too!

          If you have any suggestions on how any of the above can be tidied up, please
          don't hesitate to let me know, though it works so that's the main thing.

          Finally, do let me know if you know of any good onlie php/mysql tutorials
          for creating image galleries and the like.

          Thanks again,

          Ste


          Comment

          • Rik

            #6
            Re: Problems Creating the Code to Open Images Within a Template PHP Page

            ste wrote:[color=blue]
            > I
            > looked up the $_POST command. In the same chapter, I came across the
            > $_GET command too, and although I still don't know what they mean,
            > the following sentence rang out to me: 'Contains all the variables
            > passed from a previous page as part of the URL.' Of course, that
            > still means nothing, except for the bit about the URL.[/color]

            A bit simplefied:
            $_GET contains values that are set in the url:
            index.php?page= something
            then $_GET['page'] is "something"
            $_POST contains posted variables, usually by a form, that aren't in the url.

            Have fun coding,
            --
            Rik Wasmus


            Comment

            • ste

              #7
              Re: Problems Creating the Code to Open Images Within a Template PHP Page


              "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
              news:e2tol4$fdq $1@netlx020.civ .utwente.nl...[color=blue]
              > ste wrote:[color=green]
              >> I
              >> looked up the $_POST command. In the same chapter, I came across the
              >> $_GET command too, and although I still don't know what they mean,
              >> the following sentence rang out to me: 'Contains all the variables
              >> passed from a previous page as part of the URL.' Of course, that
              >> still means nothing, except for the bit about the URL.[/color]
              >
              > A bit simplefied:
              > $_GET contains values that are set in the url:
              > index.php?page= something
              > then $_GET['page'] is "something"
              > $_POST contains posted variables, usually by a form, that aren't in the
              > url.
              >
              > Have fun coding,
              > --
              > Rik Wasmus[/color]

              Thanks for that Rik, much clearer - you should write books for dummies! :-)

              Ste


              Comment

              Working...