на сколько я понимаю, если нужен вывод случайного товара, заранее определенного в админке, то это
можно сделать на основе встроенных в магаз спец-предложений:
в файл ./cfg/database_structure.xml
после блока
PHP:
<table name='SS_special_offers' alias='SPECIAL_OFFERS_TABLE' PRODUCTANDCATEGORYSYNC='true' >
<column TYPE='int' PRIMARYKEY='true' IDENTITY='true' >offerID</column>
<column TYPE='int' >productID</column>
<column TYPE='int' DEFAULT='0' >sort_order</column>
<ForeignKey REFERTO='SS_products.productID' >productID</ForeignKey>
</table>
добавляем
PHP:
<table name='SS_random_offers' alias='RANDOM_OFFERS_TABLE' PRODUCTANDCATEGORYSYNC='true' >
<column TYPE='int' PRIMARYKEY='true' IDENTITY='true' >offerID</column>
<column TYPE='int' >productID</column>
<column TYPE='int' DEFAULT='0' >sort_order</column>
<ForeignKey REFERTO='SS_products.productID' >productID</ForeignKey>
</table>
файл ./cfg/tables.inc.php добавляем куда угодно
PHP:
if ( !defined('RANDOM_OFFERS_TABLE') )
{
define('RANDOM_OFFERS_TABLE', 'SS_random_offers');
}
файл ./core_functions/catalog_import_functions.php
после строки
PHP:
db_query("DELETE FROM ".SPECIAL_OFFERS_TABLE);
добавляем
PHP:
db_query("DELETE FROM ".RANDOM_OFFERS_TABLE);
файл ./core_functions/product_functions.php
после строки
PHP:
db_query("delete from ".SPECIAL_OFFERS_TABLE.$whereClause);
добавляем
PHP:
db_query("delete from ".RANDOM_OFFERS_TABLE.$whereClause);
файл ./includes/home.php
после строки
PHP:
$smarty->assign("special_offers",$result);
добавляем
PHP:
//random offers
$result = array();
$q = db_query("SELECT productID FROM ".RANDOM_OFFERS_TABLE." order by RAND() LIMIT 1") or die (db_error());
while ($row = db_fetch_row($q))
{
$q1 = db_query("SELECT productID, name, default_picture, Price, categoryID FROM ".
PRODUCTS_TABLE.
" where productID=$row[0]") or die (db_error());
if ($row1 = db_fetch_row($q1))
{
if ( is_null($row1[2]) )
continue;
$picture = db_query( "select filename, thumbnail, enlarged from ".
PRODUCT_PICTURES." where photoID=".$row1[2] );
$picture_row = db_fetch_row( $picture );
if ( $picture_row )
{
if ( file_exists( "./products_pictures/".$picture_row[0] ) )
{
$row1[2] = $picture_row[0];
$row1[3] = show_price($row1[3]);
$result[] = $row1;
}
}
}
}
$smarty->assign("random_offers",$result);
файл ./sql/demo_database.sql //это для сборки
после блока
PHP:
insert into SPECIAL_OFFERS_TABLE(offerID, productID, sort_order) values(1, 64, 3);
insert into SPECIAL_OFFERS_TABLE(offerID, productID, sort_order) values(3, 101, 1);
insert into SPECIAL_OFFERS_TABLE(offerID, productID, sort_order) values(4, 94, 2);
insert into SPECIAL_OFFERS_TABLE(offerID, productID, sort_order) values(5, 4, 4);
добавляем
PHP:
insert into RANDOM_OFFERS_TABLE(offerID, productID, sort_order) values(1, 64, 3);
insert into RANDOM_OFFERS_TABLE(offerID, productID, sort_order) values(3, 101, 1);
insert into RANDOM_OFFERS_TABLE(offerID, productID, sort_order) values(4, 94, 2);
insert into RANDOM_OFFERS_TABLE(offerID, productID, sort_order) values(5, 4, 4);
выполнить запрос в базу данных:
PHP:
CREATE TABLE IF NOT EXISTS `SS_random_offers` (
`offerID` int(11) NOT NULL AUTO_INCREMENT,
`productID` int(11) DEFAULT NULL,
`sort_order` int(11) DEFAULT '0',
PRIMARY KEY (`offerID`)
) ENGINE=InnoDB DEFAULT CHARSET=cp1251 AUTO_INCREMENT=9 ;
--
--
--
INSERT INTO `SS_random_offers` (`offerID`, `productID`, `sort_order`) VALUES
(1, 64, 3),
(3, 101, 1),
(4, 94, 2),
(6, 103, 0);
файл ./languages/russian.php
PHP:
define('ADMIN_ADD_PRODUCT_RAND', 'Случайный товар:');
define('ADMIN_RANDOM_OFFERS_DESC', 'Предложение случайного товара.'); // (179 стр.)
define('ADMIN_NO_RANDOM_OFFERS', 'Случайные предложения не выбраны');
define('ADMIN_ADD_RANDOM_OFFERS', 'Добавить в список случайных предложений');
файл ./templates/backend/catalog_products_categories.tpl.html
после строки
PHP:
<td width=1%>{$smarty.const.ADMIN_ADD_PRODUCT_INTO}</td>
добавляем строку
PHP:
<td width=1%>{$smarty.const.ADMIN_ADD_PRODUCT_RAND}</td>
в этом же файле после строк
PHP:
<td align=center>
{if $products[i].picture_count ne 0}
<a href="admin.php?dpt=catalog&sub=special&new_offer={$products[i].productID}">
<img src="images/admin_special_offer.gif" border=0 alt="{$smarty.const.ADMIN_ADD_SPECIAL_OFFERS}">
</a>
{else}
{/if}
</td>
добавляем
PHP:
<td align=center>
{if $products[i].picture_count ne 0}
<a href="admin.php?dpt=catalog&sub=special&random_offer={$products[i].productID}">
<img src="images/admin_special_offer.gif" border=0 alt="{$smarty.const.ADMIN_ADD_SPECIAL_OFFERS}">
</a>
{else}
{/if}
</td>
файл ./includes/admin/sub/catalog_special.php
после строк
PHP:
$smarty->assign("admin_sub_dpt", "catalog_special.tpl.html");
}
вставить
PHP:
if (!strcmp($sub, "special"))
{
if (isset($_GET["save_successful"])) //update was successful
{
$smarty->assign( "save_successful", ADMIN_UPDATE_SUCCESSFUL );
}
if (isset($_POST["save_random_offers"])) //save extra product options
{
if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
{
Redirect("admin.php?dpt=catalog&sub=special&safemode=yes");
}
//save existing
db_query("delete from ".RANDOM_OFFERS_TABLE) or die (db_error());
$offers = array();
//foreach ($_POST as $key => $val)
//{
//if(strstr($key, "offer_productID_") != false)
//{
//$a = str_replace("offer_productID_","",$key);
//$offers[$a]["productID"] = $val;
//}
//if(strstr($key, "offer_sort_") != false)
//{
//$a = str_replace("offer_sort_","",$key);
//$offers[$a]["sort"] = $val;
//}
//}
foreach ($offers as $key => $value)
{
$value["sort"] = (int)$value["sort"];
db_query("insert into ".SPECIAL_OFFERS_TABLE." (offerID, productID, sort_order) ".
" values ('$key', '".$value["productID"]."', '".$value["sort"]."')") or die (db_error());
}
Redirect("admin.php?dpt=catalog&sub=special&save_successful=yes");
}
if (isset($_GET["new_offer"])) //add new special offer
{
if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
{
Redirect("admin.php?dpt=catalog&sub=special&safemode=yes");
}
db_query("insert into ".RANDOM_OFFERS_TABLE." (productID, sort_order) values ('".$_GET["new_offer"]."',0)") or die (db_error());
Redirect("admin.php?dpt=catalog&sub=special");
}
//random offer
if (isset($_GET["random_offer"])) //add new special offer
{
if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
{
Redirect("admin.php?dpt=catalog&sub=special&safemode=yes");
}
db_query("insert into ".RANDOM_OFFERS_TABLE." (productID, sort_order) values ('".$_GET["random_offer"]."',0)") or die (db_error());
Redirect("admin.php?dpt=catalog&sub=special");
}
if (isset($_GET["delete_random_offer"])) //delete random offer
{
if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
{
Redirect("admin.php?dpt=catalog&sub=special&safemode=yes");
}
db_query("delete from ".RANDOM_OFFERS_TABLE." where offerID='".$_GET["delete_random_offer"]."'") or die (db_error());
Redirect("admin.php?dpt=catalog&sub=special");
}
//now select all available product options
$q = db_query("select offerID, productID, sort_order from ".RANDOM_OFFERS_TABLE." order by sort_order") or die (db_error());
$result = array();
while ($row = db_fetch_row($q))
{
//get product name
$q1 = db_query("select categoryID, name from ".PRODUCTS_TABLE." where productID=$row[1]") or die (db_error());
if ($row1 = db_fetch_row($q1))
{
$row[3] = $row1[0];
$row[4] = $row1[1];
$result[] = $row;
}
}
$smarty->assign("random_offers", $result);
//set sub-department template
$smarty->assign("admin_sub_dpt", "catalog_special.tpl.html");
}
в предыдущем файле на самом деле много ненужных запросов и движений, их можно выковырять при желании...
файл ./templates/backend/catalog_special.tpl.html копируйте целиком
PHP:
{* special offers on the homepage *}
<table cellpadding="10" cellspacing="10" bgcolor="#CCCCCC">
<p>
{if $save_successful ne NULL}
{$save_successful}
{/if}
<tr><td bgcolor="#FFFFFF" width="50%">
<p>
{$smarty.const.ADMIN_SPECIAL_OFFERS_DESC}
<form action="admin.php" method=POST>
<table cellpadding=5 cellspacing=0 width="100%">
<tr class="mainmenu_selected_{$current_dpt}">
<td><div align="left">{$smarty.const.ADMIN_PRODUCT_NAME}</div></td>
<td>{$smarty.const.ADMIN_SORT_ORDER}</td>
<td width="10%">{$smarty.const.DELETE_BUTTON}</td>
</tr>
{section name=i loop=$offers}
<input type=hidden name="offer_productID_{$offers[i][0]}" value="{$offers[i][1]}">
<!--<tr class="{*cycle values="bg_white, bg_$current_dpt"*}">-->
{if $smarty.section.i.index is div by 2}<tr height="34">{else}<tr bgcolor="#D0E7FD" height="34">{/if}
<td><a href="admin.php?dpt=catalog&sub=products_categories&categoryID={$offers[i][3]}">{$offers[i][4]}</a></td>
<td align=center><input type=text size=5 name="offer_sort_{$offers[i][0]}" value="{$offers[i][2]}"></td>
<td><div align="center"><a href="admin.php?dpt=catalog&sub=special&delete={$offers[i][0]}"><img src="images/backend/button_delete.gif" border=0 alt="{$smarty.const.DELETE_BUTTON}"></a></div></td>
</tr>
{sectionelse}
<tr bgcolor=white>
<td colspan=3><nobr>{$smarty.const.ADMIN_NO_SPECIAL_OFFERS}</nobr></td>
</tr>
{/section}
</table>
<p>
<input type=hidden name="dpt" value="catalog">
<input type=hidden name="sub" value="special">
<input type=hidden name="save_offers" value="yes">
<input type=submit value="{$smarty.const.SAVE_BUTTON}">
</form>
<!-----------------------begin random offers------------------------------>
</td><td width="50%" valign="top" bgcolor="#FFFFFF">
{* random offers on the homepage *}
<br><br><br>{$smarty.const.ADMIN_RANDOM_OFFERS_DESC}
<p></p>
<form action="admin.php" method=POST>
<table cellpadding=5 cellspacing=0 width="100%">
<tr class="mainmenu_selected_{$current_dpt}">
<td><div align="left">{$smarty.const.ADMIN_PRODUCT_NAME}</div></td>
<td>{$smarty.const.DELETE_BUTTON}</td>
</tr>
{section name=i loop=$random_offers}
<input type=hidden name="offer_productID_{$random_offers[i][0]}" value="{$random_offers[i][1]}">
{if $smarty.section.i.index is div by 2}<tr height="34">{else}<tr bgcolor="#D0E7FD" height="34">{/if}
<td><a href="admin.php?dpt=catalog&sub=products_categories&categoryID={$random_offers[i][3]}">{$random_offers[i][4]}</a></td>
<!--<td align=center><input type=text size=5 name="offer_sort_{*$random_offers[i][0]*}" value="{*$random_offers[i][2]*}"></td>-->
<td width="10%"><div align="center"><a href="admin.php?dpt=catalog&sub=special&delete_random_offer={$random_offers[i][0]}"><img src="images/backend/button_delete.gif" border=0 alt="{$smarty.const.DELETE_BUTTON}"></a></div></td>
</tr>
{sectionelse}
<tr bgcolor=white>
<td colspan=3><nobr>{$smarty.const.ADMIN_NO_RANDOM_OFFERS}</nobr></td>
</tr>
{/section}
</table>
<p>
<input type=hidden name="dpt" value="catalog">
<input type=hidden name="sub" value="special">
<input type=hidden name="save_random_offers" value="yes">
<!--<input type=submit value="{$smarty.const.SAVE_BUTTON}">-->
</form>
</td></tr></table>
я не стал заморачиваться с отдельным шаблоном для рандомного товара, поэтому
файл ./frontend/tmplXXX/index.tpl.html
там где хотите выводить случайный товар вставляем
PHP:
{* now show random offers *}
<p>
<center>
<table border=0 cellspacing=1 cellpadding=5>
{section name=i loop=$random_offers}
{if $smarty.section.i.index is div by 2}<tr>{/if}
<td valign=top>
<table border=0 width=100% bgcolor=#{$smarty.const.CONF_MIDDLE_COLOR} cellpadding=0 cellspacing=1>
<tr><td>
<table border=0 bgcolor=white width=100%>
<tr>
<td colspan=2><a href="index.php?productID={$random_offers[i][0]}"><img src="products_pictures/{$random_offers[i][2]}" border=0></a></td>
</tr>
<tr>
<td align=center><a href="index.php?productID={$random_offers[i][0]}">{$random_offers[i][1]}</a></td>
<td align=right><font color=red><b>{$random_offers[i][3]}</b></font></td>
</tr>
</table>
</td></tr>
</table>
В предыдущем коде тоже много лишнего, надеюсь сможете удалить самостоятельно.
по умолчанию товар будет на всех страницах, чтобы убрать с главной, для последнего блока кода нужна проверка:
PHP:
{if $main_content_template !== "home.tpl.html"}
{* now show random offers *}
<p>
<center>
<table border=0 cellspacing=1 cellpadding=5>
{section name=i loop=$random_offers}
{if $smarty.section.i.index is div by 2}<tr>{/if}
<td valign=top>
<table border=0 width=100% bgcolor=#{$smarty.const.CONF_MIDDLE_COLOR} cellpadding=0 cellspacing=1>
<tr><td>
<table border=0 bgcolor=white width=100%>
<tr>
<td colspan=2><a href="index.php?productID={$random_offers[i][0]}"><img src="products_pictures/{$random_offers[i][2]}" border=0></a></td>
</tr>
<tr>
<td align=center><a href="index.php?productID={$random_offers[i][0]}">{$random_offers[i][1]}</a></td>
<td align=right><font color=red><b>{$random_offers[i][3]}</b></font></td>
</tr>
</table>
</td></tr>
</table>
</td>
{if ($smarty.section.i.index+1) is div by 2}</tr>{/if}
{/section}
</table>
</center>
{/if}
End!