Show Others Images if Available Places - php

I want to show others image based on limit.
Table group images
| id | Name | Max places |
1 Static 3
2 Rotate 2
Table images
| id | group_id | url |
1 1 https://ima.ges.png
2 2 https://ima.ges.png
3 2 https://ima.ges.png
I want to show images in my template based on Max available places on the groups. Let say I have 1 images to show in group static, then the groups will still have 2 more slot then show others images.
In conclusion, it still show 3 images in template.
1. images from https://ima.ges.png
2. images from default value
3. images from default value
The question is, how to do this based on mysql data?
thanks for helping

Related

Fast way to find out if one value of one column exists in the same table with 2 or 3 other values

I have 3 tables. One for products, one for tags and one for products-tags-associations.
The tags have different levels and work like categories and subcategories, but the tags are not associated directly to each other. The only thing that connects tags to each other (or put a subcategory in a category) are the products.
Imagine I have the product with the id 1, called Apple iPhone 13 Pro Max
It has the following tags:
+----+---------------------+-------+
| id | name | level |
+----+---------------------+-------+
| 1 | Telefoon | 1 |
| 2 | Apple | 2 |
| 3 | iPhone 13 Pro Max | 3 |
Which means that the products_tags_association table has these rows in it:
+------------+--------+
| product_id | tag_id |
+------------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
Because of this combination of information, I now know that the tag "Telefoon" has the tag "Apple" inside it, and the tag "Apple" has the tag "iPhone 13 Pro Max" inside it.
This is how I build the menu on my website.
I now have a problem though, as I have a website with 239 536 rows in the products_tags_association table.
I'm trying to create a step-by step selection tool where they first choose a tag from level 1, then level 2, then level 3, but the tags for each level need to only be the tags that have products which have the selected tag from the previous level.
The logic I have right now is this:
$sql='SELECT tags.id,level, name FROM tags WHERE tags.level="'.$lvl.'";';
$tags=getDataFromDb($sql,'id');
if(is_array($tags) && is_array($selectedTagsByLvl) && $lvl>1){
foreach($tags as $t=>$tg){
$conditions='';
if(is_array($selectedTagsByLvl)){
foreach($selectedTagsByLvl as $l=>$tgs){
$conditions.=' AND id IN (SELECT product_id FROM products_tags_association WHERE product_id=products.id AND tag_id IN ('.implode(',',$tgs).'))';
}
}
$sql='SELECT id FROM products WHERE online=1 AND id IN (SELECT product_id FROM products_tags_association WHERE product_id=products.id AND tag_id='.$tg['id'].') '.$conditions.' LIMIT 1;';
if(!is_array(getDataFromDb($sql))){
unset($tags[$t]);
}
}
if(count($tags)==0){
return NULL;
}
}
return $tags;
So first I get all the tags that have the level I'm selecting right now, and then for each of those tags I check if there is one product that has that tag, plus each of the tags that I've selected in the previous steps. I should only be able to select a few tags per level.
This works, the problem is that it's too slow.
How can I make this faster?
All I need to know is if there is a combination of one product_id with each of the selected tag_id's and the tag_id's for the current level. Maybe something with GROUP BY product_id?
I'm using PHP and mysql (MariaDB)
SELECT product_id, COUNT(*) AS ct
FROM table
GROUP BY product_id
Then, ct tells you how many times each product_id shows up in the table. Tacking on HAVING ct > 1 to list just the products that occur more than once.
General rule: When you can do a task in a single SQL statement it is better to do so than shoveling data into the client to have it do the work. (And usually a lot less coding!)

I want to be able to search for a Post in the Post table based on whats inside a different table

Inside my post table is my designerID which is the same in the Designer table, when i make a post the designerID is set to the same as it is in the designer table, I want to have a search at the top to allow users to find designers based on what they do, i.e logo design, web design.
The designer table where I have recorded in Boolean what the designer does
Is it possible for me to filter posts based on what ive got?
thanks in advance
I want the user to be able to click a button that says logo which will then only show the posts from designers who ticked logo.
As you said, you are saving the type of work they do in a boolean.
1 representing that they do the type of work
0 representing that they do not do the type of work
If you would like to select all the designers that do logos, this would be your query.
SELECT * FROM designers WHERE logo = 1
This query will give you all the designers that do logos.
You do not need an additional table for categories.
option 1
If you determine that your type will not increase , you can just change
bool to int , when user create a new post, increment the number.
So you can use numbers to determine if he is working on some type,and easy to find
out who is really good at some type.
option 2
create category to recode post types , category_counter to record the numbers of designer working types
category
id | name
--- | ---
1 | logo
2 | website
post_counter
id | designer_id | category_id | number
--- | --- | --- | ---
1 | 1 | 1 | 10
2 | 1 | 2 | 20
3 | 2 | 1 | 5
Maybe my answer is wrong. I apologize for failing to understand the question .

Inserting and searching for multiple keywords

I am planning designing a PHP MySQL database for a local museum's photo gallery.
One thing that terrifies me now is how to implement saving multiple keywords for a single image for items where more than one technic or material was used to design them, e.g.
PAINTING TECHNICS:(liner, wiping, scumbling), MATERIAL USED: (oil, canvas,).
Ideally, I would have to use the select option lists to save them for each record. But if I want to save two or more of the words from the same list, I cant.
Do you have any suggestions I might consider?
joseph
You will need to implement a one-to-many relationship between your image table and your keyword table. One image can have many keywords. Each row in your keyword table will need to reference a row in the image table through a foreign key. For example:
images
id | asset
---------------
1 | image1.png
2 | image2.png
keywords
id | image_id | keyword | category
-----------------------------------------------
1 | 1 | liner | painting techniques
2 | 1 | wiping | painting techniques
3 | 1 | scumbling | painting techniques
3 | 2 | oil | material used
Or something along those lines. This is how you allow one row (an image) to be associated with many rows (keywords) in a different table.
For each keyword submitted, you would add another row to the keywords table, making sure to reference the associated image.

How to assign comment id?

I am making comment system and now I want to insert comments to database and I am confusing that on what basis to assign specific comment_id.
Suppose we have multiple dives of images with comment system.if someone comment on image then how can we assign a specific comment id on that specific image.and if other user comment on same image then how can he found that image comment_id so the comment save in right direction.
we have many images and comment system for that image.
My english is bad may be you understand what i want to say.
You need to create a number of different tables in the database
table: comments
|comment_id | userID | name | comment | (for example)
1 50 James test
2 50 James test
3 50 James test
table: images
|image_id | link |
1 example.com/images/image1.png
2 example.com/images/image2.png
3 example.com/images/image3.png
table: comments_on_images (to make the table's purpose clear)
|id | comment_id | image_id
1 1 2
2 2 2
3 3 1
Using this method you can assosciate any number of comments to any images. You have to query the database using JOINS to get all the information you need.

How do I save related data in CakePHP?

I have a 'websites' and 'products' table. The a product can be attached to different websites. So I created a new table called 'attached_products'. It has the fields id, website_id and product_id.
So I have two questions for now, the first one is how do I save a website that will appear something like this in the attached_products table:
id | website_id | product_id
1 | 4 | 3
2 | 4 | 5
3 | 4 | 6
4 | 4 | 10
the above means four products with ids 3,5,6,10 were attached to a website with an id of 4
my second question is, how do I modify the record for example I want to remove the two products attached to a website?
I'm new to CakePHP, many thanks for any help! :)
You have the right idea about what you're doing. Read about HABTM relationship in Cake book. You should learn more about Cake (from simple to complicated things). That one is pretty hard for a newcomer.

Categories