我正在尝试使用PHP和MySQL自动更新"Products"
页面。我使用MySQL存储产品的ID,产品名称,价格,数量等。我需要帮助PHP获取PHP来获取MySQL数据并将每个ID放入<div>
特定类中。
我以为我需要一个while()
循环来获取数据数组和一个foreach()
循环来获取ID的每个实例,但是我没有foreach()
正确地使用循环。
请原谅我的编码混乱。在寻找更有效的做事方法之前,我试图将所有概念都付诸实践。请注意,其中sql2['id']
包含多个项目。
<?php
mysql_connect('localhost', 'user', 'password');
mysql_select_db('store');
$sql1 = mysql_query('
SELECT c.id,
c.name,
c.description,
c.price,
c.quantity,
c.itemid,
c.imgname,
c.position,
(SELECT Count(t.id)
FROM topics AS t
WHERE t.parent = c.id
AND t.id2 = 1) AS topics,
(SELECT Count(t2.id)
FROM topics AS t2
WHERE t2.parent = c.id
AND t2.id2 != 1) AS replies
FROM categories AS c
GROUP BY c.id
ORDER BY c.position ASC
');
if($sql1 === false){
die(mysql_error());
}
while($sql2 = mysql_fetch_array($sql1)){
foreach($sql2['id'] as $value){?>
<div class="itemInset">
<p><?php echo $sql2['name']; ?></p>
<img src="admin/image/<?php echo $sql2['imgName']; ?>" alt="<?php echo $sql2['imgName']; ?>" />
</div><?php
}
}
?>
类别SQL:
CREATE TABLE IF NOT EXISTS `categories` (
`id` smallint(6) NOT NULL,
`name` varchar(256) NOT NULL,
`description` text NOT NULL,
`price` text NOT NULL,
`quantity` int(10) NOT NULL,
`itemID` text NOT NULL,
`cC` text NOT NULL,
`imgName` text NOT NULL,
`position` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
还有一个var_dump()
:
array(20) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(9) "PC"
["name"]=>
string(9) "PC"
[2]=>
string(58) "computer "
["description"]=>
string(58) "computer "
[3]=>
string(6) "150.00"
["price"]=>
string(6) "150.00"
[4]=>
string(4) "1000"
["quantity"]=>
string(4) "1000"
[5]=>
string(8) "PCR-1000"
["itemID"]=>
string(8) "PCR-1000"
[6]=>
string(0) ""
["imgName"]=>
string(0) ""
[7]=>
string(1) "1"
["position"]=>
string(1) "1"
[8]=>
string(1) "0"
["topics"]=>
string(1) "0"
[9]=>
string(1) "0"
["replies"]=>
string(1) "0"
}
你foreach()
如果循环是不必要的$sql2['id']
只包含一个对象。
还要为什么做一个$value = $sql2['id'];
?该$value
变量由foreach()
循环使用,该循环会自动为其分配集合的当前对象(在本例中为一个对象的集合)。foreach()
如果您甚至不使用$value
变量,为什么还要创建循环并完成我之前描述的赋值,这会增加不可理解性的原因是什么?
同样在这种情况下,这$sql2
是非常令人误解的命名,因为该对象不代表与SQL相关的事物。
如果查看mysql-fetch-array()文档,则会看到一些示例以及应如何使用它。还要注意变量的命名!您的代码应如下所示:
$result = mysql_query('
SELECT c.id,
c.name,
c.description,
c.price,
c.quantity,
c.itemid,
c.imgname,
c.position,
(SELECT Count(t.id)
FROM topics AS t
WHERE t.parent = c.id
AND t.id2 = 1) AS topics,
(SELECT Count(t2.id)
FROM topics AS t2
WHERE t2.parent = c.id
AND t2.id2 != 1) AS replies
FROM categories AS c
GROUP BY c.id
ORDER BY c.position ASC
');
while ($row = mysql_fetch_array($result)) {?>
<div class="itemInset">
<p><?php echo $row['name']; ?></p>
<img src="admin/image/<?php echo $row['imgName']; ?>" alt="<?php echo $row['imgName']; ?>">
</div>
<?php } ?>
请注意,mysql_*
函数已被弃用,您不应使用它们。
编辑:据我所知,您确实在做/认为事情真的很糟糕。您应该使用我给您的第一种技术并以<div>
这种方式创建。如果这对您不起作用,则表明您编写了错误的SQL查询,或者您的数据库设计错误。根据您的SQL SELECT
,sql2['id']
如果不是,则应该只输入一个数字,否则您的数据库设计/命名是完全错误的,更不用说如果它不是数字,而是某种字符串,那么您就不能foreach()
裸露的绳子,等待某种奇迹发生,这将生成您想要的整个布局。在这一点上,我不得不说,如果我给您的第一个解决方案没有用,那么您就必须重新考虑数据库以及如何从这些表中插入/选择内容的方式,因为我认为您对以下内容有误解您应该如何工作。
您从中获得的每一行while ($row = mysql_fetch_array($result))
应代表一个类别,而类别表的列应代表一个类别的属性,以及从$row
数组中获得的每个对象。因此,当您从$row
数组中获取一个值时,您应该使用属性名称(如果未在SQL查询中重命名该列的名称)来对其进行索引,那么您应该获得一个值。例如,$row['name']
应包含该特定类别的名称,$row['id']
应为该特定类别的ID。您无法在其中进行foreach()
循环,除非它们包含类似的内容,否则"auto,mobile,car"
您必须explode()
先对其进行循环。并重复自己的ID应该是一个具体的,特定类别的唯一编号,如1
,2
,56
或468
它不能包含其他/更多内容,因为在这种情况下,您的命名和有关ID的概念是错误的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句