Jump to content

Multiple While's, Single Query - Is there a way?


HCProfessionals

Recommended Posts

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>";
			?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

:shrug:

 

<?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";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";

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.