HCProfessionals Posted March 30, 2012 Share Posted March 30, 2012 I can use the first while loop, but there is no data in the second while loop. Is there a better way as I have never done this before... <?php $featured_results = mysql_query("SELECT * FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'"); $fa=0; while($featured_row = mysql_fetch_assoc($featured_results)) { $fthumb_result = mysql_query("SELECT image_name FROM product_images WHERE product_id='".$featured_row['product_id']."' AND thumb='1'"); $fthumb = mysql_fetch_row($fthumb_result); if ($fa==0) { echo "\n<img id=\"home-slider-photo-".$fa."\" class=\"home-slider-photo preload\" src=\"/includes/getimage.php?img=".$fthumb[0]."&w=370&h=370\" alt=\"\" />"; } else { echo "\n<img id=\"home-slider-photo-".$fa."\" class=\"home-slider-photo preload home-slider-photo-unsel\" src=\"/includes/getimage.php?img=".$fthumb[0]."&w=370&h=370\" alt=\"\" />"; } $fa++; } echo "<div id=\"home-slider-photo-price\">"; $fb=0; while($featured_row2 = mysql_fetch_assoc($featured_results)) { if ($fb==0) { echo "\n<div id=\"home-slider-photo-price-".$fb."\" class=\"home-slider-photo-price\">\n<span>only</span>$".$featured_row2['product_price']."\n</div>"; } else { echo "\n<div id=\"home-slider-photo-price-".$fb."\" class=\"home-slider-photo-price home-slider-photo-price-unsel\">\n<span>only</span>$".$featured_row2['product_price']."\n</div>"; } $fb++; } echo "</div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/ Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 Rather than attempting to query your database twice to get the same set of data, you should select the data out into an array, then loop through the array. You could also avoid doing two loops by having one loop which builds your two different sets of HTML (into strings), then echoing the HTML separately. Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332745 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 I know it's a lot of work, but can I get an example? Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332756 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 Try doing what I said, and I'll help you fix any problems. What I said can be easily converted into psuedo code at least. Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332760 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 Guess I'm screwed, lol.. I'm guessing using foreach? Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332766 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 Yes. Here's a bit to get you started. <?php $featured_results = mysql_query("SELECT * FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'"); $results = array(); while($row = mysql_fetch_assoc($featured_results)) { $results[] = $row; } $first_html = ''; $second_html = ''; foreach($results AS $result){ //Put your HTML into the strings in here. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332769 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 I'm lost.... Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332772 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 I just realized I was making it even more difficult than it needs to be. <?php $featured_results = mysql_query("SELECT * FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'"); $first_html = ''; $second_html = ''; while($row = mysql_fetch_assoc($featured_results)) { //Put your HTML into the strings in here. } ?> What are you lost on? Try using this and editing it for your code. you'll also want to turn your SQL into a join at some point, to really cut down on queries. But for now just see if you can get this working. Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332773 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 What goes in the first_html and then I echo them inside the while loop, right? Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332774 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 No, you'll echo it after you've put everything into it. Just try a simple version of your original project. I assume your product has a name, try adding each name to the string $first_html .= " ".$row['product_name']; //Make this the right variable for your db. Then outside of the loop, echo that string. Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332778 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 This is taking up way to much time to keep go back and forth, I learn from seeing and studying, so give me a price... This a free project I am doing to help a friend and this is torturing the hell out of me. :'( Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332781 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 <?php $featured_results = mysql_query("SELECT * FROM products JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'"); $i=0; while($row = mysql_fetch_assoc($featured_results)) { $fthumb_result = mysql_query("SELECT image_name FROM product_images WHERE product_id='".$row['product_id']."' AND thumb='1'"); $fthumb = mysql_fetch_row($fthumb_result); if ($i==0) { $first_html .= "\n<img id=\"home-slider-photo-".$i."\" class=\"home-slider-photo preload\" src=\"/includes/getimage.php?img=".$fthumb[0]."&w=370&h=370\" alt=\"\" />"; } else { $first_html .= "\n<img id=\"home-slider-photo-".$i."\" class=\"home-slider-photo preload home-slider-photo-unsel\" src=\"/includes/getimage.php?img=".$fthumb[0]."&w=370&h=370\" alt=\"\" />"; }; $i++; } echo "$first_html"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332787 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 On your last line, you don't need the variable in quotes. I assume the shrug means you didn't get what you expected. you need to provide more info. Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332789 Share on other sites More sharing options...
HCProfessionals Posted March 30, 2012 Author Share Posted March 30, 2012 I did get what I expected, but I banged my head on the keyboard along the way and they don't have an icon for that Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332792 Share on other sites More sharing options...
jcbones Posted March 30, 2012 Share Posted March 30, 2012 <?php $featured_results = mysql_query("SELECT * FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'"); $fa=0; while($featured_row = mysql_fetch_assoc($featured_results)) { if ($fa==0) { echo "\n<img id=\"home-slider-photo-".$fa."\" class=\"home-slider-photo preload\" src=\"/includes/getimage.php?img=".$featured_row['image_name']."&w=370&h=370\" alt=\"\" />"; } else { echo "\n<img id=\"home-slider-photo-".$fa."\" class=\"home-slider-photo preload home-slider-photo-unsel\" src=\"/includes/getimage.php?img=".$featured_name['image_name']."&w=370&h=370\" alt=\"\" />"; } $fa++; } echo "<div id=\"home-slider-photo-price\">"; $fb=0; mysql_data_seek($featured_results,0); //return resource pointer to first row of data. while($featured_row2 = mysql_fetch_assoc($featured_results)) { if ($fb==0) { echo "\n<div id=\"home-slider-photo-price-".$fb."\" class=\"home-slider-photo-price\">\n<span>only</span>$".$featured_row2['product_price']."\n</div>"; } else { echo "\n<div id=\"home-slider-photo-price-".$fb."\" class=\"home-slider-photo-price home-slider-photo-price-unsel\">\n<span>only</span>$".$featured_row2['product_price']."\n</div>"; } $fb++; } echo "</div>"; ?> mysql_data_seek Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332795 Share on other sites More sharing options...
Psycho Posted March 30, 2012 Share Posted March 30, 2012 DO NOT RUN QUERIES WITHIN LOOPS! You can get all the data you need with a single query. //Create/run query $query = "SELECT products.product_price, products.product_id, product_images.image_name FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'": $result = mysql_query($query); //Vars to hold content $html_1 = ''; $html_2 = ''; $index = 0; //Process data into the content vars while($row = mysql_fetch_assoc($result)) { $imgClass = ($index==0) ? 'home-slider-photo preload' : 'home-slider-photo preload home-slider-photo-unsel'; $divClass = ($index==0) ? 'home-slider-photo-price' : 'home-slider-photo-price home-slider-photo-price-unsel'; $html_1 .= "\n<img id=\"home-slider-photo-{$index}\" class=\"{$imgClass}\" src=\"/includes/getimage.php?img={$row['image_name']}&w=370&h=370\" alt=\"\" />"; $html_2 .= "\n<div id=\"home-slider-photo-price-{$index}\" class=\"{$divClass}\">\n<span>only</span>${$row['product_price']}\n</div>"; $index++; } echo $html_1; echo $html_2; echo "</div>"; Quote Link to comment https://forums.phpfreaks.com/topic/260013-multiple-whiles-single-query-is-there-a-way/#findComment-1332798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.