I am starting with the answer on this page PHP/mySQL - how to fetch nested rows into multidimensinal array because I am working with the same kind of relationship between tables. My code now looks like this:
$query=sprintf("SELECT * FROM product INNER JOIN production ON product.ID = production.product_ID");
$data = mysql_query($query);
$products = array();
while ($row = mysql_fetch_assoc($data))
{
if (!isset($products[$row['product_ID']]))
{
$row['production'] = array();
$products[$row['product_ID']] = $row;
}
$products[$row['product_ID']]['production'][] = $row;
}
foreach ($products as $product)
{
foreach ($product['production'] as $item)
//... do stuff
}
The query is solid - I have tried this directly on the database.
My problem is that I can't work out the final bit where it says //... do stuff
No matter what combination of variables etc I stick in there I get ArrayArrayArray (etc). What I want is to get a table of output which I can work with.
I hope this is enough information, this is my first question on StackOverflow. Thank you.
When I do var_dump($item) I get a long display which looks like this:
array(9) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(1) "4" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(3) "130" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "21" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "705" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "23" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "26" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "28" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(2) "12" } array(9) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(1) "5" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "10" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "14" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "140" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "16" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "220" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "17" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-04" ["quantity"]=> string(3) "120" } array(9) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(1) "6" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "11" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "13" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-10-10" ["quantity"]=> string(2) "75" } array(9) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(1) "7" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(2) "12" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" } array(9) { ["ID"]=> string(1) "1" ["product_name"]=> string(11) "Thighs 12kg" ["order"]=> string(3) "105" ["hidden"]=> string(1) "1" ["id"]=> string(1) "8" ["product_ID"]=> string(1) "1" ["date"]=> string(10) "2012-10-01" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(9) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "29" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "30" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "31" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "32" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" }
You are going to construct your table's html inside your last two foreach loops. I would recommend naming your columns better as the text that identifies your array keys in item is confusing (you have three ID fields).
print "<table><thead><tr><th>ID</th><th>Product</th><th>Order</th><th>Hidden</th><th>ID</th><th>Product ID</th><th>date</th><th>Quantity</th></tr></thead><tbody>";
foreach ($products as $product)
{
foreach ($product['production'] as $item)
{
print "<tr>";
print "<td>$item['ID']</td>";
print "<td>$item['product_name']</td>";
print "<td>$item['order']</td>";
print "<td>$item['hidden']</td>";
print "<td>$item['id']</td>";
print "<td>$item['product_id']</td>";
print "<td>$item['date']</td>";
print "<td>$item['quantity']</td>";
print "</tr>";
}
}
print "</tbody></table>";
Related
I have an array like this:
array(53) { [0]=> string(2) "11" [1]=> string(1) "1" [2]=> string(2) "11" [3]=> string(1) "7" [4]=> string(2) "11" [5]=> string(1) "7" [6]=> string(2) "10" [7]=> string(2) "10" [8]=> string(1) "9" [9]=> string(1) "8" [10]=> string(1) "8" [11]=> string(2) "12" [12]=> string(1) "6" [13]=> string(2) "10" [14]=> string(2) "10" [15]=> string(2) "12" [16]=> string(1) "2" [17]=> string(1) "3" [18]=> string(1) "8" [19]=> string(1) "5" [20]=> string(1) "4" [21]=> string(1) "3" [22]=> string(1) "2" [23]=> string(1) "2" [24]=> string(1) "3" [25]=> string(1) "7" [26]=> string(1) "3" [27]=> string(1) "6" [28]=> string(1) "9" [29]=> string(1) "3" [30]=> string(1) "3" [31]=> string(2) "12" [32]=> string(2) "12" [33]=> string(1) "1" [34]=> string(2) "12" [35]=> string(1) "5" [36]=> string(1) "2" [37]=> string(1) "8" [38]=> string(1) "6" [39]=> string(1) "9" [40]=> string(1) "9" [41]=> string(2) "10" [42]=> string(1) "1" [43]=> string(1) "2" [44]=> string(1) "3" [45]=> string(2) "10" [46]=> string(1) "7" [47]=> string(1) "4" [48]=> string(2) "11" [49]=> string(2) "12" [50]=> string(1) "1" [51]=> string(1) "9" [52]=> string(1) "9" }
I want to every value in this array to have two character length, if array value is:9 to change to 09.
With this I can`t achieve... somewhere I missed something
foreach ($outMonth as $key => $value) {
if (strlen($outMonth[$value]) == 1 ) {
$outMonth[$value] = "0".$value;
//echo (strlen($outMonth[$value])). "<br>";
}
}
If I dump, I got:
array(53) { [0]=> string(2) "11" [1]=> string(2) "01" [2]=> string(2) "11" [3]=> string(2) "03" [4]=> string(2) "11" [5]=> string(2) "05" [6]=> string(2) "10" [7]=> string(2) "10" [8]=> string(2) "08" [9]=> string(2) "09" [10]=> string(3) "010" [11]=> string(2) "12" [12]=> string(3) "012" [13]=> string(2) "10" [14]=> string(2) "10" [15]=> string(2) "12" [16]=> string(1) "2" [17]=> string(1) "3" [18]=> string(1) "8" [19]=> string(1) "5" [20]=> string(1) "4" [21]=> string(1) "3" [22]=> string(1) "2" [23]=> string(1) "2" [24]=> string(1) "3" [25]=> string(1) "7" [26]=> string(1) "3" [27]=> string(1) "6" [28]=> string(1) "9" [29]=> string(1) "3" [30]=> string(1) "3" [31]=> string(2) "12" [32]=> string(2) "12" [33]=> string(1) "1" [34]=> string(2) "12" [35]=> string(1) "5" [36]=> string(1) "2" [37]=> string(1) "8" [38]=> string(1) "6" [39]=> string(1) "9" [40]=> string(1) "9" [41]=> string(2) "10" [42]=> string(1) "1" [43]=> string(1) "2" [44]=> string(1) "3" [45]=> string(2) "10" [46]=> string(1) "7" [47]=> string(1) "4" [48]=> string(2) "11" [49]=> string(2) "12" [50]=> string(1) "1" [51]=> string(1) "9" [52]=> string(1) "9" }
You can see that only few values are changed...
Thanks your time!
You should use the key ($outMonth[$key]) for accessing the array element and change the value
foreach ($outMonth as $key => $value) {
if (strlen($value) === 1 ) {
$outMonth[$key] = "0".$value;
}
}
You are giving wrong index while updating array value:
foreach ($outMonth as $key => $value) {
if (strlen($value) == 1 ) {
$outMonth[$key] = "0".$value; //<-----------Need to add $key as index
//echo (strlen($outMonth[$value])). "<br>";
}
}
What you want is the str_pad function, check out the documentation. It will left or right pad a string with another string to a given length.
foreach ($outMonth as $key => $value) {
$outMonth[$key] = str_pad($value, 2, '0', STR_PAD_LEFT);
}
Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.
i have an array in nested set model https://en.wikipedia.org/wiki/Nested_set_model, And i need convert the array to json parent-children with infinite levels.
Array example:
array(6) {
[0]=>
array(8) {
["title"]=>
string(5) "cat 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "128"
["lft"]=>
string(1) "1"
["rgt"]=>
string(1) "4"
["depth"]=>
string(1) "0"
}
[1]=>
array(8) {
["title"]=>
string(5) "sub 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "129"
["lft"]=>
string(1) "2"
["rgt"]=>
string(1) "3"
["depth"]=>
string(1) "1"
}
[2]=>
array(8) {
["title"]=>
string(5) "cat 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "130"
["lft"]=>
string(1) "5"
["rgt"]=>
string(2) "10"
["depth"]=>
string(1) "0"
}
[3]=>
array(8) {
["title"]=>
string(5) "sub 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "131"
["lft"]=>
string(1) "6"
["rgt"]=>
string(1) "9"
["depth"]=>
string(1) "1"
}
[4]=>
array(8) {
["title"]=>
string(5) "sub 3"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value3"
["id"]=>
string(3) "132"
["lft"]=>
string(1) "7"
["rgt"]=>
string(1) "8"
["depth"]=>
string(1) "2"
}
[5]=>
array(8) {
["title"]=>
string(4) "item"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "133"
["lft"]=>
string(2) "11"
["rgt"]=>
string(2) "12"
["depth"]=>
string(1) "0"
}
}
json example:
[{"name":"Cat 1","value":"value1","children":[{"name":"sub 2", "value":"value2","children":[{"name":"sub 3", "value":"value 3"}]}]}, {"name":"Cat 2","value":"value 2","children":[{"name":"sub 1", "value":"value1"}]},{"name":"item","value":"value1"}]
can someone help me create a function to do the Convert?
json_encode should be the answer here http://php.net/manual/en/function.json-encode.php
First sorry for my English.
I have a weird problem using the php $_SESSION Object.
I spent 2 days without find the solution.
I am triying to save a multidimensional array that stores some post values.
I create an array with this values and then i create a multidimensional array containing this arrays.
Page 1 (setvalues in multi array ans save in session)-> GET -> Page 2 read the session.
My code Page 1:
First, save the post values into a object property and validate them.
public function validateData(){
$this->nombres=$_POST["nombre"];
$this->imp_nombres=$_POST["imp_nombre"];
$this->numeros=$_POST["numero"];
$this->imp_numeros=$_POST["imp_numero"];
$this->tallas=$_POST["talla"];
$this->cantidades=$_POST["cantidad"];
$this->productos=$_POST["products"];
$this->equipos=$_POST["equipo"];
if(WSI_Funtions::compareSizes($this->nombres,$this->imp_nombres,$this->numeros,$this->imp_numeros,$this->tallas,$this->cantidades,$this->productos,$this->equipos))
{
$this->isValidModel=true;
$this->saveProductsValues();
}
else{
$this->isValidModel=false;
$this->errorMessage="Los datos no son correctos. Los parametros no coinciden";
}
}
If data is ok, I save those values:
public function saveProductsValues()
{
$this->productsValues=array();
$this->productsValues["names"]=$this->nombres;
$this->productsValues["imp_nombres"]=$this->imp_nombres;
$this->productsValues["numeros"]=$this->numeros;
$this->productsValues["imp_numeros"]=$this->imp_numeros;
$this->productsValues["tallas"]=$this->tallas;
$this->productsValues["cantidades"]=$this->cantidades;
$this->productsValues["productos"]=$this->productos;
$this->productsValues["equipos"]=$this->equipos;
}
Then I save it in a session :
public function saveSessionValues()
{
$_SESSION['customer'] = $this->customerObject;
$_SESSION['productsValues'] =$this->productsValues;
echo var_dump($_SESSION['productsValues']);
}
The saveSessionValues echo print this:
array(8) { ["names"]=> array(12) { [0]=> string(0) "" [1]=> string(12) "ÁNGEL HDEZ." [2]=> string(11) "VUJASINOVIC" [3]=> string(4) "ABIA" [4]=> string(10) "MUTAKABBIR" [5]=> string(8) "PETROVIC" [6]=> string(5) "DOBOS" [7]=> string(4) "HOMS" [8]=> string(6) "CASTRO" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["imp_nombres"]=> array(12) { [0]=> string(0) "" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["numeros"]=> array(12) { [0]=> string(1) "7" [1]=> string(1) "8" [2]=> string(1) "9" [3]=> string(0) "" [4]=> string(2) "11" [5]=> string(2) "12" [6]=> string(2) "18" [7]=> string(2) "19" [8]=> string(2) "22" [9]=> string(1) "5" [10]=> string(2) "33" [11]=> string(0) "" } ["imp_numeros"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(0) "" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(0) "" } ["tallas"]=> array(12) { [0]=> string(4) "XXXL" [1]=> string(3) "XXL" [2]=> string(2) "XL" [3]=> string(3) "XXL" [4]=> string(3) "XXL" [5]=> string(3) "XXL" [6]=> string(4) "XXXL" [7]=> string(3) "XXL" [8]=> string(2) "XL" [9]=> string(4) "XXXL" [10]=> string(2) "XL" [11]=> string(3) "XXL" } ["cantidades"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(3) "145" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(1) "1" } ["productos"]=> array(12) { [0]=> string(3) "109" [1]=> string(3) "109" [2]=> string(3) "109" [3]=> string(3) "109" [4]=> string(3) "109" [5]=> string(3) "109" [6]=> string(3) "109" [7]=> string(3) "109" [8]=> string(3) "109" [9]=> string(3) "109" [10]=> string(3) "109" [11]=> string(3) "109" } ["equipos"]=> array(12) { [0]=> string(7) "LEB ORO" [1]=> string(7) "LEB ORO" [2]=> string(7) "LEB ORO" [3]=> string(7) "LEB ORO" [4]=> string(7) "LEB ORO" [5]=> string(7) "LEB ORO" [6]=> string(12) "ES TALLA 4XL" [7]=> string(7) "LEB ORO" [8]=> string(7) "LEB ORO" [9]=> string(12) "ES TALLA 4XL" [10]=> string(7) "LEB ORO" [11]=> string(7) "LEB ORO" } }
My code Page 2 (php tags ommitted):
if (!isset($_SESSION)) { session_start(); }
echo var_dump($_SESSION['productsValues']);
This echo print the next value:
array(8) { ["names"]=> NULL ["imp_nombres"]=> NULL ["numeros"]=> NULL ["imp_numeros"]=> NULL ["tallas"]=> NULL ["cantidades"]=> NULL ["productos"]=> NULL ["equipos"]=> NULL }
The first level array exists, because the subarrays keys are printed, but all the second level arrays are NULL..
Maybe is for use the $_POST value??
I had try to encode the values of the $_POST object, saving only a json string instead of saving a object with the same result, the first nodes in the JSON are the keys of the arrays but the values are "NULL"
Any help please??
Thanks!!
You are over writing the values. Try with -
$_SESSION['productsValues'][] = $this->productsValues;
i'm using codeigniter for create a web site. and i need to take result from mysql table as
this
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(12) ["type"]=> int(0) }
but always i got as a result something efferent from this.
when i'm using result_object() in codeigniter i'm getting something like this
array(12) { [0]=> object(stdClass)#24 (4) { ["id"]=> string(1) "1" ["label"]=> string(15) "Web Development" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [1]=> object(stdClass)#25 (4) { ["id"]=> string(2) "10" ["label"]=> string(19) "Sales and Marketing" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [2]=> object(stdClass)#26 (4) { ["id"]=> string(1) "7" ["label"]=> string(18) "Design and Artwork" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [3]=> object(stdClass)#27 (4) { ["id"]=> string(1) "2" ["label"]=> string(16) "Content Creation" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [4]=> object(stdClass)#28 (4) { ["id"]=> string(1) "4" ["label"]=> string(19) "OSCommerce projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [5]=> object(stdClass)#29 (4) { ["id"]=> string(1) "3" ["label"]=> string(8) "PHP Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [6]=> object(stdClass)#30 (4) { ["id"]=> string(1) "5" ["label"]=> string(22) "Technical Writing Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [7]=> object(stdClass)#31 (4) { ["id"]=> string(1) "6" ["label"]=> string(13) "Forum Posting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [8]=> object(stdClass)#32 (4) { ["id"]=> string(1) "8" ["label"]=> string(20) "Blog Design Projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [9]=> object(stdClass)#33 (4) { ["id"]=> string(1) "9" ["label"]=> string(24) "Freelance Website Design" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [10]=> object(stdClass)#34 (4) { ["id"]=> string(2) "11" ["label"]=> string(29) "Internet Marketing Consulting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } [11]=> object(stdClass)#35 (4) { ["id"]=> string(2) "12" ["label"]=> string(25) "Leads Generation Services" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } }
$this->db->order_by('parent_id','id','ASC');
$query = $this->db->get('dyn_menu');
if ($query->num_rows() > 0) {
$data = $query->result_object();
return $data;
}
how can i get the result as i want?
try using just result(), like:
...
$data = $query->result();
return $data;
...
try this if single record you want, make $is_single = true
if($is_single)
return $query ->row_array();
else
return $query ->result_array();