hello everyone, new to the forums here and pretty much with php also. What i have done is create forms to upload images into my database. what i need to do is get my images viewable by the user. if i have 10 images, how can i set up a page that will show the first image in my database by id number and then when the person clicks the next button that i have on the page it will display the next image by id number in the place of the first image. so lets say it shows image id#1 then the person clicks next i want it to show image id#2. if the person clicks back it displays image id#1 again. if the user clicks all the way to image #10 and there are no more images it goes back to image #1. if anyone has any idea how to write something like this i would greatly appreciate it. thanks in advance.

-matty p

    perfectly straightforward stuff.

    you need to create a script which takes an argument identifying the image to display so that you can request mething like:

    www.yourdomain.com/showimage.php?id=5

    The above would query the database for image number 5. It would also check how many images are in the database so that it can work out what number to drop into the "previous" and "next" links.

    Hopefully, the description above should help you see how you might go about starting this piece of work. You might also want to think about making it more customisable so that your users can choose how many images they want to see on each page etc etc.

    I'd be very surprised if anyone here will post the code for you to copy and paste here. For starters, they'd need to know the details of your current set up, database connections etc.

    If you get stuck into this and are having problems getting it working, post the problem snippets of code and you'll probably find that someone will help you tease out the problem.

    On the other hand, if you don't have the time/inclination to learn this, and would just like someone to write it for you, you could well find that someone here would be interested in doing it for you offline. Don't be surprised if they expect something in return for the effort, though.

    Personally, I'd be happy to do it for you. If you're interested, contact me at the address below and we can discuss terms 🙂

    breceennia@kriocoucke.mailexpire.com

    (the above alias is good for 3 months, or until I decide to kill it. Check out www.mailexpire.com for details)

      thanks for the help js, im not really sure where to start but i would like to do this project on my own, i just dont know how to, you know that feeling? like if i could go to a php store and tell the guy im building a php script and he could be like well you need this and blah blah, it sucks i wish i knew how to do it but im learning everyday, thanks again.

        HI matty,

        Do you store the actual image in the database, or just the name of the image file?

        If you just store the image name, it's a fairly trivial task. If you store the image itself, it's slightly less trivial, but only slightly.

          i do actually have all the images stored in the database

          <?php
          $conn = mysql_connect("my server", "my database", "my password") OR DIE (mysql_error());
          $sql = "SELECT * FROM image WHERE image_id=$iid";
          $iid = $_GET["iid"];
          $result = mysql_query ($sql, $conn);

          if (mysql_num_rows ($result)>0) {

          print "<img src=\"image_script.php?iid=$iid\">";
          $iid++;
          print "<a href=\"image_script.php?iid=$iid>\"Next</a>";
          
          }

          php?>

          coming up with this error:

          Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/i/n/v/invisiblestuds/html/image_script.php on line 19

          any suggestions?

            hmm... where to start?

            First off the sequence of commands at the start of your script looks wrong:

             $sql = "SELECT * FROM image WHERE image_id=$iid";
            $iid = $_GET["iid"]; 
            

            You're only assigning a value to $iid after you construct the sql query. This means that the query you send to sql is just SELECT * FROM image WHERE image_id=

            Of course that's not a complete statement, so it won't execute, which is why $result doesn't contain a valid result resource afterwards.

            The next problem I see is that, assuming you correct the above, you're doing nothing with the data you return from the query. You just continue to use $iid which by now you have set equal to one of the GET variables:

             print "<img src=\"image_script.php?iid=$iid\">";
            $iid++;
            print "<a href=\"image_script.php?iid=$iid>\"Next</a>"; 
            

            From what I can see, you need two separate scripts here:

            show_image.php
            This script would take an argument of $id, search the database for that image and display it. It could be that simple. The main thing here is that this just returns a gif/jpg - no html at all. (You could enhance it a little if you like so that for invalid values of $id, it would default to a "no such image" graphic. You could also enhance this so that it would only respond to queries where the referrer was one of your pages.)

            show_image_page.php
            This script would take an argument of $id, search the database for that image, and identify what the id of the "previous" and "next" images are. It would then build this page to contain "previous" and "next" links to itself with the apprpriate values for $id, and it would also call show_image.php in an <img> tag with the current value for $id to actually display the image somewhere on the page.

            Does this help clarify at all?

            J

              i appreciate the help js, your are being very helpful. it's a little fuzzy to me, sorry for my lack of knowledge in the subject, im still new to php but what's an argumemt. thanks for the help again, i do appreciate it.

                ok so heres what i have so far

                <?php

                $conn = mysql_connect("server", "db", "pass") OR DIE (mysql_error());
                @mysql_select_db ("idatabase", $conn) OR DIE (mysql_error());
                $sql="SELECT image_id FROM image";
                $result = mysql_query($sql, $conn);

                while($query_data = mysql_fetch_row($result))
                {
                	print $query_data[];
                	$iid = $query_data["image_id"];
                	  print "<img src=\"test.php?id=$iid\">";
                }

                ?>

                ok so what this is doing is displaying a page and showing the image id numbers like this 1234567 and so on but in between these numbers there are pictures and they are not working. i cant get the pictures to show up. im getting closer and closer. before i couldnt get anything to show up. any ideas?

                  Write a Reply...