3

If I stored values as S5001:31,32,33|S5002:42,44,46|S5003:21,23,25... etc.

in database, how to retrieve the values using implode function so that I can display the marks separately to find total for each registered number in HTML table?

$internalmark_col="mark_internal";
        $student_internal_marks_array = array();
        if(isset($student_internal_mark->$internalmark_col))
        {
            $internalmarks_stud_code_arr=explode(',',$student_internal_mark->$internalmark_col);
            foreach($internalmarks_stud_code_arr as $marks_each)
            {
                $internalcode_mark=explode(':',$marks_each);
                if(isset($internalcode_mark[0]) && isset($internalcode_mark[1]))
                    $student_internal_marks_array[$internalcode_mark[0]]=$internalcode_mark[1];
            }

        }

        print_r($student_internal_marks_array);
        exit;

For above code I got wrong output. Desired output is:

Register No | Mark 1 | Mark 2 |Mark 3 | Total 
-------------------------------------

S5001       | 3 | 2 | 3 | 8

S5002       |4 | 4 |6 | 14

S5003       |1 |3 |5 | 9
3
  • Show us your code Commented Nov 25, 2016 at 9:00
  • Is your data stored using serialize ? Commented Nov 25, 2016 at 9:04
  • what output do you getting as now. Commented Nov 25, 2016 at 9:07

4 Answers 4

2

From below code you will get desired output:

$data = "S5001:31,32,33|S5002:42,44,46|S5003:21,23,25";
    $d = explode("|",$data);
    foreach ($d as $value) {
        $register_no_arr = explode(":",$value);
        $register_no = $register_no_arr[0];
        $marks = explode(',',$register_no_arr[1]);
        echo '<br>Register No :- '.$register_no;
        for($i=0;$i<sizeof($marks);$i++){
            echo "<br>Mark ".($i+1)." :- ".$marks[$i];
        }

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

Comments

2

You can use preg_split to read the row params:

$str = 'S5001:31,32,33|S5002:42,44,46|S5003:21,23,25';


$table = '<table>
<tr>
<th>Register No</th>
<th>Mark 1</th>
<th>Mark 2</th>
<th>Mark 3</th>
<th>Total</th>
</tr>';

$internalmarks_stud_code_arr=explode('|', $str);
foreach($internalmarks_stud_code_arr as $marks_each)
{   
    list($num, $m1, $m2, $m3) = preg_split('/(:|,)/',$marks_each);
    $table .= '
        <tr>
            <td>'.$num.'</td>
            <td>'.$m1.'</td>
            <td>'.$m2.'</td>
            <td>'.$m3.'</td>
            <td>'.($m1 + $m2 + $m3).'</td>
        </tr>';

}
$table .='</table>';         
print $table;

preg_split is a core function that allows you to explode strings using a regex. In this case I already know that the sum of the elements separated by : and , is fixed (4 items), so I can use list core function as well that saves the result of the split in four defined variables ($num, $m1, $m2, $m3)

list($num, $m1, $m2, $m3) = preg_split('/(:|,)/',$marks_each);

The regex I used /(:|,)/ is made up by / which are php regex delimiters and a group syntax between round brackets (). The pipe into group syntax means "or" so this (:|,) means "find : OR , ". Since my replacement pattern is quite easy and there's no need to use groups it can also be written as a range like this: /[,:]/ and It's even a better choice

2 Comments

I think it will be better if you can give a small explanation about the regex you used, may be helpful for new regex user to understand regex clearly.
You're right! I added more information about my snippet, hope It's more clear now
1
$tot =0;
$internalmark_col = S5001:31,32,33|S5002:42,44,46|S5003:21,23,25;
$m = explode('|',$internalmark_col);
echo '<tr>';
foreach($m as $mark){
    $scode = explode(':',$mark);
    echo '<td>'.$scode[0].'</td>';
    $code = $scode[1];
    $smark = explode(',',$code);
    foreach($smark as $student_mark){
        echo '<td>'.$student_mark.'</td>';
        $tot += $student_mark;
    }
    echo '<td>'.$tot.'</td>';
}
echo '</tr>';

Comments

0

Try this:

$array = 'S5001:31,32,33';
$str = str_replace(":", ",", $array);

list($register, $mark1, $mark2, $mark3) = explode(',', $str);

echo "<br>Register:" . $register . "<br>Mark1:" . $mark1 . "<br>Mark2:" . $mark2 . "<br>Mark3:" . $mark3;

Comments

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.