-6

I am not good with functions and classes in PHP. I am echoing a function instead of return because it gives me desired results. But it also echo out the result value to my desired page. This function is in class. Just see this screenshot and you will understand what i want.

Echo out the total numbers:

<?php 

echo $results->get_total_marks_subjects($subject_detail['subject_id']); 

?>

Here is function code in class:

    while($rec  =   mysql_fetch_array($link)) {
        //i think the code below echoes out that message.
        echo    $rec['total_marks']." | "; 
        //return    $rec['total_marks'];
    }
}    
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
user1313942
  • 21
  • 2
  • 6
  • 1
    What is the question ? i don;t see any function here ! – The Alpha Sep 27 '13 at 18:40
  • I didn't post the all function code, because the problem is that i am echoing value in function instead returning. Just see the image and then you will what i want. – user1313942 Sep 27 '13 at 18:45
  • just comment echo, and you'll get what you want – user4035 Sep 27 '13 at 18:45
  • Are you asking if its better to echo or return the result from inside a function ? My preferred way is to return data from a function, then deside if i will echo it or use it as a php variable/state at the time i call it. – Tom Sep 27 '13 at 18:53
  • Echo gives me all values in a result set but if i return then it gives me only first value. Let say, if there are 3 test in database then return i s giving me only one, while echo gives me all and that all i want. – user1313942 Sep 27 '13 at 18:57

1 Answers1

0

I'm going guess what you want...

I'm assuming it's going to echo $rec['total_marks'] multiple times as it is going through results of a mysql object.

instead of:

<?php echo $results->get_total_marks_subjects($subject_detail['subject_id']); ?>

try:

<?php 
     $_results=$results->get_total_marks_subjects($subject_detail['subject_id']);
     foreach ( $_results as $_result ) {
         echo $_result;
     }
?>

insteadof:

     while($rec  =   mysql_fetch_array($link)) {
        echo    $rec['total_marks']." | "; i think, this code echo    out that message.
        //return    $rec['total_marks'];
        }

try:

     $_tempcounter=0;
     while($rec  =   mysql_fetch_array($link)) {
        $arrayofresults[$_tempcounter++] = $rec['total_marks'];
        }
     return $arrayofresults;
Zork
  • 149
  • 1
  • 1
  • 11
  • thanks for your answer...but sadly, it's not working...It is making my page blank...there is may be a problem in your code. – user1313942 Sep 27 '13 at 19:09
  • i could not find a solution by using php but hide those values by using CSS. For the time being, it is working. Thanks for your replies. – user1313942 Sep 27 '13 at 19:45