Im a begginer in the whole SO and programming enviroment, and here comes my first question(which i tried finding in here but with no luck so far..)
I have an html table that prints specific patients of the doctor that is active (in a simple web-application that i created). The doctor gets filtered using $_SESSIONS global var in php.
My problem is that i want to implement a few actions in the same HTML table that displays the patients, like view history (which is stored in a local DB table, from an HTML from using POST method) and create a new form for the same person.
I saw that providing the table with row.ids could be a solution, but my table isn't static, i woulda like for the user to have the option to add/delete patients..
Following is a sample of my code that displays the HTML table "Existing Patients" :
<table>
<tr>
<th>Patient Id</th><th>Patient Name</th><th>Phone Number</th><th>Email</th><th>History</th>
<th>Add a Follow Up Visit</th><th>Remove Patient</th>
</tr>
<?php $sql = "SELECT * FROM patients WHERE Doctor_ID = $usersid";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
while($row = $result->fetch()){?>
<tr>
<td><?php echo $row['Patient_id']; ?></td>
<td><?php echo $row['Patient_name'] ; ?></td>
<td><?php echo $row['Phonenum'] ; ?></td>
<td><?php echo $row['Email']; ?></td>
<td><?php echo "<a href='/patienthistory.php'</a>" . 'Previous Visits'; ?></td> //problem here
<td><?php echo "<a href='/patientform.html'</a>" . 'Add Follow Up' ; }?></td> //and here
</tr>
</table>
I want the last 2 lines to connect immediately to the specific database stored Patient_id and retrieve the existing information stored there from the patients previous visits.
I hope i gave enough of a description, but if there is any more info neccesary, please let me know.
Thank you in advance!
You can specify the patient ID as a query parameter in the URL that you're linking to. Also the HTML you're generating for your links is invalid currently. And the second link is going to need to go to a .php script, not a .html file, if you want it to execute code and fetch data.
Try this example:
<td><?php echo "<a href='/patienthistory.php?id='".$row['Patient_id']."'>Previous Visits</a>"; ?></td>
<td><?php echo "<a href='/patientform.php?id='".$row['Patient_id']."'>Add Follow up</a>"; ?></td>
Then in each of the PHP scripts, use $_GET["id"] to retrieve that ID of the patient and use that in a query
e.g.
$patientID = $_GET["id"];
I have a table like this.
author_id author_state author_name
1 CA Hello
2 MI World
3 CA How
4 MI Are
Please let me know how can I achieve this. In other words, I should loop it and combine the state values first and display the details.
Here is my PHP Code
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$row_data = mysql_query("select * from author" );
while($row = mysql_fetch_array($row_data) ) {
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
My Expected output is to combine the state and display something like below:
Author ID Author Name
=======================================
CA
==
1 Hello
3 How
MI
==
2 World
4 Are
You can do this fairly easily, by sorting your output by author_state, and using a variable to keep track of which state you're outputting. If the state changes, then you simply output the state as a new header.
You can take advantage of the SQL order by clause so that you don't have to sort the data yourself.
Here is an edit of your code, where I've simply added the order by to your mysql_query(), and added a variable for $current_state.
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$current_state = "";
$row_data = mysql_query("select * from author order by author_state" );
while($row = mysql_fetch_array($row_data) ) {
// Output the "new" state
if ($row['author_state'] != $current_state)
{
echo "<tr><td colspan=\"3\">{$row['author_state'}}</td></tr>";
$current_state = $row['author_state'];
}
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
As a side note, I would strongly encourage you to look into using mysqli or PDO instead of the mysql_* functions. The mysql_* functions are only availble in very old versions of PHP (they were completely removed in PHP7).
as i am a beginner so i want to ask how can i run the multiple queries using PHP for the same row to have multiple data in the table in html. i have tried many attempts but nothing worked i am using MYSQL as backend. I know that i have not written code for the first row and second column i.e. the second <td> on wicket keeper but the thing is that i want all rounder data to come in the second <td> of wicket keeper section. when running this code it is providing the output fine for wicket keeper but for all rounder column the data is starting after the end of wicket keeper data. i know why this problem is occurred but how can i solve this please tell??
CODE HERE:
<?php
$sql="select * from teams where role = 'WicketKeeper'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php
$sql="select * from teams where role = 'AllRounder'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td></td>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
Here is a good way to generate the rows of the display you showed.
First, use this query:
SELECT name, role FROM teams ORDER BY role DESC;
It generates rows in the order you want them, wicketkeepers first.
Then, use this php code to generate your display.
<?php
$sql="SELECT name, role FROM teams ORDER BY role DESC";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
$role = $row["role"];
$name = $row["name"];
$wicketkeeper = $role == "WicketKeeper" ? $name : "";
$allrounder = $role == "AllRounder" ? $name : "";
?>
<tr>
<td><?php echo $wicketkeeper ?></td>
<td><?php echo $allrounder ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
This renders the rows in the result set from your query, placing names in columns according to their roles.
In the general case, this task is known as pivoting a table.
Pro tip 1 Try to avoid SELECT * in queries. Instead give the names of the columns you need. This makes your code easier for a stranger to read, and it may make your queries run faster.
Pro tip 2 Anytime you catch yourself reusing the same query where it only varies by a WHERE clause, try to use a single query. This can make your application run faster.
I've read through about 20 different answers regarding this question, but either I'm mis-understanding the answers, or its just not clicking. Here is my situation:
I have a table that lists ingredients for a recipe. Columns in the table are: ingredient_id, ingredient_title, ingredient_oz, ingredient_grams, ingredient_lbs (pounds), etc.
I want to list each ingredient, then after all ingredients have been listed, add a final row that sums up all the oz, grams, lbs, etc. Below is an example of the output I am trying to achieve.
Example:
INGREDIENT TITLE OZ GRAMS LBS
ingredient1 4 6 3
ingredient2 1 2 4
ingredient3 9 4 4
TOTAL 14 12 11
My first thought was simply using SUM() AS in the SQL
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams, SUM(ingredient_oz) as oz_sum, SUM(ingredient_lbs) as lbs_sum, SUM(ingredient_grams) as grams_sum FROM ingredients
And here is the code on my page:
<!-- Beginning of table is here -->
<?php
while ($ingredientRow = $ingredients->fetch_assoc()) { ?>
<tr>
<td><?php echo $ingredientRow["ingredient_title"]; ?></td>
<td><?php echo $ingredientRow["ingredient_oz"]; ?></td>
<td><?php echo $ingredientRow["ingredient_lbs"]; ?></td>
<td><?php echo $ingredientRow["ingredient_grams"]; ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td>TOTALS</td>
<td><?php echo $ingredientRow["oz_sum"]; ?></td>
<td><?php echo $ingredientRow["lbs_sum"]; ?></td>
<td><?php echo $ingredientRow["grams_sum"]; ?></td>
</tr>
</tfoot>
</table>
<?php }?>
However all that does is return the first row (ingredient 1), and doesn't return the remaining rows or the sum. Then as I continued to read about this, I saw a low of people discussing using "group by" as well. So then I tried:
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams, SUM(ingredient_oz) as oz_sum, SUM(ingredient_lbs) as lbs_sum, SUM(ingredient_grams) as grams_sum FROM ingredients GROUP BY ingredient_title
That returns all the rows, but again doesn't return a sum. Am I grouping by the wrong field? Do I need to group each of the fields I'm trying to sum?
When you run a query, you will get the data back that you ask for, so basically if you run a query to return all the rows individually - you will get those back, without the total. If on the other hand you run a query to get only the sum/totals, you won't get the individual rows of data.
There are two ways to get what you want. One is done via a query, one is done via PHP itself.
You can write a union query to get the individual rows of data, then return the sums, something like this:
SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM
ingredients
union all
SELECT
ingredient_title,
SUM(ingredient_oz) as oz_sum,
SUM(ingredient_lbs) as lbs_sum,
SUM(ingredient_grams) as grams_sum
FROM
ingredients
Which will return both.
Or you can write a quick snippet of PHP code to do the addition for you in your code based on the first part of the query:
<?php
$sql="SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM
ingredients";
//Execute query:
while($result)
{
echo $result['ingredient_title'];
echo $result['ingredient_oz'];
// etc etc. Format as needed...
$ingOz+=$result['ingredient_oz'];
$ingLbs+=$result['ingredient_lbs'];
$ingGrams+=$result['ingredient_grams'];
}
// And now the totals:
echo $ingOz;
echo $ingLbs;
// etc etc.
?>
I would personally probably use the second approach - you don't need to make the database run the query twice just to get the results - and you are already getting all the individual rows of data, therefore you may as well simply keep a simple running total in a variable to be displayed as needed.
Don't be afraid to run 2 separate SQL queries, because:
Grouping (for sum) works differently
Also code logic will more readable and decoupled
$query = "
SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM ingredients
";
$ingredients = $db->query($query)->fetch_all();
$query = "
SELECT
SUM(ingredient_oz) as oz_sum,
SUM(ingredient_lbs) as lbs_sum,
SUM(ingredient_grams) as grams_sum
FROM ingredients
";
$ingredientSummary = $db->query($query)->fetch_assoc();
<!-- Beginning of table is here -->
<?php
foreach ($ingredients as $ingredientRow) ?>
<tr>
<td><?php echo $ingredientRow["ingredient_title"]; ?></td>
<td><?php echo $ingredientRow["ingredient_oz"]; ?></td>
<td><?php echo $ingredientRow["ingredient_lbs"]; ?></td>
<td><?php echo $ingredientRow["ingredient_grams"]; ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td>TOTALS</td>
<td><?php echo $ingredientSummary["oz_sum"]; ?></td>
<td><?php echo $ingredientSummary["lbs_sum"]; ?></td>
<td><?php echo $ingredientSummary["grams_sum"]; ?></td>
</tr>
</tfoot>
</table>
<?php }?>
I think you should make it a separate query when selecting the sum. Does it make sense that it can get the sum of each row when the sum is of all the rows? By grouping you cause the SUM to sum each group, and every ingredient is a marked as a group.
its little late, but its easy with a roll back at the end of query
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams
FROM ingredients
WITH ROLL BACK
here some examples from other site
https://www.mysqltutorial.org/mysql-rollup/
I am trying to use PHP to return SQL values into an HTML table. I am able to get every column to populate without a problem except for the last column, "GROUP _ CONCAT (provision_id)."
Relevant code:
<?php
global $wpdb;
$wpdb->show_errors();
$contents = $wpdb->get_results( $wpdb->prepare("SELECT salaries.id, name, remaining, contract_value, GROUP_CONCAT( provision_id ) FROM salaries LEFT JOIN contracts ON contracts.id = salaries.id GROUP BY salaries.id"));
?>
[table header stuff...]
<?php
foreach ($contents as $content) {
?>
<tr>
<td><?php echo $content->name ?></td>
<td><?php echo $content->remaining ?></td>
<td><?php echo $content->contract_value ?></td>
<td><?php echo $content->GROUP_CONCAT(provision_id) ?></td>
<?php }; ?>
</tr>
Just echoing $content->provision-id doesn't work either.
Use an alias for the column.
GROUP_CONCAT( provision_id ) as pids
...
echo $content->pids
If you are fetching into objects, you should give your columns names that are legal class member identifiers in PHP (I'll link to the manual, although their description of valid variable names is horrible):
SELECT ... GROUP_CONCAT(provision_id) AS provisions