views:

265

answers:

2

Hello

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 />";
      }
+2  A: 

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.

Tony
ok I am going to try this, Im very new to php, so I may need more explanation
Anders Kitson
am I supposed to use the GET variable to grab the image name, Im lost.
Anders Kitson
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.
Tony
$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...
Tony
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.
Anders Kitson
You need to have some relation to your images from your database data, so either what I suggested or putting the image path in the database and using that then to fetch the images using your upload script. You can just pass the path retrieved from the database to your upload script and use it to upload the image.
Tony
+2  A: 

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
 ....

}
pygorex1
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.
Anders Kitson
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.
pygorex1
move the image manually that seems to defeat the purpose of having a upload script. it would be faster to ftp it. Im confused.
Anders Kitson
pygorex1
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
Anders Kitson