1

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?

EDIT: 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?

4
  • I know it's not what you're ultimately looking to accomplish, but that block of $foo = $row['foo']; statements can be done in one swoop with extract($row);. Commented Jun 13, 2011 at 19:15
  • 1
    you should add MYSQL_ASSOC to your mysql_fetch_array(). should look like while ( $row = mysql_fetch_array( $result,MYSQL_ASSOC ) ) {. how long do you need to "store" your data construct? Commented Jun 13, 2011 at 19:18
  • just for the page but if I need it longer i'll put it in a $_SESSION variable Commented Jun 13, 2011 at 19:21
  • @b_dubb or use mysql_fetch_assoc() instead of mysql_fetch_array() for the same effect Commented Jun 13, 2011 at 19:22

2 Answers 2

1

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
1
  • i want to store that information in a multi-dimensional array but i'm not sure how to go about doing it... something like $companyIDArray[$companyid] => $productidArray["$productid"] => $CompanyNameArray[$CompanyName] etc Commented Jun 13, 2011 at 19:12
0
 $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.

2
  • how should I store all the product information? like the industry, link, keywords, region? Commented Jun 13, 2011 at 19:32
  • Add them also into the data array. under one company name Commented Jun 13, 2011 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.