4

I have a table which stores the courses registered by students during a semester and session. I populated it with course-codes for a particular student. Here is the function that fetches the course-code below:

function get_course_code(){
    global $connection;

    $query = "SELECT DISTINCT course_code ";
    $query .= "FROM tbl_registered_courses_400 ";
    $query .= "WHERE matric_no = '03/55ec101' LIMIT 10";

    $course_code_set = mysql_query($query, $connection);
    confirm_query($course_code_set);
    return $course_code_set;
}

When I called the function in my main page. It returned the following result

$courseCode = get_course_code();
$courseFilter = '';
while ($courseList = mysql_fetch_array($courseCode)){
    $courseList['course_code']; 
    $courseFilter .= "\""."{$courseList['course_code']}"."\",";
    $courseFilter;
}

$course = array($courseFilter);

print_r($course);

Array ( [0] => "PHY 432","CSC 491","CHM 401","CHM 402","MAT 451","MAT 452","CSC 423","BLY 401", )

I want to split the $course array into an array that will have the values of the string in the above to read

array(
    [0] => PHY 432
    [1] => CSC 491
    [2] => CHM 401
    [3] => CHM 402
    .
    .
    .
    e.t.c
)

The string data in the $course array is from the course_code column in the database. My intention is to use the results of the new array to form a row in the database that will hold the results of each matric_no for different courses done for the semester/session. I would appreciate any help I can get to get this done.

1
  • 1
    as a side note, you should switch from mysql to mysqli or PDO as it is becoming depreciated! Commented Aug 7, 2012 at 12:19

1 Answer 1

5

create the array and then assign the values to that array.

$courseCode = get_course_code();
$course = array();
while ($courseList = mysql_fetch_array($courseCode)){
    $course[] = $courseList['course_code'];
}

print_r($course);
Sign up to request clarification or add additional context in comments.

3 Comments

Whao...Thanks a million. I would want you to be my mentor. Wouldn't know if I can use this medium to communicate you personally?
I answer here on my small spare time and, believe me, you will find very good and helping people here :). Glad I helped.
Then I would be a regular visitor to StackOverFlow. Once again...Thanks a bunch!

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.