0

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

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

what i mean is $newGambar[$s] = $newGambar[$s] . $barangGambar[$i]; it is same as $newGambar[$s] .= $barangGambar[$i];
lets just i asking this, if i want to take a string seperated by coma, and input every string into an array and insert to different index each, am i doing it right ?
1

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.

Comments

1

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

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

1 Comment

i am trying to insert a string there, please read the code again
1

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

1 Comment

And if there's another possibilities of string than post it within your question too..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.