我已经试着弄清楚这一点有一段时间了,这是PHP的新手,所以这可能很容易。我有一个数组,我现在手动填充,但我想根据mysql表中的数据使其动态。
我的静态数组如下:
$Prod1Array = [ ['NA', 'None'], [10001, 'Prod1 Item1'], [10011, 'Prod1 Item2'], [10002, 'Prod1 Item3'] ];
我的mysql表很简单,包含两列Product_code和Description。值如下所示:
10000, 'Product 1'
10001, 'Prod1 Item1'
10011, 'Prod1 Item2'
10002, 'Prod1 Item3'
10100, 'Product 2'
10101, 'Prod2 Item1'
etc...
所以我想得到的是一个数组,它看起来像这样:
$Products = [Product1Name
['NA', 'None'],
[10001,'Prod1 Item1'],
[10011,'Prod1 Item2'],
[10002,'Prod1 Item3']
,
Product2Name
['NA', 'None'],
[10101,'Prod2 Item1'],
[10102,'Prod2 Item2']
];
(也许我没有正确地想象出来,但希望你能明白)基本上,它是一个带有Product名称的顶级数组,以及Products项的子数组。
根据给出的评论和答案,这就是我现在所拥有的:
$queryp = "select Description, Product_code from Products where Product_code like '___00' AND Product_code < 19999";
$resultp = queryMysql($queryp);
while($arr=mysqli_fetch_array($resultp))
{
$CODE = substr($arr[1],0,3) . '__';
$str1 = "select Product_code, Description from Products where Product_code like'$CODE' AND Product_code <> $arr[1]";
$result1 = queryMysql($str1);
while($arr1=mysqli_fetch_array($result1))
{
$arr[] = array($arr1);
}
}
echo "<pre>";print_r($arr);echo"</pre>";
多亏了"Ronser",我已经很接近了,但我仍然缺少一些东西,因为最后的打印在数组中没有显示任何内容。
在代码的后面,我使用数组在我的表单中构建了一个选项列表,它与静态数组配合得很好,但我当然想让代码更动态,如果我添加了一个新产品,或更改了mysql表中的描述,它将立即反映在表单中。
现在,我的表单也有产品列的静态标题,其想法是遍历数组的第一级以获得列标题,然后在表单表行中,我遍历不同的选项以创建列表(这对我的静态数组很有效)。
我知道这是一个冗长的问题,感谢所有试图回答甚至阅读这篇文章的人。
使用sql非常简单创建一个产品表(产品id、名称等)
和
项目表(用于存储项目及其id、描述等)
在项目表中使用fkpid这样的列(在这里存储产品选项卡中的产品的pid)
一旦完成
使用此
$con = db_connect(); //first try this then add joins to the query.
$str = "select pid, pname from Products";
$result = mysql_query($str,$con);
while($arr=mysql_fetch_array($result))
{
echo "The product name is: ".$arr[1]. " and its items are:";
$str1 = "select iid, iname, idescription from items
where fkpid = ".$arr[0];
$result1 = mysql_query($str1,$con);
while($itemsss=mysql_fetch_array($result1))
{
echo "<br>item name: ". itemsss[1];
echo "<br>item description". itemsss[2]; // use `nl2br(itemsss[2])` inorder to have page breaks.
}
}