Jump to content

Recommended Posts

$company = mysql_query("SELECT comp_images.*, company.* FROM comp_images LEFT JOIN company ON num > 2");
while($comp = mysql_fetch_assoc($company))
{ echo "company name: $comp['name'] <br>";
$c = mysql_fetch_assoc(mysql_query("SELECT * FROM comp_images WHERE company='". $comp['name'] ."'"));
echo $c;
}

You can either do two queries (not in a loop, but two separate single queries) to get the list of companies and the list of all images sorted by country, OR you can do this (which is how I'd do it)

 

In the join the ON needs to represent the relationship between company and comp_images. There should be a unique ID for each company.

 

<?php
$sql = mysql_query("SELECT comp_images.*, company.* FROM comp_images LEFT JOIN company ON company.id = comp_images.company_id ORDER BY company.name");
$last_company_id = 0;
while($comp = mysql_fetch_assoc($sql))
{ 
    if($comp['id'] != $last_company_id){
        echo "company name: $comp['name'] <br>";
        $last_company_id = $comp['id'];
    }
   echo $comp['image_info_here'].'<br>';
}
?>

EDIT: jesirose beat me to it, but I'll post this nonetheless since I don't like seeing queries written inside the mysql_query() function as it makes debugging more difficult

 

You didn't show your current code, so this is only a guess based upon the table fields

$query = "SELECT comp_images.image, company.name
          FROM company
          LEFT JOIN comp_images
            ON company.id = comp_images.company";
$result = mysql_query($query) or die(mysql_error());

$company = false;
while($row = mysql_fetch_assoc($result))
{
    if($company != $row['name'])
    {
        $company = $row['name'];
        echo "<br><b>{$company}</b><br>\n";
    }
    echo $row['image'};
}

How would I go about two queries?

 

If I use SELECT on them, how would I make it loop without in a while? Or would I make it into a while loop?

 

I'm not sure how I would make it into a while loop though, because of the mysql_fetch_assoc for the while condition.

 

EDIT: Explanation why: I'm not really a big fan of the LEFT JOIN and things, I'd prefer use SELECT statements if you know what I mean.

 

It may be because I'm not the biggest under-stander of them..

A LEFT JOIN is still a SELECT.

 

If you plan on using a relational database, I'd suggest starting to get comfortable with JOINs. They're very powerful, and efficient.

 

Then the code you gave me still does the same. It displays x and then y straight after, and repeats it until it's done.

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.