raymond spark
raymond spark

Reputation: 49

php codeigniter array undefined offset error

i am using codeigniter, and i am declaring a variable array, i used the array and it worked fine, but php keep showing some error that does't affect the program. anyone know what's happen ?? this is my code :

view :

<?php
$banyak = 0;
$id = $results->id;
$barangNama = $results->nama;
$barangHarga = $results->harga;
$barangGambar = $results->gambar; //resulting string "1.jpg,2.jpg,3.jpg,4.jpg"

$s = 0;
//declare variable array
$newGambar = array();

for ($i = 0; $i < strlen($barangGambar); $i++) {
    if ($barangGambar[$i] == ',') {
        $s++;
    } else {
        /* here is line 90 */ $newGambar[$s] .= $barangGambar[$i];
    }
}

print_r($newGambar);
?>

<?php
$w = 0;
for ($i = 0; $i <= $s; $i++) {


    if ($w % 4 == 0) {
        $banyak++;
    }
    $w++;
    echo $banyak;
    ?>
    <div class="item <?php if ($i == 0) {
        echo "active";
    } ?>" data-thumb="<?php echo $banyak - 1; ?>">
        <img src="<?php echo base_url(); ?>assets/images/product_picture/<?php echo $newGambar[$i]; ?>">
    </div>

    <?php
}
?>

what i want to achieve is divided $barangGambar each coma, and input to $newGambar[0] = 1.jpg $newGambar[1] = 2.jpg and so on

but i have to loop to decide how many picture that provided

this is error msg :

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined offset: 0
   Filename: webView/webProductZoomed.php
   Line Number: 90

Upvotes: 0

Views: 1074

Answers (4)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

I'm not sure about your question but this'll work for you

$barangGambar = "1.jpg,2.jpg,3.jpg,4.jpg";

if(!empty($barangGambar)){
    $newGambar = explode(',',$barangGambar);
}

print_r($newGambar);

DEMO

Upvotes: 1

user4904106
user4904106

Reputation:

remove . from this line and remove $s from array index

$newGambar[] = $barangGambar[$i];

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14862

You wish to separate the string by comma and store the filenames in a new array? ie. turn "1.jpg,2.jpg,3.jpg,4.jpg" into array("1.jpg","2.jpg","3.jpg","4.jpg") ?

Replace:

for ($i = 0; $i < strlen($barangGambar); $i++) {
    if ($barangGambar[$i] == ',') {
        $s++;
    } else {
        /* here is line 90 */ $newGambar[$s] .= $barangGambar[$i];
    }
}

with:

$newGambar = explode(',',$barangGambar);

explode will split a string by the delimiter (in this case the comma ,) and store each part in the resulting array.

As for why undefined offset error:

During the first loop $newGamber[0] equals null (does not exist). As such attempting to concatenate onto it will return an undefined offset error. It is only a value has been placed into $newGamber[0] the error will stop.

Upvotes: 1

Damien Pirsy
Damien Pirsy

Reputation: 25435

You're doing something strange here:

$newGambar[$s] .= $barangGambar[$i];

Maybe you meant: $newGambar[] = $barangGambar[$i]; ?

There's no need for a counter here, since every element added to the array will automatically receive an incremented index, so you'll still end up with $newGambar => array (0 => 'element1', 1 => 'element2') and so on

Upvotes: 1

Related Questions