I have two seperate tables in my DB, here the relevant fields:
table images:
CREATE TABLE `images` (
`image_id` int(4) NOT NULL AUTO_INCREMENT,
`project_id` int(4) NOT NULL,
`user_id` int(4) NOT NULL,
`image_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date_created` date NOT NULL,
`link_to_file` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`link_to_thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`given_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`note` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`image_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=51 ;
and table projects:
CREATE TABLE `projects` (
`project_id` int(4) NOT NULL AUTO_INCREMENT,
`user_id` int(4) NOT NULL,
`project_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`project_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date_created` date NOT NULL,
`date_last_edited` date NOT NULL,
`shared` int(1) NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=25 ;
I would like to display in a variable $content, a gallery of the oldest image from each project as a link to that project page and I have no idea how the mysql query should be built. Can you please help me with this? I have tried several if and while statements but the results have been complete failures and i am at the end of my (very limited) knowledge. I’m about to jump out the window…
So I would like to end up with
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_x" />
</a>
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_y" />
</a>
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_z" />
</a>
Update1:
To clarify I am trying to combine:
"SELECT * FROM projects WHERE user_id='$UserID' ORDER BY project_id DESC"
And maybe something like this:
$query = "SELECT images.project_id, projects.project_name ".
"FROM images, projects ".
"WHERE images.project_id = projects.project_id";
Haven’t tested for errors, but I’d do something like this: