rprp
rprp

Reputation: 1

assign array data in PHP

I did search for this but may be my search sucks. So posting it here..my code below

while( $row = mysqli_fetch_array($queryRecords) ) {
            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
                         $marksheet    =  calculatepercent(totalmark),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );

Here I call function calculatepercent(totalmark) so that the function returns a value and store that in $marksheet. But my problem is this does not work i.e I cannot store the result of calculatepercent(totalmark) because I cannot access 'totalmark'

Forget performance for a minute but how do you make this work? (If you have tips for performance that's a bonus too! ) - Thanks Coders!

RR

Upvotes: 0

Views: 70

Answers (2)

rprp
rprp

Reputation: 1

I guess I was more desperate than any one else to get an answer! So after trying many things I got it working and sharing it here for any one else

while( $row = mysqli_fetch_array($queryRecords) ) {
   $marksheet = calculatepercent($row['totalmark'];


            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
          //move the fn 'calculatepercent' outside while loop  $marksheet  => //calculatepercent($row['totalmark']),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );

Upvotes: 0

Hikmat Sijapati
Hikmat Sijapati

Reputation: 7004

Try like this.Send $row['totalmark'] to the calculatepercent() function.

while( $row = mysqli_fetch_array($queryRecords) ) {
            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
                         $marksheet    => calculatepercent($row['totalmark']),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );

Upvotes: 1

Related Questions