Import categories with magmi datapump - csv

This is an example of my category CSV.
I never used datapump and I can't find any sample code.
I use this code to create a category tree:
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(0);
$url = "www.xxxxxxxxx.com/category.csv";
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
$app = Mage::app('default');
$exit= 0;
$continua = 0;
$cont = 0;
$f = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $f);
curl_exec($curl);
curl_close($curl);
rewind($f);
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['id'] === $id) {
return $val['nombre'];
}
}
return null;
}
while (($data = fgetcsv($f, 10000 , ",")) !== FALSE ) {
$fila="";
foreach($data as $row) {
$fila= $fila.$row;
}
$datos = explode(";",$fila);
// ID_CATEGORY CATEGORY_NAME Father_CATEGORY
$clave = $datos[0];
$cat = $datos[1];
$padre = $datos[2];
$categoria[$cont] = array ('id'=>$clave, 'nombre'=>$cat);
$nomPad = searchForId($padre,$categoria);
}
Example Result:
[21] => Array
(
[id] => 160
[name] => Madera de Teca
)
[22] => Array
(
[id] => 163
[name] => Polyrattan Natur
)
[23] => Array
(
[id] => 165
[name] => Polyrattan / acero / cristal
)
[24] => Array
(
[id] => 166
[name] => Polyrattan / forja / cristal
)
How can use this in magmi datapump?
Do I need to import a category creator plugin? How can I combine the sample code with my own.

Related

how to create short code for data array sql

i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..
Below is my controller:
$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';
for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;
$data[] = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);
$this->excel_import_model->insert($data);
This is my result:
My question is how to re-code for my data[] array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english
$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';
$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);
//Split String into array
$list = str_split($mark);
$mlist = array();
//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;
//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);

fetch data from array and save it in database

I had this code
echo "<pre>";
print_r($data);
echo "</pre>";
through which i got the following data
Spreadsheet_Excel_Reader Object
(
[sheets] => Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 2
[numCols] => 5
[cells] => Array
(
[1] => Array
(
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
)
)
)
)
)
I needed numCols so i used the following code
echo $data->sheets[0]['numCols'];
and got the result 5
Now i wish to fetch the following values
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
and store them in the table like this
id value
1 sno
2 candidta name
3 email
4 ctc
5 location
Can anyone please tell how it can be done
$Data = array();
foreach($data->sheets[0]['cells']['1'] as $key=>$value):
$Data[] = "('".$key."','".$value."')";
endforeach;
mysql_query("INSERT INTO tbl_name(`id`,`value`) VALUES".implode(",",$Data)) or die (mysql_error());
$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
{
echo $newarray[$i];
$sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
if(!mysqli_query($con,$sql))
{
echo "Error: " . $sql. "<br>" . mysqli_error($con);
}
}

How to get the first row of a result in Yii2? (queryRow Equivalent)

how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()

Add JSON array to MYSQL

I'm trying to get specific value by array name:
<?php
$json = json_decode($_POST['json'], true);
print_r($json);
?>
I'm getting this varray:
Array
(
[0] => Array
(
[name] => pav
[value] => g
)
[1] => Array
(
[name] => ppav
[value] => f
)
[2] => Array
(
[name] => kiekis
[value] => g
)
[3] => Array
(
[name] => kaina
[value] => g
)
[4] => Array
(
[name] => ppav
[value] => f
)
[5] => Array
(
[name] => kiekis
[value] => g
)
[6] => Array
(
[name] => kaina
[value] => f
)
[7] => Array
(
[name] => ppav
[value] => g
)
)
Tried using foreach function, but cant get specific value:
foreach ($json as $key => $value) {
echo "name".$key['name']." value".$value['value']."<br />";
}
It prints all array values:
name value<br />name valueasd<br />name valueasd<br />name values<br />name values<br />name values<br />name values<br />name valuea<br />name valueasd<br />name valued<br />
But I cant select specific value by name to add to nysql. How to do that?
Following is the tested code
<?php
$json_array = array(
array('name'=>'pav', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f'),
array('name'=>'kiekis', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f')
);
echo "<pre>";
print_r($json_array);
echo "</pre>";
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
echo "<pre>";
print_r($assoc_array);
echo "</pre>";
echo "assoc_array['pav'] = ".$assoc_array['pav'];
?>
output of the code is given below and you can see that exactly same array as yours is converted into associative array, there is one problem as your array has repeated names eg. ppav or kiekis so there will only 1 index for kiekis or ppav having the latest value.
you have to recreate array
$json_array = json_decode($_POST['json'], true);
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
after this you will get $assoc_array and you can access its elements by keys.

php $json google maps parsing

I have the following code:
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
echo "<pre>"; print_r($details); echo "</pre>";
?>
The result is:
Array
(
[destination_addresses] => Array
(
[0] => 150th St, Summit, SD 57266, USA
)
[origin_addresses] => Array
(
[0] => 38591 Patterson Rd, Wanette, OK 74878, USA
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 1,299 km
[value] => 1299343
)
[duration] => Array
(
[text] => 13 hours 26 mins
[value] => 48361
)
[status] => OK
)
)
)
)
[status] => OK
)
I need to extract from $json only the distance (1,299 km).
I am not sure how to do this.
Any help is very much appreciated.
<?php $q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json);
$distance=$details->rows[0]->elements[0]->distance->text;
echo $distance; ?>