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').
Related
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++;
}
I am having a little trouble adding validators to a ZF2 form object. I an building a form from a schema set in a database so I can validate a set of user input quickly.
This is the code that generates the form object
//initialise the form
$form = new Form();
//need to loop through the schema to create the form
for($i=0; $i < count($schema); $i++)
{
$form_options = array();
//add the basics to the form
$form_options['name'] = $schema[$i]['field_name'];
$form_options['type'] = $schema[$i]['field_type'];
//check if this is a required field
if($schema[$i]['is_required'] == 'true')
{
$form_options['required'] = true;
}
//add functions to filter the input form
$function_filters = explode(',', $schema[$i]['function_filter']);
if(!empty($function_filters))
{
$filters = array();
for($j=0; $j < count($function_filters); $j++)
{
$filters = array('name' => $function_filters[$j]);
}
$form_options['filters'] = $filters;
}
//add validators to the field array
$validators = array();
if(!is_null($schema[$i]['min_length']) && !is_null($schema[$i]['max_length']))
{
$validators[] = array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => (int) $schema[$i]['min_length'],
'max' => (int) $schema[$i]['max_length'],
)
);
}
//our regex validator if it exists
if(!is_null($schema[$i]['regex_filter']) || strlen($schema[$i]['regex_filter']) != 0)
{
$validators[] = array(
'name' => 'regex',
'options' => array(
'pattern' => $schema[$i]['regex_filter'],
'messages' => array(
\Zend\Validator\Regex::INVALID => $schema[$i]['regex_invalid'],
\Zend\Validator\Regex::NOT_MATCH => $schema[$i]['regex_not_match'],
\Zend\Validator\Regex::ERROROUS => $schema[$i]['regex_errorus'],
)
)
);
}
//only add the validators if theres something in there
if(!empty($validators))
{
$form_options['validators'] = $validators;
}
$form->add($form_options);
}
//return our form object
return $form;
The block of code behaves the way I expect it, the output of $form_option after it's performed the above code looks like this:-
Array
(
[name] => username
[type] => Text
[required] => 1
[filters] => Array
(
[name] => StripTags
)
[validators] => Array
(
[0] => Array
(
[name] => StringLength
[options] => Array
(
[encoding] => UTF-8
[min] => 3
[max] => 50
)
)
[1] => Array
(
[name] => regex
[options] => Array
(
[pattern] => /^[a-zA-Z0-9_]{3,50}$/
[messages] => Array
(
[regexInvalid] => L_REGEX_INVALID
[regexNotMatch] => L_REGEX_USERNAME
[regexErrorous] => L_REGEX_ERRORUS
)
)
)
)
)
When I came to test it, it ignores the StringLength & Regex validators entirely, and only pay attention to the required validator for the last form element.
Anyone have any idea what's gone wrong?
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.
I just teach Symfony and Doctrine.
I don't know hot to fetch 1 row array instead of multirow array in Doctrine.
I using this:
$q = $this->createQuery('a')
->innerJoin('a.Translation t')
->andWhere('t.lang = ?', $language)
->andWhere('t.name LIKE ?', 'somename%');
return $q->execute(array(), Doctrine::HYDRATE_RECORD);
And then i get something like:
Array
(
[0] => Array
(
[id] => 1
[created_at] => 2012-03-19 17:40:52
[updated_at] => 2012-03-21 17:44:04
[created_by] => 1
[updated_by] => 1
[Translation] => Array
(
[en] => Array
(
[id] => 1
[name] => somename
[lang] => en
[slug] => somename
)
)
)
)
But i need
Array
(
[0] => Array
(
[id] => 1
[created_at] => 2012-03-19 17:40:52
[updated_at] => 2012-03-21 17:44:04
[created_by] => 1
[updated_by] => 1
[id] => 1
[name] => somename
[lang] => en
[slug] => somename
)
)
Some body knows how i can make it?
I use just
$q = $this->createQuery('a')
->select('a.id as id, t.name as name')
->leftJoin('a.Translation t')
->where('t.name LIKE ?', $name.'%')
->andWhere('t.lang = ?', $language);
Have you tried to explicit add a select with columns you need?
$q = $this->createQuery('a')
->select('a.id, a.created_at, a.updated_at, a.created_by, a.updated_by, t.id, t.name, t.lang, t.slug')
->innerJoin('a.Translation t')
->andWhere('t.lang = ?', $language)
->andWhere('t.name LIKE ?', 'somename%');
return $q->execute(array(), Doctrine::HYDRATE_ARRAY);
Using the WITH keyword?
$q = $this->createQuery('a')
->innerJoin('a.Translation t WITH t.lang = ?', $language)
->where('t.name LIKE ?', 'somename%');
return $q->execute(NULL, Doctrine::HYDRATE_ARRAY);
I have a link on a view like this
Save Playlist
However, when it is clicked I am redirected to
/playlists/add/video[]=0&video[]=4&video[]=1
and the output of $this->params['url'] is
Array ( [url] => playlists/add/video[]=0 [video] => Array ( [0] => 4 [1] => 1 ) )
instead of
Array ( [url] => playlists/add/ [video] => Array ( [0] => 0 [1] => 4 [2] => 1 ) )
I can't work out why the firs parameter is always read as part of the url, nor why the leading ? is removed
Try playlists/add?video -> playlists/add/?video
Or just write properly formatted URLs like
$this->Html->link('Save Playlist', array('controller' => 'playlists', 'action' => 'add', 'values[0]'=>3, 'values[1]'=>2, 'values[2]'=>23)); ?>