所以我还没有设法解决下拉/选择方面的问题,但我已经设法回答了如何从一个查询中传递结果然后对其进行查询的问题——我还设法将结果打印在一个HTML 表格。
因此,为了查询最终将成为整个页面及其下拉列表基础的类别:
$query = "select id, name, replace(replace(arrange, 'c,', ''), '|', ',') As Arrange
from cats
where arrange like ('c,%') limit 10";
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
上面正在执行第一个查询,以准备将结果存储为字符串。
while ($row = mysql_fetch_array($result)) {
$arrange = $row['Arrange'];
}
echo $arrange;
mysql_free_result($result);
现在这是说从第一个查询中查找排列的值,回显将其打印出来,以便您可以确认返回的值。重要的部分是 $arrange 现在在代码中存储该数组。
//Query Subcategories
$query = "select id, name, Group_concat(replace(replace(arrange, 'i,', ''), '|', ',')) As Products from cats
where id in (".$arrange.")";
这是子类别查询,重要的是对 $arrange 的结果进行查询。
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_array($result)) {
$prodsarray = $row['Products'];
}
echo "<br>";
echo $prodsarray;
然后我重复了上述过程,将 Group Concat 作为字符串返回,然后可以根据上面第二个查询的第一行使用该字符串。
$query = "select p.id As ID,substring(c.name, 1, 100) As Name, c.code As Code, sum(qty) As Sold,
p.price As Price, format(p.price*1.2,2) As VAT, p.rrp As RRP, p.cost As Cost
from cart c
join prods p on c.item = p.id
and order_status = ('Processed Order')
and p.id in (".$prodsarray.")
and date_ordered < now()-interval 3 month
group by code
order by sold desc";
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
下一步是将此查询的结果回显到表中。
echo "<table class='equalDivide' cellpadding='0'cellspacing='0' width='100%' border = '3'><tr>";
echo "<td width = '5%' align='center'>ID</td>";
echo "<td width = '35%'align='center'>Name</td>";
echo "<td width = '15%' align='center'>Code</td>";
echo "<td width = '7.5%' align='center'>Sold</td>";
echo "<td width = '7.5%' align='center'>Price</td>";
echo "<td width = '7.5%' align='center'>VAT</td>";
echo "<td width = '7.5%' align='center'>RRP</td>";
echo "<td width = '7.5%' align='center'>Cost</td>";
echo "</tr></table><br>";
以上是字段名称的硬编码。
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_array($result)) {
echo "<table class='equalDivide' cellpadding='0'cellspacing='0' width='100%' border = '1'><tr>";
echo "<td width = '5%'>".$row['ID']."</td>";
echo "<td width = '35%'>".$row['Name']."</td>";
echo "<td width = '15%'>".$row['Code']."</td>";
echo "<td width = '7.5%'>".$row['Sold']."</td>";
echo "<td width = '7.5%'>".$row['Price']."</td>";
echo "<td width = '7.5%'>".$row['VAT']."</td>";
echo "<td width = '7.5%'>".$row['RRP']."</td>";
echo "<td width = '7.5%'>".$row['Cost']."</td>";
echo "</tr></table>";
}
这是用结果填充表的循环,注意行下的 ['Sold'] 是我在选择查询中声明的行。
下一步是将前两个查询变成下拉选择,然后我就完成了!繁荣!