Jump to content

While inside while, needs to be something different


HCProfessionals

Recommended Posts

Everything works except for the data needed for "$f_images", which what I am trying to get is the multiple images for each product.

 

            	<?php
			$featured_results = mysql_query("SELECT * FROM products JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_active='1' AND thumb='1' Limit 10");

			while($featured_row = mysql_fetch_assoc($featured_results))
			{
				//Thumbnail Query
				$fthumb_result = mysql_query("SELECT image_name FROM product_images WHERE product_id='".$mfeatured_row['product_id']."' AND thumb='1'");
				$fthumb = mysql_fetch_row($fthumb_result);

				$fimages_result = mysql_query("SELECT image_name FROM product_images WHERE product_id='".$featured_row['product_id']."'");
				while($fimages_row = mysql_fetch_assoc($fimages_result))
				{
					$f_images .= "'/includes/getimage.php?img=".$fimages_row[0]."&w=224', ";
				}

				$f_vars .=  "\nproductId = ".$featured_row['product_id'].";\napp.isotope.vars.homeFeaturedImages[productId] = [".$f_images."];\napp.isotope.vars.homeFeaturedProducts[productId] = {\nname  : \"".$featured_row['product_name']."\",\nprice : \"".$featured_row['product_price']."\",\nurl   : '/'\n};";

			}
			?>

Link to comment
Share on other sites

Wasn't this already covered in your earlier post? There is  no need to run the 2nd query inside the first while loop.  And, you should NEVER run queries within loops - it will lead to significant performance problems. You are already JOINing the product_images table on the products table. So, you already have the data you need in the first query results. Take another look at the code I last posted in your earlier thread.

Link to comment
Share on other sites

No, this is entirely separate from my last post.

Did you LOOK at the code I provided? Does it provide the results you were looking for? If not, how are they different. If you even gave the code a cursory glance you will notice that I used the results from the initial query to get the product information and the image information. I will say this a third time (between the last post and this one): Running that second query inside the while() loop is completely unnecessary and not advised. You should never run queries in a loop

 

both Psycho and I did address that in the previous post, and I believe he posted an actual example of how to do it.

Yes, I did.  ::)

Link to comment
Share on other sites

I need to figure out how to make this work with the 2 queries. Everything but the $mfimages_results works.

I really don't want to use Join, because with the rules I can only pull the thumbs and if I leave the thumbs rule out, it messes everything up.

 

                      <?php
			//Featured Item Data
			$mfeatured_results = mysql_query("SELECT * FROM products WHERE product_featured='1' AND product_active='1'");

			//Featured Item Images
			$mfimages_results = mysql_query("SELECT image_name FROM product_images WHERE product_id='".$mfeatured_row['product_id']."' AND thumb='1'");

			$mf_i=0;
			while($mfeatured_row = mysql_fetch_assoc($mfeatured_results))
			{
				$imgClass = ($mf_i==0) ? 'home-slider-photo preload' : 'home-slider-photo preload home-slider-photo-unsel';
				$priceClass = ($mf_i==0) ? 'home-slider-photo-price' : 'home-slider-photo-price home-slider-photo-price-unsel';
				$descClass = ($mf_i==0) ? 'home-slider-description' : 'home-slider-description home-slider-description-unsel';

				$mf_thumbs .=  "<img id=\"home-slider-photo-".$mf_i."\" class=\"".$imgClass."\" src=\"/includes/getimage.php?img=".$mfimages_results[0]."&w=370&h=370\" alt=\"\" />\n";
				$mf_prices .=  "<div id=\"home-slider-photo-price-".$mf_i."\" class=\"".$priceClass."\"><span>only</span>$".$mfeatured_row['product_price']."</div>\n";
				$mf_vars .=  "app.slider.vars.productUrl[".$mf_i."] = '?".$mfeatured_row['product_id']."';\n";
				$mf_description .=  "<div id=\"home-slider-description-".$mf_i."\" class=\"".$priceClass."\"><h2>".$mfeatured_row['product_name']."</h2><p>".$mfeatured_row['product_desc']."</p></div>\n";

			$mf_i++;
			}
			?>

Link to comment
Share on other sites

I need to figure out how to make this work with the 2 queries. Everything but the $mfimages_results works.

I really don't want to use Join, because with the rules I can only pull the thumbs and if I leave the thumbs rule out, it messes everything up.

 

I've said it three time and I'm not going to say it again. But, not that you "seem" to have provided some more information I will attempt to help further. I think you are saying that the products may have multiple images in the product_images table, but you only want the thumbnail image. Are there multiple thumbnails per product? If so, do you only want one? The problem you were likely having in your previous query was probably due to some products having no thumb or multiple thumbs. But, you didn't provide any information as to your DB layout. But, this should be able to be resolved by putting the thumb check in the correct place and possibly using a GROUP BY function.

 

I believe I also stated you should not be using '*' in your select query and should instead list the applicable fields.

 

No matter - You still only need ONE query.. If you are running queries in a loop you are doing it wrong. The following query will include the thumbnail image in the results for each product. Even if there are multiple thumbnails you will only get one per product.

SELECT p.product_price, p.product_id, i.image_name
FROM products AS p
LEFT JOIN product_images AS i
   ON p.product_id = i.product_id
  AND i.thumb='1'
WHERE p.product_featured='1'
  AND p.product_active='1'
GROUP BY p.product_id

Now this may not be the correct query, but it's the best I can come up with based on the information you have provided. But, this is definitely the direction you want to go in.

Link to comment
Share on other sites

products table

product_id, cat_id, product_name, product_desc, product_price, product_featured, product_inv, product_date, product_active

 

product_images table

image_id, product_id, image_name, thumb, image_date

 

Step 1:

Featured Area: There are several images per item and the thumbnail is based on which image has thumb=1 to make it easy to change the thumbnail for each product. If I don't have the thumbnail indicated in the sql query, it will pull all the images and insert multiple rows from the same product when it is pulled from the database.

 

Step 2:

Random Products: At the bottom of the page is 10 random products with their thumbnail shown. If you click on the thumbnail it will open a slideshow (similar to lightbox) with more information for the product. I have never done anything quite this advanced and is really where I need help with how the SQL statements are made along with PHP.

 

Thank you for the help as I know I am frustrating!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.