First I want to point that I’m newbie in php&mysql and still learning.
I have 2 tables.
1 table:
Cats - where I have:
- cat_id
- cat_name
2 table is for images and I have:
- id
- caption
- name
- size
- type
- file_path
- img_category
I’m trying to make category for my images on site. So far I can show categories list but how to make when I click on some category to show me images only from this category?
Here is what I have so far but when I choose category it show me first image in that category and when I click ‘Next’ I get next image from next category.
<?php
// show categories list
$q = mysqli_query($con,"select * from cats");
while ($res = mysqli_fetch_assoc($q))
{
echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>';
}
?>
<hr>
<?php
//show image from that category
$cat_id = $_GET['cat_id'];
$query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$previd = -1;
$currid = $line[0];
if (isset($_GET['id'])) {
do {
$currid = $line[0];
if ($currid == $_GET['id']) break;
$previd = $currid;
$line = mysqli_fetch_array($result, MYSQL_BOTH);
} while ($line);
}
if ($line) {
echo "<div id=\\"picture\\">";
echo "<img style=\\"width:100%;margin:0 auto;\\" src=\\"upload/".$line['name']."\\" /></a><br />";
echo "<div id=\\"caption\\">".$line['caption']."</div><br />";
}
else echo "There is no images!\
";
if ($previd > -1) echo '<a href="pic.php?cat_id='.$previd.'" class="prev_pic"><span>Prev</span></a>';
echo str_repeat(' ', 5);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
$query = "select * from images order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="pic.php?cat_id='.$row['id'].'"class="random">Random</a>';
}
echo str_repeat(' ', 5);
if ($line) echo '<a href="pic.php?cat_id='.$line[0].'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
echo "</div>";
?>
I gues is a little mess in the code and I’m sorry about that.
Any help is appreciate
edit://
One image can be in one category at the time… one-to-one
Oh and BTW this is a one-to-many relationship (many images in one category) not a one-to-one. Captions to images is a one-to-one (one caption per image).
Those links for prev and next are for the previous and next category, no? If so then just generate them from $cat_id.
It must be 1 image at the time whit pagination buttons for next and prev. So I can put LIMIT in sql query but how to make buttons to work?
Oh I see what you are trying to do now. Look up a tutorial on PHP pagination.
I thought is one-to-one because one image is in one category only not in many…
No because a category stores many images. A one-to-one between images and categories would be where you have exactly one category for each image.
Ah OK, what you need is to use the LIMIT clause in MySQL.
So you’re probably want to query for the images within the category (as you are now) with the addition of a LIMIT $offset, 1 at the end where $offset is increased and decreased by your Next and Previous buttons
I still don’t get how to create link for next and prev image while I hold both category= and id= at the same time.
I have one page also where I query all the images in the table and there I have buttons and no problem with them… But in the category page I don’t understand how this work.
To SELECT all categories and display them (in alphabetical order):
SELECT cat_image_category_id, name FROM CAT_IMAGE_CATEGORY ORDER BY name
With the result, you’ll be able to display all the categories with their unique ID.
When somebody clicks on a specific category, you’ll grab the ID and select the corresponding images based on the selected category.
To SELECT all images in one particular category, this would look like this:
SELECT file_path, caption FROM CAT_IMAGE WHERE cat_image_category_id = 18
Whit this query you should be able to use pagination more easily.
I know I didn’t answer your “main” question, but starting with the right database design will help you a lot. And if you’re able to display all images from a category, adding pagination after will be much more easier.
Yes, this is what I try to achieve. I have this two tables:
===============
IMAGE_CATEGORY
===============
- image_category_id
- name
===============
IMAGE
===============
- image_id
- image_category_id
- caption
- name
- size
- type
- file_path
For example category like:
Cars
Animals
Buildings
If I have 3 images in category ‘Car’ and I choose on index page category ‘Car’ I want to load first image from this category then whit buton ‘Next’ change to second image from this category and so on… That’s it.
IIt’s just working as expected and seems so easy… I don’t know what to say. Thank you very much!
Just one more thig:
On ‘Random’ button when hit some ‘id’ from another category it shows blank page. Is there some way to search and show random image just from this category?