Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9276413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:50:50+00:00 2026-06-18T16:50:50+00:00

I have tried to create a script that loops through 5 items that match

  • 0

I have tried to create a script that loops through 5 items that match 1 item and then show one image associated with each of the 5 items.
Here is a fiddle I create to illustrate my issue.
http://sqlfiddle.com/#!2/17d66/1/0

I want to select one picture from each matching item. Here is the code I have come up with but so far it is not working. There are two points of comparison I have used, a tag and family. The tag associated with the type of product and the family is the category it belongs to. For instance, the iPhone 4 would have smartphone for a tag and iPhone 4 as the family. Any help would be appreciated, my code is below.

<?php
    // main product
$images = "SELECT img_id FROM product_images WHERE img_tag = '$p_type' && img_family = '$p_family' LIMIT 5";
$img_list = mysql_query($images);
while($rows = mysql_fetch_array($img_list))  {
    $img_name = $rows['img_name'];
    $img_tag = $rows['img_tag'];
    $img_family = $rows['img_family'];
    $img_id = $rows['img_id'];
    //echo $img_name;


            <div>
              <img src="view.php?h=400&w=400&imgid=<?php echo $img_id; ?>" />
            </div>


}

         </div>      
  </div>

<div id="left_other">
    <div class="titleHeaders">Other matching items:</div>
    <?php 
    $match_items = "SELECT id,product_name,product_type FROM product_list WHERE name_family = '$p_family' AND id <> '$id' LIMIT 5";
    $match_q = mysql_query($match_items);   
    $count = mysql_num_rows($match_q);

    if($count > 0) {
        //get matching products
        while($items = mysql_fetch_array($match_q)) {
        $id = ($items['id']);
        echo "<p>$id</p>";
        $p_name = ($items['product_name']);
        echo "<p>$p_name</p>";
        $type = ($items['product_type']);
        echo "<p>$type</p>";
        $full = "$p_name $type";
        echo "<p>$full</p>";

        //get photos

            $other_photos = "SELECT * FROM product_images WHERE img_family = '$p_family' AND img_tag <> '$p_type' LIMIT 1"; 
            echo $other_photos;
            $other_q = mysql_query($other_photos);  
            while($pictures = mysql_fetch_array($other_q)) {
                echo "<p>----------------------</p>";
                $theid = ($pictures['img_id']);
                echo "<p>$theid</p>";
                $photo = ($pictures['img_name']);
                echo "<p>$photo</p>";
                $thetype = ($pictures['img_tag']);
                echo "<p>$thetype</p>";
                echo "<p>----------------------</p>";

            if($photo != $newname && $theid != $newid){
                if ($photo == '') {
                    $poly = 'images/pph.jpg';
                    //echo $poly;
                }else{
                    $poly = 'product_images/regular_images/'.$photo;
                    //echo $poly;
                } // end if
                $newname = $photo;
                $newid = $theid;
            }else{
                echo "<p>The photo name is the same!</p>";
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:50:51+00:00Added an answer on June 18, 2026 at 4:50 pm

    You can use a single query to grab the results. Here is how you can do it.

    SELECT
      pl.id,
      pl.product_name,
      pl.product_type ,
      pi.product_images
    FROM   product_list AS pl
    LEFT  JOIN (SELECT product_list_id , product_images FROM product_images LIMIT 1) AS `pi` ON pi.product_list_id = pl.id
    WHERE pl.img_tag = '$p_type' AND pl.img_family = '$p_family'
    LIMIT 5 
    

    But it would be batter if there was some table structure , sample data and desired output.

    EDIT

    Try this query and you can put a where at last

    SELECT 
      * 
    FROM
      `products` AS p 
      LEFT JOIN (SELECT MAX(img_id), img_name, img_tag, img_family FROM  `images`) AS i ON i.img_family = p.`p_family` AND i.img_tag = p.`p_tag` ;
    

    EDIT

    SELECT *
    FROM `products` AS p
      LEFT JOIN images as i
        ON i.img_family = p.`p_family`
    group by p.p_name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to create this script that would create 10 empty slides, but
I can't seem to figure this one out. I have a validator-type script that
I have an odd issue. I have one PHP script that is used for
I have prepared simple script that displays image under ProgressGauge bar on wpInstalling Page.
I have tried create this table, but nothing I have tried works from FKs.
I have tried to create a simple controller/model (based on the samples code of
We have tried to create sample webservices helloworld in Java using Jersey and Tomcat
I have tried with ado.net create table columnName with unique name. as uniquename I
Have anybody ever tried to create thumbnails/previews of MS Office files? I do not
I have tried FBConnect for iPhone and am able to create/manage sessions and upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.