希望这对某些人来说非常简单。
我有以下代码:
<?php
// Connects to your Database
mysql_connect("localhost", "xxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());
require("../includes/common.php");
require("admin_header.php");
require("admin_menu.php");
$query = "Truncate TABLE pt_menutitles";
$result = mysql_query($query) or die(mysql_error());
// Menu Headers for Category
$data = "select a.menuheader, a.total from(SELECT distinct (menuheader),count(*) as total FROM `pt_products` WHERE `menuheader` <> '' group by `menuheader` order by total desc limit 4) a order by a.menuheader";
$result = mysql_query($data) or die(mysql_error());
while($info = mysql_fetch_array($result))
{
$menudata = "select a.subcategory, a.menuheader,a.totcount FROM(SELECT distinct (subcategory),menuheader,count(*) as totcount FROM `pt_products` WHERE `menuheader`='".$info['menuheader']."' AND subcategory <> ''group by `subcategory` order by totcount desc limit 4) a order by a.subcategory";
$menuresult = mysql_query($menudata) or die(mysql_error());
while($menuinfo = mysql_fetch_array($menuresult))
{
$sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";
$result = mysql_query($sql) or die(mysql_error());
}
}
?>
基本上我选择了包含最多项目的前4个菜单标题,然后在标题中选择前4个子类别并将它们插入表格中。
发生的事情是,我只是将菜单标题1和子类别1到4插入到我的表格中。
好像循环在第一轮结束后结束了?
任何建议都会很棒!
干杯 克里斯
答案 0 :(得分:1)
此行替换现有结果并结束循环。使用任何其他(不存在的)变量名称,但不使用$ result。
$sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";
$result = mysql_query($sql) or die(mysql_error());