PHP/mySQL multi-dimensional array or object oriented approach?
I have data that I want to pull from a mySQL database and I want to sort it in an organized way so I can pull it later. I want to sort it in a matter where i have CompanyID $companyid: productID $productid: productName = $Product, industryName = $industry etc
so essentially I want to have:
- CompanyID 1: ProductID 1: all the info about that specific product
- CompanyID 1: ProductID 2: all the info about that specific product
CompanyID 1: ProductID 3: all the info about that specific product
CompanyID 2: ProductID 1: all the info about that specific product
- CompanyID 2: ProductID 2: all the info about that specific product
etc etc
this is the while loop that pulls all the info, and where i will be storing it in either a multi-dimensional array or some object oriented class in PHP. either way, i'm not too sure how to go about doing it.
while ($row = mysql_fetch_array($result)){
$CompanyName=$row['CompanyName'];
$companyid=$row['companyid'];
$Product=$row['Product'];
$productid=$row['productid'];
$Industry=$row['Industry'];
$Link=$row['Link'];
$Keywords=$row['Keywords'];
$region=$row['region'];}
EDIT: I want to write a multi-dimensional array to capture all the data in an orderly manner. how would I go about writing that or is that even the best solution?
开发者_如何学PythonEDIT: right now i have this in the while loop:
$companyIDArray[$companyid] = $productidArray[$productid] = $productInfoArray["CompanyName"]=$CompanyName;
and I am going to make one for each field. Is that the best way to do it?
You should be doing this in your SQL. Use an ORDER BY clause like so:
SELECT
companyid
,productid
,etc...
FROM
products
ORDER BY
companyid,
productid
$data = array();
while ($row = mysql_fetch_array($result)){
$Industry=$row['Industry'];
$Link=$row['Link'];
$Keywords=$row['Keywords'];
$region=$row['region'];}
$data[$row['CompanyName'][$row['companyid']] =
array($row['Product'],$row['productid'], $Industry,$Link,$Keywords,$region );
}
print_r($data,true);
the above is untested code but it will give you an idea atleast how to do it.
精彩评论