Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

[How-To] Делаем рандом изображения для Категории товаров


Recommended Posts

Иик, если Вы не усиновили изображение для Категории товаров, берём случайное (из товара) и выводим его...Изображение

Пример:


До:

Изображение

После:

Изображение



Для этого, открываем /catalog/controller/product/category.php и находим строки:
 

if ($category_info['image']) {$image = $category_info['image'];} else {$image = '';}

После них, всивляем:


if ($category_info['image']) {$image = $category_info['image'];} else {$image = '';// Use random image from products in the category$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id ". "WHERE c.category_id = " . $category_info['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());if (mysql_num_rows($rndImgRslt) > 0){$imgRows = array();while($imgRow = mysql_fetch_array($rndImgRslt)){$imgRows[] = $imgRow['image'];}$image = $imgRows[array_rand($imgRows)];}}

Ниже, находим блок:


if ($result['image']) {$image = $result['image'];} else {$image = 'no_image.jpg';}

После него всивляем:


if ($result['image']) {$image = $result['image'];} else {$image = 'no_image.jpg';// Use random image from products in the category$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id ". "WHERE c.category_id = " . $result['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());if (mysql_num_rows($rndImgRslt) > 0){$imgRows = array();while($imgRow = mysql_fetch_array($rndImgRslt)){$imgRows[] = $imgRow['image'];}$image = $imgRows[array_rand($imgRows)];}}

Всё. Изображение

 

Link to comment
Share on other sites

выбирать случайное надо в mysql-запросе, а не из массива тысяч товаров.

Ну, если в магазине бугдет до 1.000-1.500 товаров, то MySQL ещё прокатит, несомненно...

Но, если товаров потому чтольше - то эи лишняя нагрузка на базу. ИМХО - в этом случае, лучше использовать php.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.