0

I have a while loop that goes through a table and I echo the results within it, I also have a while loop that looks at a image directory and I can output the paths to images. My problem is I want to output the image path in the other while loop where my html and image tag reside. I have tried putting one while loop inside the other, but one of the results for the while in the while are repeated. How can I take the variable outside of the first while loop for images and put it inside the other while loop and echo it.

UPDATE I GOT IT TO WORK everyone was telling me the right thing I am just slow. I modified the code below where it says grab form data and insert form data, is what i needed to do.

Thanks

        /* LOOP THROUGH IMAGES */
        $myDir = dir("images/");
        while(($file = $myDir->read()) !==false){

            if(!is_dir($file)){

            echo "$file";

            }
        }/*SHOE IMAGE WHILE LOOP ENDS*/


        /* LOOP THROUGH SHOEDATA TABLE */

        $results = mysql_query("SELECT * FROM shoeData");


        while($row = mysql_fetch_array($results)){

        $name = $row['name'];
        $about = $row['about'];
        $company = $row['company'];
        $buy = $row['buy'];
        $tags = $row['tags'];
        $id = $row['id'];



        /* ECHO THE SHOEDATA RESULTS */     

            echo "<div class='span-8'>";


                echo "<ul>";
                    echo "<li>$name</l1>";
                    echo "<li>$about</l1>";
                    echo "<li>$company</l1>";
                    echo "<li><a href='$buy'>BUY</a></l1>";
                    echo "<li>$tags</l1>";
                echo "</ul>";




            echo "</div>";





        }/*SHOEDATA WHILE LOOP ENDS */

-----------------------UPLOAD SCRIPT UPDATE----------------------

Currently My upload script will move my files but their is nothing currently inputting a field into my database, how would I modify this script to also upload a link to my images in the table with


    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 2000000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

/------------------GRAB FORM DATA-----------------------------/

    $name = $_POST['name'];
    $about = $_POST['about'];

    $company = $_POST['company'];
    $buy = $_POST['buy'];
    $tags = $_POST['tags'];
    $imageName1 = $_FILES["file"]["name"];

/------------------INSERT INTO DATABASE----------------------/

$sql = "INSERT INTO shoeData (name,about,company,buy,tags,image)VALUES(
\"$name\",
\"$about\",
\"$company\",
\"$buy\",
\"$tags\",
\"$imageName1\"
)";

$results = mysql_query($sql)or die(mysql_error());

        if (file_exists("images/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "images/" . $_FILES["file"]["name"]);



          echo "Stored in: " . "images/" . $_FILES["file"]["name"];

        }
      }
    else
      {
      echo "Invalid file" . "<br/>";
      echo "Type: " . $_FILES["file"]["type"] . "<br />";
      }
3
  • How is information in the table related to the images? If there's no direct correlation between the two, what method makes sense to you to display them together? Commented Nov 25, 2009 at 23:52
  • I suppose somehow correlating the ID with the image name, not sure how though? Commented Nov 26, 2009 at 0:04
  • That's a fundamental issue. If you can't relate the image with each record in the table, it is not possible to show an image for a specific item. You can only show all images or some image that may or may not be related to the selected record. Linking the two is paramount to providing a useful answer to your user. Commented Nov 26, 2009 at 6:41

2 Answers 2

2

You need to query your shoe data from the database and then for every one of these you've queried, request the matching image through a function that takes the image name or something as a parameter.

Then in that function you can loop through all your images and find the one that matches the shoe you've got. Return that image $file to your database query.

So inside your Query loop, call the function which query's (loops through) the images finding the right image, matching the shoe data you've just for your database.

Now you can echo that $file and all its other data.

Sign up to request clarification or add additional context in comments.

6 Comments

ok I am going to try this, Im very new to php, so I may need more explanation
am I supposed to use the GET variable to grab the image name, Im lost.
how does the data in your database relate to the images? You should probably have a field in your database that contains an image name, that you fetch just as you do the rest of your data, then you can get that image name from your result set and pass it to the function.
$image = row['image']; getimagedir ( $image ); // here you go through your images and find one with the name passed from your database... which for eases sake should be the file name of the image...
I dont have a image row, because I am creating a upload script so I dont have to manually add the images, and I dont know how to go about adding the image path to the row when I upload an image, which is why I am running a loop to go through the image directory, obviously this may not be the way to do it though, because then their is no link to the image file.
|
2

As noted, you need to specify which image should be used for which shoe in the database:

ALTER TABLE shoeData ADD COLUMN image VARCHAR(256);
UPDATE shoeData set image='path/to/image.jpg' WHERE id=1;

Then you can grab the shoe image in your main loop:

while($row = mysql_fetch_array($results)){
 $image = $row['image'];
 printf('<img src="/images/%s" alt="" />', htmlentities($row['image']));

 ....
 Rest of the loop
 ....

}

5 Comments

my only issue with this is, that in the future I am planning to build an upload script, and have never made one where I upload image paths to the database table while I upload the image. Thats why I have the while loop to go through the image directory.
It's simple: After the image is uploaded move it to the images/ directory and update the image path in the database with the image filename.
move the image manually that seems to defeat the purpose of having a upload script. it would be faster to ftp it. Im confused.
Two key points: you need an explicit link between a product and it's image. Adding a database field provides this link. Secondly, after an image is uploaded & moved (see: php.net/move_uploaded_file) PHP can create the link for you. So, the upload script would do two things: save the file to the appropriate spot and create the link in the database.
Thanks for the help by the way, I understand what your saying, but I dont see anything on the page that will show me how to modify my upload script to add a a link to my images in my image field in my database. I added my uplaod script above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.