简体   繁体   中英

PHP array with dynamic values containing list of items to build option values later from mysql table

I've been trying to figure this out for a while, new to PHP so it may be an easy one. I have a array that I populate manually right now, but I'd like to make it dynamic based on data from mysql table.

My static array looks like this:

  $Prod1Array = [ ['NA', 'None'], [10001, 'Prod1 Item1'], [10011, 'Prod1 Item2'], [10002, 'Prod1 Item3'] ]; 

My mysql table is simple, contains 2 columns Product_code and Description. Values look like this:

10000, 'Product 1'
10001, 'Prod1 Item1'
10011, 'Prod1 Item2'
10002, 'Prod1 Item3'
10100, 'Product 2'
10101, 'Prod2 Item1'
etc...

So what I'd like to end up with is an array that looks like this:

$Products = [Product1Name
             ['NA', 'None'],
             [10001,'Prod1 Item1'],
             [10011,'Prod1 Item2'],
             [10002,'Prod1 Item3']
             ,
              Product2Name
             ['NA', 'None'],
             [10101,'Prod2 Item1'],
             [10102,'Prod2 Item2']
             ];

(Maybe I didn't visualize that properly, but hopefully you get the idea) Basically it an array with the top level with the Product names, and sub-arrays for the Products items.

Based on comments and answers given, this is what I have now:

$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>";

I'm close, thanks to "Ronser", but I'm still missing something since the print at the end shows nothing in the array.

Later in the code,I use the array to build an option list in my form, it works fine with the static array, but of course I want to make the code more dynamic where if I add a new product, or change a description in the mysql table, it will be reflected right away in the form.

Right now my form also has static headers for the columns of the products, The idea would be to iterate through the first level of the array to get the column headers, then inside the form table rows I iterate through the different options to create the list (which works fine with my static array).

I know it's a long winded question, thanks to all who try to answer or even read this.

its pretty simple with sql create a products table (product id's , names ,etc)

and

a items table (where you store items and their id's,description,etc)

use a column such as fkpid in items table(here you store the pid of the product which is in the products tabel)

once done

use this

$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.              

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM