0

The data inside while loop works fine when I use echo before each variable inside loop, but outside the loop php doesn't seem to recognize the variables and only prints once even though database has more than one row . Please help!

    if($process['organdoner']=="Yes" && $processorgan['bloodtype']== $process['bloodtype'] && $processorgan['bodytype']== $process['bodytype']  ){ //with same bloodtype,bodytype,intensivecare
        $to=$process['email'];
        $header= "Organ is urgently needed";
        $phquery="SELECT personprofile.firstname,personprofile.lastname,personprofile.fathername,hospital.hospitalname,hospital.geolocation 
        FROM personprofile,hospital,areaname 
        WHERE areaname.id= hospital.area AND personprofile.hospitaladmission= hospital.id AND areaname.area='$city' AND personprofile.admissionreason= 5";
        $fetchospitalperson=  mysqli_query($link,$phquery);
        $processtable=mysqli_fetch_assoc($fetchospitalperson);
        do{
         $messagebody= "Patient Name: ".$processtable['firstname']." ".$processtable['fathername']." ".$processtable['lastname']."  "."Hospital Name:"; 

        //$hyperlink= new DOMDocument();
        //$hyperlink->loadHTML("<html><body><a href='<".$processtable['geolocation']."'>" .$processtable['hospitalname']."</a></body></html>"); 
         $hyperlink= "<html><body><a href='<".$processtable['geolocation']."'>".$processtable['hospitalname']."</a></body></html>";
         //Name of the person that needs the blood transfusion along with hospital he is staying at,hyperlinked to its location
        }while($processtable=mysqli_fetch_assoc($fetchospitalperson));

    $message= "Dear"." ".$firstname." ".$lastname.",".PHP_EOL .$messagebody.$hyperlink.PHP_EOL; 
    if(isset($sendtoperson)){   
        if(mail($to,$header,$message)){
            echo "Sent";
        }
        else{ echo "Not sent";}
    }
} 

1 Answer 1

1

You are overwriting the variables each time round the loop so instead of using x=y use the concatenator x .= y

do{
     $messagebody .= "Patient Name: ".$processtable['firstname']." ".$processtable['fathername']." ".$processtable['lastname']."  "."Hospital Name:"; 

     $hyperlink .= "<html><body><a href='<".$processtable['geolocation']."'>".$processtable['hospitalname']."</a></body></html>";

}while($processtable=mysqli_fetch_assoc($fetchospitalperson));
Sign up to request clarification or add additional context in comments.

1 Comment

Although I'm not sure about just appending the content will give anything meaningful.

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.