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>";
}
You can use a single query to grab the results. Here is how you can do it.
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
EDIT