JAMES JAMES - 7 months ago 13
PHP Question

How to use array in PHP language

I have this code but when I run it it give me error ( Notice: Undefined offset: 1 in C:\wamp\www\test3.php on line

$str[$row['term_no']] += ",".$row['code'];
)

How I can solve this problem?

<?php

$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM `degree_plan` LEFT JOIN courses ON
degree_plan.course_number=courses.course_number
where major='COE'";

$result = mysqli_query($con , $q) ;
if($result){

$str = array();
while($row = mysqli_fetch_array($result))
{
{
$str[$row['term_no']] += ",".$row['code'];
}
foreach ($str as $key => $value)
{

echo $value;
echo $key;
}

}
}

?>

Answer

In your code, you are puting all codes in one element which is never defined.

change this:

$str[$row['term_no']] += ",".$row['code'];

to this:

$str[$row['term_no']] = $row['code'];
Comments