I am getting array data from a function ReportsController::getStudentYearwiseAvgHeightsProvince() in the below format:
Array ( [0] => Array ( [name] => Baluchistan [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) [1] => Array ( [name] => KPK [data] => Array ( [0] => 56 [1] => 58 [2] => 58 [3] => 0 [4] => 0 [5] => 0 [6] => 60 ) ) [2] => Array ( [name] => Punjab [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 78 [4] => 90 [5] => 90 [6] => 0 ) ) [3] => Array ( [name] => Sindh [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) )
ReportsController::getStudentYearwiseAvgHeightsProvince() function is:
public function getStudentYearwiseAvgHeightsProvince() {
$result = Yii::$app->db->createCommand ( '
SELECT temp.name AS name,
GROUP_CONCAT(IFNULL(temp.height,0) order by year) AS data
FROM ( SELECT years.year AS year, p.name AS NAME, FLOOR(AVG(sd.height)) AS height
FROM (
SELECT DISTINCT year FROM student_detail ORDER BY year
) years
cross join province p
left join student_detail sd on years.year = sd.year and sd.province_id = p.id
GROUP BY years.year, p.name
ORDER BY years.year )
AS temp
GROUP BY name; ' )->queryAll ();
$i = 0;
foreach ($result as $innerArray) {
$result[$i]['data'] = explode(",", $result[$i]['data']);
//$result[$i]['name'] = $innerArray['name'];
$i++;
}
//$result = array_map(function($var){ return (int) $var['data']; }, $result); // extraction from 2 level associative arry to 1-d associative array
print_r ( $result );
return $result;
}
and I am setting highcharts component as follows:
echo Highcharts::widget([
'scripts' => [
'modules/exporting',
'themes/grid-light',
],
'options' => [
'title' => ['text' => 'Student Province-wise Yearly Avg Heights'],
'plotOptions' => [
'column' => [
'depth' => 25
]
],
'xAxis' => [
'categories' => ReportsController::getDistinctCols("student_detail", "year")
],
'yAxis' => [
'min' => 0,
'title' => ['text' => 'Avg Height']
],
'series' =>
ReportsController::getStudentYearwiseAvgHeightsProvince()
]
]);
No graph is generated :(
I fixed the graph not showing issue by just converting the STRING ARRAY to INTEGER ARRAY which is returned by EXPLODE in my function getStudentYearwiseAvgHeightsProvince()
by editing the code inside FOREACH LOOP as:
foreach ($result as $innerArray) {
$result[$i]['data'] = explode(",", $result[$i]['data']);
$temp = array();
foreach ($result[$i]['data'] AS $index => $value)
$temp[$index] = (int)$value;
$result[$i]['data'] = $temp;
$i++;
}
Related
I want to insert multiple row in single table using foreach.
<pre class='code'>Array
(
[instructor_id] => 76
[vehicle_id] => 2
[arr_bookings] => Array
(
[0] => Array
(
[0] => 07-10-2016
[1] => 1:10 PM
[2] => 2:02 PM
[3] => s
[4] => s
)
[1] => Array
(
[0] => 07-10-2016
[1] => 1:15 PM
[2] => 2:01 PM
[3] => a
[4] => a
)
)
)
my result is this in print_r($result). how can i insert using foreach? any ideas about this.
You would do something like.
$sql = "INSERT INTO table_name (column_1, column_2) VALUES";
Then loop through your array like,
$last_key = end(array_keys($array));
foreach ( $array as $key => $value ) {
// Don't forget to protect against SQL injection.
$sql .= "('$value[0]', '$value[1]')";
if ( $last_key === $key ) {
$sql .= ",";
}
}
After that you should have a sql statement that looks something like INSERT INTO table_name (column_1, column_2) VALUES ('Some Value', 'Another Value'), ('foo', 'bar').
I try to insert mutiple arrays per job_id when i try this my array values out put as null value this is my store function
$input output :
array:7 [▼
"date" => "2016-01-10",
"job_id" => "2",
"charge" => array:2 [▼
0 => "ch1",
1 => "ch2"
],
"category" => array:2 [▼
0 => "se1",
1 => "se2"
],
"amount" => array:2 [▼
0 => "100"
1 => "200"
]
]
Full Code :
$input = Input::all();
$charge = Input::get('charge');
$date = Input::get('date');
$job = Input::get('job_id');
$amount = Input::get('amount');
$cat = Input::get('category');
$id = \Auth::id();
$data = array();
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$key['charge'],
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$key['amount'],
'category'=>$key['category'],
];
}
dd($data);
When i try to get out put i will get this out put
array:2 [▼
0 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
1 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
]
What i want is i want insert data to my database any one suggest me good idea ?
Answer for this question from Laracast Check this link
Your final output looks correct, try to use the following code to insert the $data to your DB :
Model::insert($data); // Eloquent
DB::table('table')->insert($data); // Query Builder
You should construct your array using $inputs['amount'][$key] for amount value and $inputs['category'][$key] for category, like following :
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$inputs['amount'][$key],
'category'=>$inputs['amount'][$key],
];
}
Hope this helps.
I've changed around the path structure for HybridAuth quite a bit, all of the HybridAuth files are in a login directory.
require_once('login/Auth.php');
$auth = new Hybrid_Auth(self::$settings['auth']);
Where self::$settings['auth'] is this json file
{
"debug": 1,
"database": {
"driver": "mysql",
"host": "localhost",
"port": 3306,
"name": "ws_db",
"username": "root",
"password": "",
"charset": "utf8"
},
"auth": {
"base_url": "http://localhost/login/process",
"providers": {
"Twitter": {
"enabled": true
},
"Google": {
"enabled": true,
"keys": {
"id": "",
"secret": ""
}
},
"Facebook": {
"enabled": true,
"keys": {
"id": "",
"secret": ""
},
"trustForwarded": false
},
"Steam": {
"enabled": true
}
},
"debug_mode": true,
"debug_file": "auth.txt"
}
}
I have not yet acquired keys for the providers I intended to use (is that possibly why this is happening?). I tried to login with Steam to test that it was working since it was the only provider that didn't require me to get keys.
$_SESSION['user'] = $auth->authenticate('Steam');
I was correctly redirected to a Steam login page however upon clicking Login I received the above error.
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in login\Auth.php:153 Stack trace: #0 login\Auth.php(39): Hybrid_Auth::initialize(Array) #1 index.php(83): Hybrid_Auth->__construct(Array) #2 index.php(6): Site::main() #3 {main} thrown in login\Auth.php on line 153
I thought it possibly had something to do with this line:
Hybrid_Logger::debug( "Hybrid_Auth initialize. dump used config: ", serialize( $config ) );
because that is the only line that I see serializing something but commenting that out didn't seem to work. Line 83 in my index.php is the initialization of Hybrid_Auth.
$auth = new Hybrid_Auth(self::$settings['auth']);
I can't seem to figure out what is going wrong with this though. Since that line of code is run when attempting to login and only causes a problem when returning from the Steam authorization page.
Here is the information that was output into auth.txt, I removed some information.
(
[message:protected] => Serialization of 'SimpleXMLElement' is not allowed
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => login\Storage.php
[line:protected] => 73
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => login\Storage.php
[line] => 73
[function] => serialize
[args] => Array
(
[0] => Hybrid_User Object
(
[providerId] => Steam
[timestamp] => 1431707732
[profile] => Hybrid_User_Profile Object
(
[identifier] => ***************
[webSiteURL] =>
[profileURL] => http://steamcommunity.com/id/******/
[photoURL] =>
[displayName] => Renari
[description] => <span>profile information was here</span>
[firstName] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[lastName] =>
[gender] =>
[language] =>
[age] =>
[birthDay] =>
[birthMonth] =>
[birthYear] =>
[email] =>
[emailVerified] =>
[phone] =>
[address] =>
[country] =>
[region] => location information was here
[city] =>
[zip] =>
)
)
)
)
[1] => Array
(
[file] => login\Providers\Steam.php
[line] => 37
[function] => set
[class] => Hybrid_Storage
[type] => ->
[args] => Array
(
[0] => hauth_session.Steam.user
[1] => Hybrid_User Object
(
[providerId] => Steam
[timestamp] => 1431707732
[profile] => Hybrid_User_Profile Object
(
[identifier] => ***************
[webSiteURL] =>
[profileURL] => http://steamcommunity.com/id/******/
[photoURL] =>
[displayName] => ******
[description] => <span>profile information was here</span>
[firstName] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[lastName] =>
[gender] =>
[language] =>
[age] =>
[birthDay] =>
[birthMonth] =>
[birthYear] =>
[email] =>
[emailVerified] =>
[phone] =>
[address] =>
[country] =>
[region] => location information was here
[city] =>
[zip] =>
)
)
)
)
[2] => Array
(
[file] => login\Endpoint.php
[line] => 182
[function] => loginFinish
[class] => Hybrid_Providers_Steam
[type] => ->
[args] => Array
(
)
)
[3] => Array
(
[file] => login\Endpoint.php
[line] => 55
[function] => processAuthDone
[class] => Hybrid_Endpoint
[type] => ->
[args] => Array
(
)
)
[4] => Array
(
[file] => login\Endpoint.php
[line] => 71
[function] => __construct
[class] => Hybrid_Endpoint
[type] => ->
[args] => Array
(
[0] =>
)
)
[5] => Array
(
[file] => index.php
[line] => 90
[function] => process
[class] => Hybrid_Endpoint
[type] => ::
[args] => Array
(
)
)
[6] => Array
(
[file] => index.php
[line] => 6
[function] => main
[class] => Site
[type] => ::
[args] => Array
(
)
)
)
[previous:Exception:private] =>
)
This was an issue with the Steam provider and was fixed in a commit you can download the fixed steam provider from the github repo.
As you can see the chart displays with yaxis and axis but the value for xaxis is not shown. My data looks like this,
Array
(
[series] => Array
(
[0] => Array
(
[answer_option_group_id] => 0
[name] => All
[data] => Array
(
[999] => 1
[172] => 1
[173] => 0
[174] => 77
[194] => 6
)
)
[174] => Array
(
[answer_option_group_id] => 174
[name] => Does Not Meet
[data] => Array
(
[1459dcb35a7888ab4ff3dd89c07d679e] => 7
[c424759c02fd0900e3ec7c58c745138c] => 1
[e789fa7486d59950d382b8c395de166a] => 2
[b6c632c0a07b9feaa1eefe547a1a6aa8] => 4
[2ecfa0fc3d3990a2ff30009bea9b8ae0] => 2
[b6a52c346ad6a9f4cc58ddfe3c83939e] => 8
[36894aa1fcc16e360813d1ef430bb8fa] => 6
[4284ce3f8523823a457602fe5997fcfa] => 5
[e45e137a7da3e3fe5dc3785d7d628e44] => 11
[fb9bd545acc9397e0c84a8ff5d1a3b92] => 3
[1bcace373da4bb1e821158e3a5fa80a4] => 7
[88928dea322b243c18082e23aa9bc7a4] => 1
[2e9cf454d6eb69e69e744fcc15287f49] => 3
[eaa42f3a7bf5e5f2ac3198d5f8d487a4] => 3
[690463148302dc63a0dfd327ab5253f9] => 1
[325795fe2d5f5a22136b2362ee863be4] => 4
[18bb150bdbf978e058b44ba52d836591] => 1
[418702f6d334d6531e9ac4883a690adb] => 7
[c1ebb58ebb59c5bc6bb87a567c763cfe] => 1
)
)
)
)
I used console.log() to check if I got the data right and the result looked like this,
["All", "Does Not Meet"]
[85, 77]
I tried this on having a jsfiddle and it worked but on my server it didn't. I also dont think this is about the including of scripts since it already displays the chart only that the values are not shown. Any help would be appreciated. Thanks.
I've setup a project localy using composer to autoload my vendors and modules.
This is done on a Windows XP machine running Nginx.
When I sync everything to my production server, running LAMP, the autoloader stops working and every class i call its not found.
Since this is a shared host i cant run composer.phar update to try to refresh the classmap namespace.
Anyone knows what might be happening?
+INFO:
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname(dirname($vendorDir));
return array(
'Zend\\' => $vendorDir . '/zendframework/zendframework/library/',
'ZendTest\\' => $vendorDir . '/zendframework/zendframework/tests/',
'Symfony\\Component\\Console' => $vendorDir . '/symfony/console/',
'Doctrine\\ORM' => $vendorDir . '/doctrine/orm/lib/',
'Doctrine\\DBAL' => $vendorDir . '/doctrine/dbal/lib/',
'Doctrine\\Common' => $vendorDir . '/doctrine/common/lib/',
'DoctrineORMModule\\' => $vendorDir . '/doctrine/doctrine-orm-module/src/',
'DoctrineORMModuleTest\\' => $vendorDir . '/doctrine/doctrine-orm-module/tests/',
'DoctrineModule\\' => $vendorDir . '/doctrine/doctrine-module/src/',
'DoctrineModuleTest\\' => $vendorDir . '/doctrine/doctrine-module/tests/',
'Application\\' => $baseDir . '/module/Application/src',
);
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname(dirname($vendorDir));
return array(
);
$loader returned:
Composer\Autoload\ClassLoader Object
(
[prefixes:Composer\Autoload\ClassLoader:private] => Array
(
[Zend\] => Array
(
[0] => /home/XXXX/public_html/vendor/zendframework/zendframework/library/
)
[ZendTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/zendframework/zendframework/tests/
)
[Symfony\Component\Console] => Array
(
[0] => /home/XXXX/public_html/vendor/symfony/console/
)
[Doctrine\ORM] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/orm/lib/
)
[Doctrine\DBAL] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/dbal/lib/
)
[Doctrine\Common] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/common/lib/
)
[DoctrineORMModule\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-orm-module/src/
)
[DoctrineORMModuleTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-orm-module/tests/
)
[DoctrineModule\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-module/src/
)
[DoctrineModuleTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-module/tests/
)
[Application\] => Array
(
[0] => /home/XXXX/public_html/module/Application/src
)
)
[fallbackDirs:Composer\Autoload\ClassLoader:private] => Array
(
)
[useIncludePath:Composer\Autoload\ClassLoader:private] =>
[classMap:Composer\Autoload\ClassLoader:private] => Array
(
)
)
++INFO:
spl_autoload_functions:
Array
(
[0] => Array
(
[0] => Composer\Autoload\ClassLoader Object
(
[prefixes:Composer\Autoload\ClassLoader:private] => Array
(
[Zend\] => Array
(
[0] => /home/XXXX/public_html/vendor/zendframework/zendframework/library/
)
[ZendTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/zendframework/zendframework/tests/
)
[Symfony\Component\Console] => Array
(
[0] => /home/XXXX/public_html/vendor/symfony/console/
)
[Doctrine\ORM] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/orm/lib/
)
[Doctrine\DBAL] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/dbal/lib/
)
[Doctrine\Common] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/common/lib/
)
[DoctrineORMModule\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-orm-module/src/
)
[DoctrineORMModuleTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-orm-module/tests/
)
[DoctrineModule\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-module/src/
)
[DoctrineModuleTest\] => Array
(
[0] => /home/XXXX/public_html/vendor/doctrine/doctrine-module/tests/
)
[Application\] => Array
(
[0] => /home/XXXX/public_html/module/Application/src
)
)
[fallbackDirs:Composer\Autoload\ClassLoader:private] => Array
(
)
[useIncludePath:Composer\Autoload\ClassLoader:private] =>
[classMap:Composer\Autoload\ClassLoader:private] => Array
(
)
)
[1] => loadClass
)
)
The solutions was str replacing all the \ from the namespaces with / in all the modules autoloaders.
So,this:
public function getAutoloaderConfig() {
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/', __NAMESPACE__),
),
),
);
}
will get everything working as intended on a LAMP machine.