Codeigniter active records with join 4 table - php

guys i have 4 table uhd_voucher_menu, uhd_menu, uhd_voucher_variant_menu, uhd_variant_menu
which uhd_voucher_menu and uhd_menu is connected by menu_id, than uhd_voucher_variant_menu and uhd_voucher_menu is connected by voucher_menu_id and the last, uhd_variant_menu and voucher_variant_menu is connected by variant_menu_id
this is my code how i get the data :
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,uhd_variant_menu.variant_name,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$res = $this->db->get()->result_array();
from that active recored will be return to this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(4) "Sour"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(14) "Red Crown Crab"
["qty"]=>
string(1) "2"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(8) "Omellete"
["qty"]=>
string(1) "3"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
as you see the golden_salmon with same voucher_menu_id is return twice
i want if the voucher_menu_id is same, so from this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
i want that data return to become this :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
array(2){
[1]=>
string(5) "Spicy"
[2]=>
string(6) "Medium"
}
["voucher_menu_id"]=>
string(1) "3"
}
same as the other golden_salmon with same voucher_menu_id 4
i want the variant_name is become array so the if data with same voucher_menu_id will not call twice
guys can you help me how to execute the data as i want?
thank you (:

You have to use group by, I slightly modified you query:
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,GROUP_CONCAT(uhd_variant_menu.variant_name SEPARATOR ',') AS variants,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$this->db->group_by("uhd_voucher_menu.voucher_id");
$res = $this->db->get()->result_array();
we can convert the comma seperated values to array after query output.
Try and post the result...

Related

php array update values if string length is equal to 1

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);
}

combine some data in 1 arrray if in same id on multidimesensional array

I have a multidimensional array $voucher_data with this following array :
array(6) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "2"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "1"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
In my array [0] and [1] the voucher_menu_id is 3. They have same voucher_menu_id and also [2] and [3] is 4
So i want to combine the variant_menu_id become 1 array become like this :
array(4) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
array(2){
string(1) "2"
string(1) "6"
}
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
array(2){
string(1) "1"
string(1) "4"
}
["voucher_menu_id"]=>
string(1) "4"
}
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
Guys can you help me how to combine it?
Thank you (:
Just like the comment above, just use your voucher_menu_id as your key into assigning the new container. Use that so that it'll be group when you loop:
$new_array = array();
foreach ($voucher_data as $v) {
if(empty($new_array[$v['voucher_menu_id']])) { // if empty initialize
$new_array[$v['voucher_menu_id']] = $v; // base array group
$new_array[$v['voucher_menu_id']]['variant_menu_id'] = array(); // intialize container
}
// push id
$new_array[$v['voucher_menu_id']]['variant_menu_id'][] = $v['variant_menu_id'];
}
$new_array = array_values($new_array); // array reindex
Untested, but something like this might work:
$output = array();
foreach ($array as $entry) {
// make the variant_menu_id an array
$entry['variant_menu_id'] = (array) $entry['variant_menu_id'];
$key = $entry['voucher_menu_id'];
if (!array_key_exists($key, $output)) {
// create a new entry in the output array using the entire $entry
$output[$key] = $entry;
continue;
}
// otherwise, add your variant menu ID to the existing values
$output[$key]['variant_menu_id'] = array_merge($output[$key]['variant_menu_id'], $entry['variant_menu_id']);
}
This will essentially create a new array by grouping entries by their voucher_menu_id, and adding variant_menu_id values into an array in the $output.

Select all rows from table in Yii

I want get all rows of ref table : ciudad
public function actionGetCiudades(){
$model = ciudad::model()->findAll();
$lCiudades = array();
$i=0;
$dataReader = Yii::app()->db->createCommand( 'SELECT * FROM ciudad' )->query();
foreach( $dataReader as $row ) {
$ciudad = new ciudad;
echo $row->ciudadid.":".$row->estadoid.":".$row->ciudaddsc.":".$row->activo;
echo "<br/";
unset( $ciudad );
}
Unfortunately, output is :
:::
But I have 700+ rows in my table.
What's wrong???
UPDATE :
var_dump($dataReader) -->
array(4) { ["ciudadid"]=> string(1) "1" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(15) "VALLE DE MEXICO" ["activo"]=> string(1) "1" } [1]=> array(4) { ["ciudadid"]=> string(1) "2" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(6) "TOLUCA" ["activo"]=> string(1) "1" } [2]=> array(4) { ["ciudadid"]=> string(1) "3" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(11) "ATLACOMULCO" ["activo"]=> string(1) "1" } [3]=> array(4) { ["ciudadid"]=> string(1) "4" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(14) "AGUASCALIENTES" ["activo"]=> string(1) "1" } [4]=> array(4) { ["ciudadid"]=> string(1) "5" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(18) "SAN JOSE DE GRACIA" ["activo"]=> string(1) "1" } [5]=> array(4) { ["ciudadid"]=> string(1) "6" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(19) "PABELLON DE ARTEAGA" ["activo"]=> string(1) "1" } [6]=> array(4) { ["ciudadid"]=> string(1) "7" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(8) "CALVILLO" ["activo"]=> string(1) "1" } [7]=> array(4) { ["ciudadid"]=> string(1) "8" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(19) "ENCARNACION DE DIAZ" ["activo"]=> string(1) "1" } [8]=> array(4) { ["ciudadid"]=> string(1) "9" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(15) "RINCON DE ROMOS" ["activo"]=> string(1) "1" } [9]=> array(4) { ["ciudadid"]=> string(2) "10" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "ENSENADA" ["activo"]=> string(1) "1" } [10]=> array(4) { ["ciudadid"]=> string(2) "11" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(7) "TIJUANA" ["activo"]=> string(1) "1" } [11]=> array(4) { ["ciudadid"]=> string(2) "12" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "MEXICALI" ["activo"]=> string(1) "1" } [12]=> array(4) { ["ciudadid"]=> string(2) "13" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "ROSARITO" ["activo"]=> string(1) "1" } [13]=> array(4) { ["ciudadid"]=> string(2) "14" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(6) "TECATE" ["activo"]=> string(1) "1" } [14]=> array(4) { ["ciudadid"]=> string(2) "15" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(7) "LA MESA" ["activo"]=> string(1) "1" } [15]=> array(4) { ["ciudadid"]=> string(2) "16" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(6) "LA PAZ" ["activo"]=> string(1) "1" } [16]=> array(4) { ["ciudadid"]=> string(2) "17" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(9) "LOS CABOS" ["activo"]=> string(1) "1" } [17]=> array(4) { ["ciudadid"]=> string(2) "18" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(7) "COMONDU" ["activo"]=> string(1) "1" } [18]=> array(4) { ["ciudadid"]=> string(2) "19" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(14) "CABOS AN LUCAS" ["activo"]=> string(1) "1" } [19]=> array(4) { ["ciudadid"]=> string(2) "20" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(17) "SAN JOSE DEL CABO" ["activo"]=> string(1) "1" } [20]=> array(4) { ["ciudadid"]=> string(2) "21" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(19) "CIUDAD CONSTITUCION" ["activo"]=> string(1) "1" } [21]=> array(4) { ["ciudadid"]=> string(2) "22" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(8) "CAMPECHE" ["activo"]=> string(1) "1" } [22]=> array(4) { ["ciudadid"]=> string(2) "23" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(17) "CIUDAD DEL CARMEN" ["activo"]=> string(1) "1" } [23]=> array(4) { ["ciudadid"]=> string(2) "24" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(9) "ESCARCEGA" ["activo"]=> string(1) "1" } [24]=> array(4) { ["ciudadid"]=> string(2) "25" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(7) "CALAKUL" ["activo"]=> string(1) "1" } [25]=> array(4) { ["ciudadid"]=> string(2) "26" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(7) "CALKINI" ["activo"]=> string(1) "1" }
Use queryAll() instead of query(). query() adds limit 1 to the sql query and gets only first row matching criteria.
As the Question title is generic let me post a generic answer to the question.
i am using Yii 2.
to get All rows from a table without applying any where class here is how you will get.
$dbusers = New Users;
$users = $dbusers->find()->all();
Your model should look like this.
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Users extends ActiveRecord
{
public function rules()
{
return [
[['name', 'id','email','type'], 'required'],
['id', 'numeric'],
];
}
}
Final Code :
public function actionGetCiudades(){
$lCiudades = array();
$dataReader = Yii::app()->db->createCommand( 'SELECT * FROM ciudad' )->queryAll();
foreach( $dataReader as $row ) {
$ciudad->ciudadid = $row['ciudadid'];
$ciudad->estadoid = $row['estadoid'];
$ciudad->ciudaddsc = $row['ciudaddsc'];
$ciudad->activo = $row['activo'];
array_push($lCiudades, $ciudad);
unset( $ciudad );
}
$json = json_encode($lCiudades);
echo $json;
}

Reading PHP array

I thought to access the value of '1' here, I would need to the following
$data['category'][0];
["category"]=> array(1) { [0]=> string(1) "1" }
But it doesn't return anything. How would I access the string(1) section to get the value 1?
Here is the full var dump of the variable $data array
array(18) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_id_48"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_ft_48"]=> string(4) "none" ["revision_post"]=> array(26) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["author_id"]=> string(1) "1" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["title"]=> string(3) "asd" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["category"]=> array(1) { [0]=> string(1) "1" } ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_id_48_hidden_file"]=> string(0) "" ["field_id_48_hidden_dir"]=> string(1) "1" ["field_id_48"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_ft_48"]=> string(4) "none" ["allow_comments"]=> string(1) "y" ["entry_date"]=> string(10) "1380114180" ["status"]=> string(4) "open" ["previous_status"]=> bool(false) } }
To access that data you have to use $data['revision_post']['category'][0] because category array is in revision_post array.
The value of $data['category'][0] is an array. So you would access like this:
$data['category'][0] = array('1');
echo $data['category'][0][0];
Try it for yourself by copying and pasting here http://writecodeonline.com/php/

Nested MySQL query - getting Array as output

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

Categories