Store into a database table as row the query result - php

query result
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
..........
I want to store this results into another table as row inventory.
the table will look like this:
ID inventory
1 (the result)
2 (another result)
And after I will query it back again so that I can display the result.
here's what I have done lately.
store:
//getting the result
$inv = $this->db->get_from('table','id'=>'1')->row();
<input type="hidden" name="inventory" value="<?php print_r($inv)?>">
//storing in the new table
$this->db->insert('table2',array('inventory'=>$this->input->post('inventory')));
getting:
$inventory = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
//result
array
(
[ID] => 1
[inventory] =>
array
(
[0] => stdClass Object
(
[ingredientID] => 2
...... and so on
I want to display everything in the array['inventory'] which is an array of objects.
I've done this
foreach($arr['inventory'] as $invent):
echo $invent['ingredientID'];
but there's an error in the foreach part.
error: Invalid argument supplied for foreach()
What should i do?
endforeach;

assuming:
$results = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
you should use this to print it
foreach($results['inventory'] as $inventory)
{
print_r($inventory->ingredientID);
}

Related

How to fetch data from json string using php

I have json string and I want to fetch product data from this string How can I achieve this. Please some one help me.
Below is my string,
{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}
There is a mix of JSON and PHP-serialized data.
<?php
$string = '{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}';
$dataObject = json_decode($string);
foreach($dataObject->rows as $row){
$productData = unserialize($row->setting);
print_r($productData);
}
Will result in
Array
(
[name] => Featutred
[product] => Array
(
[0] => 145
[1] => 148
)
[limit] => 5
[width] => 200
[height] => 200
[status] => 1
)
Array
(
[name] => Featured Products
[product] => Array
(
[0] => 145
[1] => 146
)
[limit] => 4
[width] => 200
[height] => 200
[status] => 1
)
NOTE: There is no error checking in code above, since it is written for your particular example. If you are not sure that your input data is correct (that is usual), you need to check if JSON is ok, object is object and also has needed properties, etc.

Insert auto generated multidimensional array to database

I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.

Looping through array and changing value with if statement

I have the following array of data from a DB and would like to change the STATUS value to a string if it matches criteria
Array ( [0] => stdClass Object ( [ORDER] => 1321[DATE] => 2015-10-05 [TEXT] => TESTING [OTIME] => 06:03:03 [STATUS] => 3 [CODE] => ABC) [1] => stdClass Object ([ORDER] => 1321[DATE] => 2015-10-05 [TEXT] => TESTING [OTIME] => 06:03:03 [STATUS] => 3 [CODE] => ABC ) )
I want to loop through the data and do and do something like below:
if (STATUS=3) {
STATUS ="replace the number with some text";
}
Lets say your output is stored in $results array so now you can iterate over loop and check like this:
<?php
for($i=0; $i<count($results);$i++){
if($results[$i]->STATUS == "3"){
$results[$i]->STATUS = "text";
}
}
?>

Codeigniter: Referencing a specific array index returned in model result

I'm still trying to wrap my head around passing db query results from a model back to controller and finally to a view. I seem to be getting the data to the right place, I'm just not sure how to best access the resulting array of objects in the view.
Specifically, I'm trying to query my db for the most recent 7 distinct dates that someone has submitted a link. I get back an array of dates, and then for each of those dates I do a query for all links submitted on that date and store the results in an array. Then in the view, for each of those distinct dates, I show a header (the date), immediately followed by the links associated with it.
The array that come from my distinct date query ($link_headers):
Array (
[0] => stdClass Object([added_date] => 2011-08-11)
[1] => stdClass Object([added_date] => 2011-05-03)
[2] => stdClass Object([added_date] => 2011-04-21)
[3] => stdClass Object([added_date] => 2011-04-10)
[4] => stdClass Object([added_date] => 2011-03-04)
[5] => stdClass Object([added_date] => 2011-02-28)
[6] => stdClass Object([added_date] => 2011-02-22)
)
The array that comes from my query for actual links submitted ($links_result):
Array
(
[0] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1178
[link_url] => http://www.amazon.com/Silicone-Rubber-CASSETTE-Design-IPHONE/dp/B004YDJWOY
[link_name] => Silicone Skin BLACK CASSETTE TAPE
[link_notes] => iPhone case... probably won't fit in my dock.
[added_date] => 2011-08-11
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[1] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1177
[link_url] => http://snorby.org/
[link_name] => Snorby - Snort front-end
[link_notes] =>
[added_date] => 2011-05-03
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[2] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1176
[link_url] => http://www.nytimes.com/2011/04/17/business/17excerpt.html?_r=4&pagewanted=1&ref=business
[link_name] => Corner Office - The 5 Habits of Highly Effective C.E.O.s
[link_notes] => Sounds a lot like what Nathanial said...
[added_date] => 2011-04-21
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[3] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1175
[link_url] => http://chezlarsson.com/myblog/2010/06/panduro-concrete-challenge-3.html
[link_name] => Concrete book-ends
[link_notes] => Cool look...
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 4
[public] => 1
)
[1] => stdClass Object
(
[user_id] => 2
[link_id] => 1174
[link_url] => http://themeforest.net/item/reciprocity-photo-blog-gallery/154590
[link_name] => Site Templates - Reciprocity - Photo Blog
[link_notes] =>
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 5
[public] => 1
)
)
[4] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1173
[link_url] => http://lifehacker.com/#!5771943/the-always-up+to+date-guide-to-jailbreaking-your-ios-device
[link_name] => The Always Up-to-Date Guide to Jailbreaking Your iOS Device
[link_notes] =>
[added_date] => 2011-03-04
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[5] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1172
[link_url] => http://lifehacker.com/#!5754463/how-to-jailbreak-your-ios-421-device
[link_name] => How to Jailbreak Your iOS 4.2.1 Device
[link_notes] =>
[added_date] => 2011-02-28
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[6] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1171
[link_url] => http://www.bitplumber.net/2010/10/a-cassandra-hardware-stack-dell-c1100s-ocz-vertex-2-ssds-with-sandforce-arista-7048s/
[link_name] => A Cassandra Hardware Stack
[link_notes] =>
[added_date] => 2011-02-22
[flag_new] => 1
[rating] => 3
[public] => 1
)
)
)
... all seems fine enough. But my problem comes from my view, where I'm trying to build the HTML as described above. A simplified view of the code I'm trying to get to work is as follows:
foreach ($link_headers as $header) {
echo "INDEX: ". $links_headers .", ADDED DATE: ". $header->added_date ."<BR>";
foreach ($links_result[$link_headers] as $result){
echo $result->added_date ."<BR>";
echo $result->link_name ."<BR><BR>";
}
}
So, I'm trying to use the index of the first one to tell my foreach loop which index of the second array to loop through and get the content. Clearly I'm misusing the $links_result[$link_headers] variable(s) but I left it in to show what I was trying to do.
Any help is very much appreciated!
Michael
I dont use CodeIgniter but whether within th framework or from PHP i would just grab it all in one go and then the issue with the indexes becomes moot:
SELECT * FROM model_table mt WHERE mt.added_date IN (
SELECT DISTINCT md.added_date from model_table md
ORDER BY md.added_date DESC
LIMIT 7
) ORDER BY mt.added_date DESC
That should get you an array of models ordered by date limted to the 7 most recent dates. So then its just a matter of choosing when to display a header:
$current = null;
foreach($links as $link) {
if($link->added_date !== $current) {
// show the header and set current to the current date
$current = $link->added_date;
echo 'HEADER: Added on ' . $current . '<br />';
}
echo $row->added_date ."<BR>";
echo $row->link_name ."<BR><BR>";
}

Remove duplicates from multi-dimensional array based on higher points

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:
Array
(
[basketball] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 30
)
[1] => stdClass Object
(
[id] => 314
[username] => slights
[points] => 20
)
[2] => stdClass Object
(
[id] => 123
[username] => gibb54
[points] => 5
)
)
[soccer] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 95
)
[1] => stdClass Object
(
[id] => 49
[username] => sans
[points] => 65
)
[2] => stdClass Object
(
[id] => 122
[username] => peano
[points] => 50
)
[3] => stdClass Object
(
[id] => 174
[username] => fordb
[points] => 30
)
[4] => stdClass Object
(
[id] => 112
[username] => danc
[points] => 30
)
)
)
As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.
I would need to do this continually until no user be the 0 value for any of the primary array values twice.
I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.
Any ideas please?!
I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.
To create the de-duped array, use something like:
$deDupedData = array();
foreach ($data as $sport => $users) {
foreach ($users as $user) {
if (isset($deDupedData[$user->id])) {
if ($user->points > $deDupedData[$user->id]->points) {
$deDupedData[$user->id]->sport = $sport;
$deDupedData[$user->id]->points = $user->points;
}
} else {
$modifiedUser = $user;
$modifiedUser->sport = $sport;
$deDupedData[$user->id] = $modifiedUser;
}
}
}
// Now reconstruct your array...

Categories