while loop in a while loop
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 />";
}
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.
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
....
}
精彩评论