samanj Posted April 2, 2020 Share Posted April 2, 2020 (edited) Hello community, I am working on a database of specialties in the hospital I work. The doctor's referral requests are sent to a mySQL database and I have, with the help of online guidance, produced a working php script that displays the information I need it to. However, I need it a little bit more specific. I intend to make multiple copies of this file for each specialty, so that when they open the file they only have the requests for that particular specialty. My question is, with reference to my code below, can I make echo information so that online, for instance, if 'specialty1 = gastroenterology' (as in, that particular specialty that that referral request is for), then only the rows on the database that have that particular text are displayed only? Hope that makes sense. Code below for your reference and assistance is highly appreciated. <!DOCTYPE html> <html> <head> <title>Specialty Referral Form</title> <style> table { border-collapse: collapse; width: 100%; color: #000000; font-family: arial; font-size: 10px; text-align: center; } th { background-color: #588c7e; color: white; } tr:nth-child(even) {background-color: #f2f2f2} </style> </head> <body> <table> <tr> <th>Patient Details</th> <th>Hospital Number</th> <th>Date of Birth</th> <th>Referred by:</th> <th>New/Repeat Visit to Patient</th> <th>Specialty</th> <th>Admission Date</th> <th>Too Ill for Clinic?</th> <th>Diagnosis Aware?</th> <th>Question</th> <th>History</th> <th>Medications</th> <th>Examination</th> <th>Results</th> <th>WorkingDiagnosis</th> <th>Investigation(s) Requested</th> </tr> <?php $conn = mysqli_connect("localhost", "view", "", "referral"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT patientdetails, hospitalnumber, DoB, referral, admission, specialty1, admissiondate, illness, awareness, question, history, medications, examination, results, workingdiagnosis, investigations FROM referralform"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while ($row = $result->fetch_assoc()) { echo "<tr><td>" . $row["patientdetails"]. "</td><td>" . $row["hospitalnumber"] . "</td><td>" . $row["DoB"] . "</td><td>" . $row["referral"] . "</td><td>" . $row["admission"] . "</td><td>" . $row["specialty1"] . "</td><td>" . $row["admissiondate"] . "</td><td>" . $row["illness"] . "</td><td>" . $row["awareness"] . "</td><td>" . $row["question"] . "</td><td>" . $row["history"] . "</td><td>" . $row["medications"] . "</td><td>" . $row["examination"] . "</td><td>" . $row["results"] . "</td><td>" . $row["workingdiagnosis"] . "</td><td>" . $row["investigations"]. "</td></tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> </table> </body> </html> I guess what I am looking for is something like echo specialty1 IF it writes a particular specialty and only that specialty. Thank you. Edited April 2, 2020 by samanj Quote Link to comment https://forums.phpfreaks.com/topic/310451-making-the-script-echo-only-specific-results/ Share on other sites More sharing options...
mac_gyver Posted April 2, 2020 Share Posted April 2, 2020 23 minutes ago, samanj said: make multiple copies of this file for each specialty no. you would use one file, that accepts a specialty id as an input, then uses a query with a WHERE clause in it to match the requested specialty information. the specialties should be defined in a database table, with an id and a name. this will assign an id to each specialty. you would query this table to retrieve all the specialties to produce some sort of navigation/selection menu. when the visitor to the page picks a specialty via the navigation/selection menu, the submitted specialty id would be used in the code in the single file to cause the correct data to be retrieved and used to display the contents on the page. Quote Link to comment https://forums.phpfreaks.com/topic/310451-making-the-script-echo-only-specific-results/#findComment-1576180 Share on other sites More sharing options...
requinix Posted April 2, 2020 Share Posted April 2, 2020 The first step is to learn about getting data from the query string (aka $_GET). The URL to the page would be "script.php?specialty=whatever". One file handles everything, like mac_gyver said. The second step is to take the value from the query string and use it with a prepared statement. Quote Link to comment https://forums.phpfreaks.com/topic/310451-making-the-script-echo-only-specific-results/#findComment-1576181 Share on other sites More sharing options...
samanj Posted April 3, 2020 Author Share Posted April 3, 2020 Ok thank you for the replies, it got the mental circuits in the brain working! This is what I have been able to find and tweak based on both of your guidances (work in progress for all of the rows needed): <!DOCTYPE html> <html> <head> <title>Specialty Referral Form</title> <style> table { width: 100%; color: #000000; font-family: arial; font-size: 10px; text-align: center; } th { background-color: #588c7e; color: white; } tr:nth-child(even) {background-color: #f2f2f2} </style> </head> <body> <table> </tr> <?php $link = mysqli_connect("localhost", "root", "", "referral"); if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $sql = "SELECT * FROM referralcard WHERE hospitalnumber='345678'"; if($res = mysqli_query($link, $sql)){ if(mysqli_num_rows($res) > 0){ echo "<table>"; echo "<tr>"; echo "<th>patientdetails</th>"; echo "<th>hospitalnumber</th>"; echo "<th>history</th>"; echo "<th>comments</th>"; echo "</tr>"; while($row = mysqli_fetch_array($res)){ echo "<tr>"; echo "<td>" . $row['patientdetails'] . "</td>"; echo "<td>" . $row['hospitalnumber'] . "</td>"; echo "<td>" . $row['history'] . "</td>"; echo "<td>" . $row['comments'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_free_result($res); } else{ echo "No Matching records are found."; } } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } mysqli_close($link); ?> I appreciate you want propose that I make one file for all the different specialties. My question is probably even more specific now, but any advice on how I can tailor this part: $sql = "SELECT * FROM referralcard WHERE hospitalnumber='345678'"; i.e. the hospital number, in a text box, where I can type the number I want and that number appear as the hospital number I want where '345678' is currently in the code. Then press submit to have the single row result I am looking for. Would this be more in the realm of merging a html text box with php coding (e.g. is it using the POST function?) Hope this makes sense. Thanks as always. Quote Link to comment https://forums.phpfreaks.com/topic/310451-making-the-script-echo-only-specific-results/#findComment-1576217 Share on other sites More sharing options...
samanj Posted April 3, 2020 Author Share Posted April 3, 2020 Final update, I have made what I wanted to work, work. One page for everything. Case closed. If anyone is interested in the code (albeit a very simple one), then feel free to get in touch Quote Link to comment https://forums.phpfreaks.com/topic/310451-making-the-script-echo-only-specific-results/#findComment-1576220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.