0

How can i get multiple details from a table instead of one id, i get more data with the table with if..... more explanation and code below

    id  | class  | gender |         score           | date
    -----------------------------------------------------------------------------------
    1   | pry1   |  male  | ,first, first, first    | ,2019-12-04,2019-11-21,2019-11-20
    2   | pry2   | female | ,second, second, second | ,2019-12-04,2019-11-21,2019-11-20
    3   | pry3   |  male  | ,first, first, first    | ,2019-12-06,2019-12-13,2019-12-19 
    4   | pry4   |  male  | ,third, third, third    | ,2019-12-20,2019-12-20,2019-12-20     
    5   | pry5   | female | ,second, second         | ,2019-12-24,2019-12-24,2019-12-24

     ////////i selected e1view from the database /////////////

            if (substr_count($e1view, ",") <'3') 
                  {
                    
                    $last=0;
                  }

            elseif (substr_count($e1view, ",") =='3' OR $m1==='0') {
                
                  $last = ltrim(strrchr($e1view,','),',');

                  if ($last =='first') {
                        $last=1;
                    }
                    else if($last=="second"){
                        $last=2;
                    }
                    else if($last=="third"){
                        $last=3;
                   
                    }

i want to get the details of only the male students using and add their scores individually.... and get the final score into a variable. like

$finalScoreforallmaleStudent = 5;
flochristos
  • 73
  • 3
  • 13
  • in short you can't use `isset` to get anything other than true/false so it makes no sense to hope it will help return `multiple details` - See https://www.php.net/manual/en/function.isset.php – Professor Abronsius Jul 02 '20 at 19:45
  • Any other way to go? – flochristos Jul 02 '20 at 19:46
  • `isset()` has nothing to do with this. If you only want male students, you use `WHERE` in the query: `WHERE gender = 'male'` – Barmar Jul 02 '20 at 19:46
  • hello, @Barmar , I'm trying to search within a date range, i have columns for dates, as i've edited in the post sir, i'm unable to get it. thanks in anticipation for your help – flochristos Jul 06 '20 at 19:14
  • hello sir @Barmar, i have created it here https://stackoverflow.com/questions/62763825/how-can-i-get-data-from-using-date-range-for-querying-multiple-details-from-a-ta – flochristos Jul 06 '20 at 20:26

1 Answers1

2

Use explode() to split the string up. Use an assoociative array to convert the words to numbers that you can add.

$place_map = ['first' => 1, 'second' => 2, 'third' => 3];
$query = "SELECT scores FROM yourTable WHERE gender = 'male'";
// perform the query ...
$finalScoreforallmaleStudent = 0;
while ($row = $result->fetch_assoc()) {
    $scores = explode(',', $row['scores']);
    $last_score = trim($scores[count($scores)-1]);
    $finalScoreforallmaleStudent += $place_map[$last_score];
}
echo $finalScoreforallmaleStudent;
Barmar
  • 741,623
  • 53
  • 500
  • 612