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 hope the title makes sense and sorry if this is a duplicate I wasn't certain what to search for to find the correct answer.
I have a table on a website that pulls info from a database. So my issue is I have two tables in the database, the_traveler and the_discipline. What I am doing is pulling the value from the traveler that looks like this 89,43 and after I LEFT JOIN with the_disciplines table, to get the name that correlates with the numbers, like so:
$query ="SELECT t.name,t.traveler_id, t.disciplines, dis.name as discname
FROM ".TABLEPRIFIX."traveler t
LEFT JOIN ".TABLEPRIFIX."discipline dis ON t.disciplines = dis.id";
$disname = $obj->queryResult($query);
I am only able to echo out the first numbers name by doing this:
<?php
foreach($disname as $traveler)
{
<tr>
<td><?php echo $traveler['discname']; ?></td>
</tr>
<?php
}
?>
My question is how would I change my code to get all the numbers from the database to echo in each row. I have tried to implode the numbers.
If you want to get a single row with all values you must change the code to:
<tr>
<?php
foreach($disname as $traveler)
{
<td><?php echo $traveler['discname']; ?></td>
<?php
}
?>
</tr>
I currently have a table on my website which I have in my SQL database and I need each row of my table to get certain information from the columns. I have something that works just fine at the moment, however, I have a feeling its not the best way of doing things. As I have around 600+ rows in my database.
I'm trying to learn more about all of this so if you have any idea to do this in a better way then that would be fantastic.
<?php
$queryContent="SELECT * FROM businesses WHERE id= 104";
$resultContent= $mysqli->query($queryContent);
$rowContent = $resultContent->fetch_assoc();
?>
<?php if ($rowContent['active'] == "Y") { ?>
<tr>
<td><?php echo $rowContent['businessname']; ?></td>
<td><?php echo $rowContent['telephoneno']; ?></td>
<td><?php echo $rowContent['emailaddress']; ?></td>
<td><?php echo $rowContent['rating']; ?></td>
</tr>
<?php } ?>
If you are trying to list all active rows, try something like this:
<?php
// Change query to select only acitve rows
$queryContent="SELECT * FROM businesses WHERE active = 'Y'";
$resultContent= $mysqli->query($queryContent);
//while cycle will repeat while sql have results
while($rowContent = $resultContent->fetch_assoc()){
?>
<tr>
<td><?php echo $rowContent['businessname']; ?></td>
<td><?php echo $rowContent['telephoneno']; ?></td>
<td><?php echo $rowContent['emailaddress']; ?></td>
<td><?php echo $rowContent['rating']; ?></td>
</tr><?php } ?>
Php documentation examples should be also usefull.
When you have a list of IDs wich you want to select from sql look at SQL WHERE - IN operator and syntax.
SELECT * FROM businesses WHERE id IN (103, 104, 105)
I could think of two things which can be improved in your code:
instead of select *, you can mention select column1, column2,... which is considered to be the standard
And if you are trying to list only one row, then either access the $rowcontent array with index 0 (or) limit the select query's output rows by using "LIMIT 1" at the end of "SELECT" query (second option valid only if you are not sure about the query whether it will fetch only one row always or not)
I am trying to make a dynamic HTML table with PHP, populating it with data from MySQL database. So far I have tried the while loop, but the result ends up in displaying the same first row it gets multiple times.
<div class = "container">
<p>Registered companies:</p>
<table border = "1px" align = "left">
<tr>
<th>Username</th>
<th>Company name</th>
<th>Company value1</th>
<th>Company value2</th>
</tr>
<?php
$compRowIncrement = 0;
while ($compRowIncrement < $companyRowCount) {
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
<td><?php echo $companyRow['company_name']?></td>
<td><?php echo $companyRow['company_value1']?></td>
<td><?php echo $companyRow['company_value2']?></td>
</tr>
<?php
$compRowIncrement++;
}
?>
</table>
</div>
It should display 3 rows of data for example (SQL query returns 3 different values). But so far I have achieved to get 3 rows (like I need) with the same data (first value it gets from the database).
How do I do it so each table row is populated with different data, as it is in the database.
I'm just learning, so if you don't mind ignore the css values in table :).
EDIT1 (Added query)//
$getPlayerCompanies = $MySQLi_CON -> query("SELECT DISTINCT *
FROM companies
LEFT JOIN player ON companies.player_id = player.player_id
LEFT JOIN users ON users.user_id = player.user_id
WHERE users.user_id =".$_SESSION['userSession']);
$companyRow = $getPlayerCompanies -> fetch_array();
$companyRowCount = $getPlayerCompanies -> num_rows;
Following query currently returns 3 rows, like it should.
where did the $companyRow values get populated?
I belive your code should be more like this (or with mysqli commands)
<?php
while ($companyRow = $getPlayerCompanies -> fetch_array() )
{
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
...
</tr>
<?php
}
?>
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/