PHP read array declaration from file? - bash

I've got a text file with an array declaration (actually an array of arrays) that I'm reading into my PHP using 'file_get_contents'. The PHP code is treating the contents that it reads from the file as a literal string, rather than creating an array. I need to actually create the array.
Example:
The file 'probe2.csv' contains the following text:
array(array(22, 296), array(43, 667), array(64, 1008), array(84, 1273), array(105, 1520), array(126, 1899), array(146, 2149))
My PHP is:
$s1 = array(array(22, 256), array(43, 524), array(64, 797), array(84, 1133), array(105, 1515), array(126, 1813), array(146, 2128));
$s2 = file_get_contents('probe2.csv');
print_r($s1);
echo "<p>";
print_r($s2);
echo "</p>";
The output is:
Array ( [0] => Array ( [0] => 22 [1] => 256 ) [1] => Array ( [0] => 43 [1] => 524 ) [2] => Array ( [0] => 64 [1] => 797 ) [3] => Array ( [0] => 84 [1] => 1133 ) [4] => Array ( [0] => 105 [1] => 1515 ) [5] => Array ( [0] => 126 [1] => 1813 ) [6] => Array ( [0] => 146 [1] => 2128 ) )
array(array(22, 296), array(43, 667), array(64, 1008), array(84, 1273), array(105, 1520), array(126, 1899), array(146, 2149))
I'm a bash guy, not a PHP guy, so I have no idea how to do this.

To make it work, you should you need to change it like this:
<?php
$s2 = array(array(22, 296), array(43, 667), array(64, 1008), array(84, 1273), array(105, 1520), array(126, 1899), array(146, 2149));
And then, in you main php file, just include it:
include('probe2.php');
If you have no control about the probe2 file, you can do this (it's bad practice, but works):
$temp = file_get_contents('probe2.csv');
eval('$s2 = ' . $temp . ';');
$s2 will have the right array from here.

Related

how to extract a specific link value (page number) from a pagination in codeigniter?

i would like to extract the current link value from my codeigniter pagination in the controller and put it in a own variable.
the usual pagination code...
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
with print_r($data["links"]); i printed the array to check what i find inside.
result:
( Array ( [0] => [1] => 1234NextLast › )
( Array ( [0] => Previous1 [1] => 2345NextLast › )
( Array ( [0] => Previous12 [1] => 3456NextLast › ) and so on
i would like to extract the current page number and add it to a different variable.
... [1] => 1
... [1] => 2
... [1] => 3 and so on
You can get current page number from the url:
$this->uri->segment(3);
If it is in the third segment, you can count the segments or i prefer this:
$segment_array = $this->uri->segment_array();
$last_segment = end($segment_array);
And last segment should be your current page number.

Echo from foreach

i have model that return
return array(
'senin'=> $senin->result(),
'selasa'=> $selasa->result(),
'rabu'=> $rabu->result()
);
and controller
$data['ot'] = $this->genbamodel->getOt();
in my view, i use 'foreach $ot as key' and when i tried to print_r($key) the result like this
Array()
Array ( [0] => stdClass Object ( [id_genba] => 1 [hari] => Tuesday [shift1] => 2 [shift2] => 0 ))
Array ( [0] => stdClass Object ( [id_genba] => 1 [hari] => Wednesday [shift1] => 1 [shift2] => 2 ))
how to get just shift1 = 2 and shift2 = 0 (from second array / hari=>Tuesday ??
Thankyou
I don't think you need the foreach loop to access those values. To get just the second record, you can do the below.
$ot['selasa'][0]->shift1
$ot['selasa'][0]->shift2
$ot['selasa'][0]->hari

Storing booking items information in session array of codeigniter

I wanted to store booking items such as
--> pid =1, qty=3
--> pid=5, qty=4
and so on
in codeigniter session.
I am finding it difficult and i am running out of deadline. Helping hands are very much appreciated.
I have made an example special for you. Suppouse it will helps
$data = array();
for ($i = 1; $i<=5; $i++)
{
$data[] = array('pid' => $i, 'qty' => $i);
}
$this->session->set_userdata('order_data', $data);
and output your result will be shown in session like these :
[order_data] => Array
(
[0] => Array
(
[pid] => 1
[qty] => 1
)
[1] => Array
(
[pid] => 2
[qty] => 2
)
[2] => Array
(
[pid] => 3
[qty] => 3
)
[3] => Array
(
[pid] => 4
[qty] => 4
)
[4] => Array
(
[pid] => 5
[qty] => 5
)
)
If you want to call only these data (booking data) then call
$this->session->userdata['order_data']
you can free use it in foreach for view like
foreach ($this->session->userdata['order_data'] as $key => $value)
{
}

Failed tests after installing EcomDev_PHPUnit

For setting up EcomDev_PHPUnit, I installed PHPUnit and relevant modules via PEAR and when I ran "phpunit" for a magento instance, out of 32 tests, 14 were failures and all the failed tests have a similar pattern. Any clues on how to fix this would be helpful.
=== Example 1:
EcomDev_PHPUnitTest_Test_Lib_Constraint_Config_Resource_Script::testGetVersionScriptsDiff with data set "to_1.0.5_one_invalid" ('directory_one_invalid', 'scheme', NULL, '1.0.5')
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
## ##
Array (
'expected' => Array (
0 => 'install-1.0.0.php'
1 => 'upgrade-1.0.0-1.0.1.php'
2 => 'upgrade-1.0.1-1.0.5.php'
0 => 'install-1.0.5.php' ) 'actual' => Array (
0 => 'upgrade-1.0.0-1.0.1.php' ) )
=== Example 2
EcomDev_PHPUnitTest_Test_Lib_Constraint_Config_Resource_Script::testParseVersions with data set "directory_one_invalid" ('directory_one_invalid')
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
## ##
Array (
'scheme' => Array (
'install' => Array ()
'upgrade' => Array (
0 => Array (...) ) ) 'data' => Array (...) )
=== Example 3:
EcomDev_PHPUnitTest_Test_Lib_Constraint_Config_Resource_Script::testGetVersionScriptsDiff with data set "from_1.0.0_one" ('directory_one_valid', 'scheme', '1.0.0', NULL)
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
## ##
Array (
'expected' => Array (
0 => 'install-1.0.0.php'
- 1 => 'upgrade-1.0.0-1.0.1.php'
)
'actual' => Array (
- 0 => 'install-1.0.0.php'
- 1 => 'upgrade-1.0.0-1.0.1.php'
)
)

Magento - How to set media gallery images programmatically?

I try to set a new magento product image e.g. che_3.png with:
$visibility = array (
'thumbnail',
'small_image',
'image'
);
$product->addImageToMediaGallery( $file, $visibility, true, false);
The product image is moved from the
temporary directory: /media/tmp/catalog/product/upload/sessionid/original/che_3.png
to the
destination directory: /media/catalog/product/c/h/che_3.png
Now, if i want to call the image filepath - that is saved by magento - is wrong. The media_gallery object looks like this:
[media_gallery] => Array
(
[images] => Array
(
[0] => Array
(
[value_id] => 89
[file] => /c/h/che_3_2_1.png //che_3.svg is already in here
[label] =>
[position] => 1
[disabled] => 0
[label_default] =>
[position_default] => 1
[disabled_default] => 0
)
[1] => Array // why this second item?
(
[value_id] => 88
[file] => /c/h/che_3_2.png
[label] =>
[position] => 1
[disabled] => 0
[label_default] =>
[position_default] => 1
[disabled_default] => 0
)
)
[values] => Array
(
)
)
As you can see the filename is extended by magento and also two items are added to the media_gallery.
My question is how can I reset the product respectively the internal counter so magento is using the original file as it is?
Thanks in advance.
Problem solved: I've saved the product twice with $product->save() and it seems $product->addImageToMediaGallery() is also executed twice.

Resources