This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
I'm trying to import data from excel to my database,
The database sucessfully inserted but an error offset : 6 show. Can someone tell me what's wrong with my array?
Controller :
$user = $this->input->post('user');
$path = "assets/excel/Book1.xls";
$this->load->library('excel_reader');
$this->excel_reader->read($path);
$data = $this->excel_reader->sheets[0] ;
$dataexcel = Array();
for ($i = 2; $i <= $data['numRows']; $i++) {
if($data['cells'][$i][2] == '')
break;
$dataexcel[$i-2]['item_id'] = $data['cells'][$i][2];
$dataexcel[$i-2]['measure_cd_from'] = $data['cells'][$i][3];
$dataexcel[$i-2]['qty_from'] = $data['cells'][$i][4];
$dataexcel[$i-2]['measure_cd_to'] = $data['cells'][$i][5];
$dataexcel[$i-2]['qty_to'] = $data['cells'][$i][6];
}
$this->prodConv->insertConvtData($dataexcel);
$this->load->view('formSukses');
Model :
function insertConvtData($dataarray)
{
for($i=0;$i<count($dataarray);$i++){
$data = array(
'item_id'=>$dataarray[$i]['item_id'],
'measure_cd_from'=>$dataarray[$i]['measure_cd_from'],
'qty_from'=>$dataarray[$i]['qty_from'],
'measure_cd_to'=>$dataarray[$i]['measure_cd_to'],
'qty_to'=>$dataarray[$i]['qty_to']
);
$this->db->insert('tb_t_product_convertion',$data);
}
}
Try to use your for loop as like that:
$j = 2; // initialize an integer with 2 for values
for ($i = 0; $i <= $data['numRows']; $i++) {
if($data['cells'][$i][2] == '')
break;
$dataexcel[$i]['item_id'] = $data['cells'][$j][2];
$dataexcel[$i]['measure_cd_from'] = $data['cells'][$j][3];
$dataexcel[$i]['qty_from'] = $data['cells'][$j][4];
$dataexcel[$i]['measure_cd_to'] = $data['cells'][$j][5];
$dataexcel[$i]['qty_to'] = $data['cells'][$j][6];
$j++;
}
Actually you want to use $i in both array key and array value and you try to use with only one incremental variable.
When your loop work on last iteration it will return you the undefined.
So, its better to use an another incremental variable for array value.
Related
How can I have same data stored as much as provided number?
Example
Store abc, 10 times
both abc and 10 are coming from form request
Code
nonuiqueAmount: 10
nonuiqueSerial: "abc"
if(!empty($request->input('nonuiqueSerial'))) {
foreach($request->input('nonuiqueAmount') as $item) { // this returns error
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
Error:
Invalid argument supplied for foreach()
You should use a for loop:
// nonuiqueAmount: 10
// nonuiqueSerial: "abc"
if (!empty($request->input('nonuiqueSerial'))) {
for ($i = 0; $i < $request->input('nonuiqueAmount', 0); ++$i) { // I've added the zero as a default value to prevent unnecessary loops
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. w3schools docs
Your nonuiqueAmount is an int. I would suggest simply stick with basic for loop
for ($x = 0; $x < $request->input('nonuiqueAmount'); $x++) {
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 7 years ago.
I have this chunk of PHP code which is giving me the error:
Warning: Illegal string offset 'mod_modulecode' in C:\xampp\htdocs\MP\multi\functions.php on line 29
This is the code that the warning is relating to:
function set_rights($menus, $menuRights) {
$data = array();
for ($i = 0, $c = count($menus); $i < $c; $i++) {
$row = array();
for ($j = 0, $c2 = count($menuRights); $j < $c2; $j++) {
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
if (authorize($menuRights[$j]["rr_create"]) || authorize($menuRights[$j]["rr_edit"]) ||
authorize($menuRights[$j]["rr_delete"]) || authorize($menuRights[$j]["rr_view"])
) {...................'
Any help would be greatly appreciated.
What PHP is saying is that at some iteration in
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
$menus[$i] is a string, not an array, and you're trying to access it like an array containing the key mod_modulecode.
To help track down this bug, I'd suggest:
if(is_string($menus[$i]) {
var_dump('string bug','i',$i,'j',$j,'$menus[$i]',$menus[$i]);
}
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
Or even better yet, if you have xdebug installed, which will show local variables when an uncaught exception is thrown: throw new Exception('string bug');
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Have some issue with returning a value in recursive function. But I can echo it.
What could be wrong with this?
function calculate($i,$count=1)
{
$str_i = (string)$i;
$rslt = 1;
for ($k=0; $k<strlen($str_i); $k++) {
$rslt = $str_i[$k]*$rslt;
}
if ( strlen((string)$rslt) > 1 ) {
$this->calculate($rslt,++$count);
} elseif ( strlen((string)$rslt) == 1 ) {
return $count;
}
}
In the if in you code the value returned in the recursive call is not used. You don't set it to a value or return it. Thus every call except the base case doesn't return a value.
Try this:
function calculate($i,$count=1)
{
$str_i = (string)$i;
$rslt = 1;
for ($k=0; $k<strlen($str_i); $k++) {
$rslt = $str_i[$k]*$rslt;
}
if ( strlen((string)$rslt) > 1 ) {
return $this->calculate($rslt,$count+1); // I changed this line
} elseif ( strlen((string)$rslt) == 1 ) {
return $count;
}
}
Now we return the value returned by the recursive call. Note I changed ++$count to $count+1 since it's bad style to mutate when using recursion.
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Here is a function in PHP that generates a random number between 1 and 15 and store that number in a array and keep calling itself to repeat until the array contains five elements, at which point, you can make it either print/echo out the elements of the array or return the array. My problem is that when I try to get it to return the array and assign it to the variable $result, I get nothing. Any ideas as to what's going on?
$storenumbers = array();
function storemynumbers($storenumbers){
$number = rand(1,15);
if(count($storenumbers) == 5){
echo "Done<br/>";
print_r($storenumbers);
//return $storenumbers;
}else{
echo $number."<br/>";
$storenumbers[] = $number;
storemynumbers($storenumbers);
}
}
//$result = storemynumbers($storenumbers);
//print_r($result);
storemynumbers($storenumbers);
Because you are only returning anything on the last run, which gets passed back to the next-to-last run, which is then dropped.
In your else block, try adding return in front of storemynumbers($storenumbers). This should pass the return value all the way back down the chain.
That said, why can't you just do:
$storenumbers = Array();
for( $i=0; $i<5; $i++) $storenumbers[] = rand(1,15);
print_r($storenumbers);
becouse you need to pass parameter by reference(& before parameter): like this:
function storemynumbers(&$storenumbers){
}
By default variables are local copy, so they not visible outside function.
missing return on the recursion. would be my guess.
$storenumbers = array();
function storemynumbers($storenumbers){
$number = rand(1,15);
if(count($storenumbers) == 5){
echo "Done<br/>";
print_r($storenumbers);
return $storenumbers;
}else{
echo $number."<br/>";
$storenumbers[] = $number;
return storemynumbers($storenumbers);
}
}
$result = storemynumbers($storenumbers);
print_r($result);
This is a easy way to create a array of $max random number between 1 and 15.
function storemynumbers($max, $start, $end) {
$store = array();
for( $i=0; $i<$max; $i++) {
$store[] = rand($start, $end);
}
return $store;
}
$result = storemynumbers(5, 1, 15);
print_r($result);
You could also use a reference to a variable
<?
for ($i=0; $i<=9; $i++) {
$b=urlencode($cl[1][$i]);
$ara = array("http://anonymouse.org/cgi-bin/anon-www.cgi/", "http%3A%2F%2Fanonymouse.org%2Fcgi-bin%2Fanon-www.cgi%2F");
$degis = array("", "");
$t = str_replace($ara, $degis, $b);
$c="$t";
$base64=base64_encode($t);
$y=urldecode($t);
$u=base64_encode($y);
$qwe = "http://anonymouse.org/cgi-bin/anon-www.cgi/$y";
$ewq = "h.php?y=$u";
$bul = ($qwe);
$degistir = ($ewq);
$a =str_replace($bul, $degistir, $ic);
}
?>
when i put $cl[1][0], $cl[1][1], $cl[1][2] works successfull but when i put $i its returning null. why is this happening?
**I'm trying to change EACH url to base64 codes that I received from remote url with preg_match_all **
Have you checked that $c1[1] has 10 elements? (From $c1[1][0] to $c1[1][9] there are 10 elements, not 9.
Maybe you are getting null for the last one $c1[1][9]. Try to do a var_dump($c1[1]) to check that it contains all the elements that you expect.
Update:
Change the this line
for ($i=0; $i<=9; $i++) {
into this
for ($i=0; $i<9; $i++) {