Write on $_POST an array inside DB like result - php

I have a problem on $weight_class_title.
When the data is saved it wrote in the database array.
Tk.
the result of var_dump($sql_data_array);
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 1
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 2
public function execute() {
$CLICSHOPPING_Language = Registry::get('Language');
$CLICSHOPPING_Db = Registry::get('Db');
$languages = $CLICSHOPPING_Language->getLanguages();
$QlastId = $CLICSHOPPING_Db->prepare('select weight_class_id
from :table_weight_classes
order by weight_class_id desc
limit 1');
$QlastId->execute();
$weight_class_id = $QlastId->valueInt('weight_class_id') + 1;
$weight_class_key = $_POST['weight_class_key'];
for ($i=0, $n=count($languages); $i<$n; $i++) {
$weight_class_title_array = $_POST['weight_class_title'];
$language_id = $languages[$i]['id'];
$weight_class_title_array = ['weight_class_title' => HTML::sanitize($weight_class_title_array[$language_id])];
$sql_data_array = ['weight_class_title' => $weight_class_title_array];
$insert_sql_data = ['weight_class_key' => $weight_class_key,
'weight_class_id' => $weight_class_id,
'language_id' => $languages[$i]['id']
];
$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
$this->app->db->save('weight_classes', $sql_data_array);
}

Related

Merge inner PHP arrays into one multidimensional array - No duplicates basing on Key value

I have tried a number of PHP functions like array_unique to merge the arrays I have but that does not work since these are not strings.
The starting output would be this on var_dump( $country_cities );
array (size=3)
0 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7886
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Laurence' (length=8)
'city_name_arabic' => string '3684hdfpfwbhisf' (length=15)
1 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
2 =>
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
The Code below merges the different arrays above.
<?php
// Make the cities unique. Remove duplicates form the inner array.
$uniques = [];
foreach($country_cities as $arr ) {
foreach( $arr as $v) {
if( !in_array($v, $uniques, false) ) {
$uniques[$v['country']] = $v;
}
}
}
var_dump($uniques);
Results logged show the code cuts out some of the arrays.
/Users/..... on line php:74:
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
Please help figure out my error or suggest a better way to fix it.
If I understood you correctly, this should be the result you wanted to achieve.
Each country code will be included once, while the cities (based on post_id) will only be added once.
<?php
$result = [];
$processedIds = [];
foreach ($country_cities as $ccs) {
foreach ($ccs as $cc => $details) {
// If the country has not yet been added to
// the result we create an outer array.
if (!key_exists($cc, $result)) {
$result[$cc] = [];
}
$postId = $details['post_id'];
if (!in_array($postId, $processedIds)) {
// Add the unique city to country's collection
$result[$cc][] = $details;
// Keep track of the post_id key.
$processedIds[] = $postId;
}
}
}
print_r($result);
<?php
//...
$uniques = [];
foreach($country_cities as $data ) {
foreach( $data as $countryCode => $item) {
if( !array_key_exists($countryCode, $uniques) ) {
$uniques[$countryCode] = []; //Create an empty array
}
$targetArray = &$uniques[$countryCode];
if( !array_key_exists($item['post_id'], $targetArray) ) {
$targetArray[$item['post_id']] = $item; //Create an empty array
}
}
}
Maybe this code will help?

Multiple where condition codeigniter active record

I want to make a multiple where query with AND and OR. Suppose my array is:
array (size=8)
0 => array (size=2)
'timeline.type' => string 'videos' (length=6)
'timeline.sourceId' => string '7' (length=1)
1 => array (size=2)
'timeline.type' => string 'loadshedding' (length=12)
'timeline.sourceId' => string '5' (length=1)
2 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '3' (length=1)
3 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '5' (length=1)
The above array could be dynamic. I want to make active record query like:
(timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.
I tried :
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
foreach($sources as $source){ //$sources is an array like given above
$this->db->or_where($source);
}
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);
But when I echo $this->db->last_query(); to see the query. It returns SELECT * FROM timeline only. What might be the problem. Thank you.
Your Code Should be like
$sources = array (
0 => array (
'timeline.type' => 'videos',
'timeline.sourceId' => '7' ),
1 => array (
'timeline.type' => 'loadshedding',
'timeline.sourceId' => '5' ) ,
2 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '3' ),
3 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '5')
);
end($sources); // move the internal pointer to the end of the array
$lastkey = key($sources);
$pr = "";
foreach($sources as $key=>$source)
{
if($key != $lastkey)
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ") OR ";
}
else
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ")";
}
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);

PHP Array Create from given Array

Thsi is my array structure.
$arr = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
I want to create an array like this.
array
0 =>
array
'Tablet' => string 'test1' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode1' (length=9)
'Since' => string 'mng' (length=3)
1 =>
array
'Tablet' => string 'test2' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode2' (length=9)
'Since' => string 'mng' (length=3)
Can someone pls help?
Sorry for bad english.
Thanks
$new = [];
foreach ($arr as $key => $sub) {
$i = 0;
foreach ($sub as $value) {
$new[$i][$key] = $value;
$i++;
}
}
That should do the trick :)
$newArr = array();
foreach ( $arr as $key => $subArr ){
for ($i= 0; $i < count($subArr); $i++){
if (!array_key_exists($i, $newArr)){
$newArr[$i] = array();
}
$newArr[$i][$key] = $subArr[$i];
}
}
$arr = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1','abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$na=array();
foreach($arr as $k=>$v){
foreach($v as $kk=>$vv){
$na[$kk][$k]=$vv;
}
}
echo "<pre>";print_r($na);
Easy you can append each one to normal array like this
$arr1 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$arr2 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$arr3 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$a=array();
array_push($a,arr1,arr2,arr3);
print_r($a);
output will look
array
0 =>
array
'Tablet' => string 'test1' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode1' (length=9)
'Since' => string 'mng' (length=3)
1 =>
array
'Tablet' => string 'test2' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode2' (length=9)
'Since' => string 'mng' (length=3)
$myLength = count($arr['Tablet'])
$newArray = array();
for (i=0; i < $myLength ; i++)
{
$newArray[i]['Tablet'] = $arr['Tablet'][i];
$newArray[i]['Medicine'] = $arr['Medicine'][i];
$newArray[i]['Dosage'] = $arr['Dosage'][i];
$newArray[i]['Mode'] = $arr['Mode'][i];
$newArray[i]['Since'] = $arr['Since'][i];
}
What do you meant adding the (lentgh = x) in your question?

Add three dimensional array values into a new array in PHP

I have a three dimensional array and now I am trying to parse the useful information. I have had some difficulties with this. First I will show you the structure of the json and then we'll get to my code:
//Start
array (size=1)
'coins' =>
array (size=66)
'Cachecoin' =>
array (size=13)
'tag' => string 'CACH' (length=4)
'algorithm' => string 'Chacha (Nf13)' (length=13)
'block_reward' => float 23.3
'block_time' => int 900
'last_block' => int 25263
'difficulty' => float 1.5235383
'difficulty24' => float 1.5260514266667
'nethash' => float 7270607.9696705
'exchange_rate' => float 0.00100001
'market_cap' => string '$223,995.33' (length=11)
'volume' => float 7.81123588043
'profitability' => int 610
'profitability24' => int 609
//end
What I tried to do is count the elements in the array 'coins', which is 66, and then loop over these elements while getting the useful information.
$jsonmining = file_get_contents('http://www.whattomine.com/coins.json');
$datamining = json_decode($jsonmining, true);
var_dump($datamining);
$coins = count($datamining['coins']);
echo $coins; //66
for ($i = 0; $i <= $coins; $i++) {
$summary[] = array(
'Coin' => $datamining['coins'][$i],
'Difficulty' => $datamining['coins'][$i]['difficulty'],
'Difficulty24' => $datamining['coins'][$i]['difficulty24'],
'Profitability' => $datamining['coins'][$i]['profitability'],
'Profitability24' => $datamining['coins'][$i]['profitability24']
);
}
is this what you want?
foreach ($datamining['coins'] as $ct => $cv) {
$summary[] = array(
'Coin' => $ct,
'Difficulty' => $datamining['coins'][$ct]['difficulty'],
'Difficulty24' => $datamining['coins'][$ct]['difficulty24'],
'Profitability' => $datamining['coins'][$ct]['profitability'],
'Profitability24' => $datamining['coins'][$ct]['profitability24']
);
}

Store value in multi-dimentional array on unique key

How do I display the following using the array shown below?
Pastors
key=>0, member_id, member_name
Deacons
key=>1, member_id, member_name
key=>2, member_id, member_name
Here is the array.
array (size=3)
0 =>
array (size=4)
'category_name' => string 'Pastors' (length=7)
'member_id' => string '3' (length=1)
'member_name' => string 'Tiny Marshall' (length=13)
'member_email' => string 'jconley#nowhere.com' (length=19)
1 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '1' (length=1)
'member_name' => string 'Jeremiah Conley' (length=15)
'member_email' => string 'jconley#nowhere.com' (length=19)
2 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '2' (length=1)
'member_name' => string 'Marcy Conley' (length=12)
'member_email' => string 'jconley#nowhere.com' (length=19)
Here is the code that I used to build the array:
while( $row = mysql_fetch_assoc( $result ) )
{
$staff[$i] = array
(
'category_name' => $row['category_name'],
'member_id' => $row['member_id'],
'member_name' => $row['member_name'],
'member_email' => $row['member_email'],
);
$i++;
}
This is the final solution:
$category_name = array();
foreach ($staff as $member) {
if (!in_array( $member['category_name'], $category_name ))
{
echo '<h1>' . $member['category_name'] . '</h1>';
$category_name[] = $member['category_name'];
}
echo $member['member_id'] . ', ' . $member['member_name'] . '<br />';
}
Use foreach to loop over your $staff array.
$pastors = array();
$deacons = array();
foreach ($staff as $member) {
if ($member['category_name'] == 'Pastors') {
$pastors[] = $member['member_id'] . ', ' . $member['member_name'];
} else if ($member['category_name'] == 'Deacons') {
$deacons[] = $member['member_id'] . ', ' . $member['member_name'];
}
}
documentation

Categories