Display several results with one SQL query (group by?) - php

I want to show several results of one query, but I need to group some of these results. I think it's better for you to see my problem :
I have this table that shows several trainings :
And when you click on a row, it'll expand and show exercises that correspond to the training's id (here's example values) :
To get all the exercises by training, I made the following query :
SELECT E.id_entrainement, E.intitule_entrainement, E.date_entrainement, EX.id_exercice, EX.reps, EX.poids, M.nom_exm FROM entrainements E, exercices EX, exercices_muscles M WHERE EX.id_entrainement = E.id_entrainement AND EX.membre = E.id_membre AND M.id_exm = EX.id_exercice_muscle AND id_membre='".$user_id."' GROUP BY E.id_entrainement
But when I put in my code the $data['id_entrainement'] for example, it shows me only the first value got by the query as you can see here :
And when I remove the GROUP BY clause from my query, I have this :
I don't know if it's really helpful for you, but here's my php code :
echo '<div class="container">';
echo '<h1>Vos entraînements</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Référence entraînement</th>
<th>Nom de l\'entraînement</th>
<th>Date de réalisation</th>
<th></th>
</tr>
</thead>
<tbody>';
$i=0;
//$getTrainings = $bdd->query("SELECT * FROM entrainements WHERE id_membre='".$user_id."'");
$getTrainings = $bdd->query("SELECT E.id_entrainement, E.intitule_entrainement, E.date_entrainement, EX.id_exercice, EX.reps, EX.poids, M.nom_exm FROM entrainements E, exercices EX, exercices_muscles M WHERE EX.id_entrainement = E.id_entrainement AND EX.membre = E.id_membre AND M.id_exm = EX.id_exercice_muscle AND id_membre='".$user_id."' ");
while ($data = $getTrainings->fetch()) {
$i++;
echo '
<tr class="plusExpand">
<th scope="row">' . $i . '</th>
<td>' . $data['id_entrainement'] . '</td>
<td>' . $data['intitule_entrainement'] . '</td>
<td>' . date("j/n/Y", strtotime($data['date_entrainement'])) . " " . date("G:i", strtotime($data['date_entrainement'])) . '</td>
<td><span class=" glyphicon glyphicon-plus" aria-hidden="true"></span></td>
</tr>
<tr class="exercicesHidden">
<td>1</td>
<td>' . $data['nom_exm'] . '</td>
<td>' . $data['reps'] . '</td>
<td>' . $data['poids'] . '</td>
<td>Up!</td>
</tr>
';
//echo '<tr class="exercicesHidden"><td>Coucou je suis caché!</td></tr>';
}
echo '
</tbody>
</table>
</div>'
I would like all the exercises are stored under one training, and when I click on it, it shows all the exercises from this training.
I hope I gave you all the things to help me to correct this problem!

Related

Data not Displayed after Submit Post [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
I have a problem with my project and i don't know to where is the bug.
Problem: After i Submit Form data is not fetch in the Page.
I tried var_dump($result) but is not return data.
I will Expose my Code and i hope you can help me or guide me to resolve the problem.
<?php
include "connect.php";
include 'header.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_POST["Search"])) {
$table_name = 'imp_acc';
$query = "
SELECT * FROM '.$table_name.' WHERE available = 'YES'
";
if (isset($_POST["regi"], $_POST["stat"])) {
$query .= "
AND reg " . $_POST["regi"] . " AND stat " . $_POST["stat"] . "
";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if($total_row > 0)
{
$output .= '
<table class="table">
<thead>
<tr>
<th scope="col">RRG</th>
<th scope="col">LV</th>
<th scope="col">CR</th>
<th scope="col">PR</th>
<th scope="col">FR</th>
<th scope="col">ES</th>
<th scope="col">RO</th>
<th scope="col">BO</th>
<th scope="col">CP</th>
<th scope="col">SK</th>
<th scope="col">LP</th>
<th scope="col">PRI</th>
<th scope="col">Action</th>
<th scope="col"><th>
<th scope="col"></th>
</tr>
</thead>
';
foreach($result as $row)
{
$timestamp = strtotime($row['lap']);
$newformat = date('Y-m-d',$timestamp);
//$product_id = $row["id"];
$output .= '
<tr>
<td>' . $row["regi"] . '</td>
<td>' . $row["lv"] . '</td>
<td>' . $row["crr_rr"] . '</td>
<td>' . $row["prr_rr"] . '</td>
<td>' . $row["frr_rr"] . '</td>
<td>' . $row["stat"] . '</td>
<td>' . $row["b_o"] . '</td>
<td>' . $row["r_o"] . '</td>
<td>' . $row["cc_count"] . '</td>
<td>' . $row["sk_count"] . '</td>
<td>' . $newformat . '</td>
<td>' . $row["pri_cc"] . '</td>
</tr>
';
}
$output .= ' </table>';
}
else
{
$output = '<h3>No Data Found</h3>';
}
echo $output;
}
?>
Also i need your Opinion for finish my Projects.
I have a Search Page with alot of filters options.
Couple of them are not mandatory , are just optional.
Every filter have a different value in form.
I want to continue in this way to makes more POST like this:
if (isset($_POST["regi"], $_POST["stat"] , ....)
Or i can give a chance to Switch statement, to see how it works.
I wait your opinions and ideea.
Thank you for help
I find the error on code.
It was my fault because i using value and not the form name.
In form data i have something like this:
submit: "Search"
and the code need to be like this:
if (isset($_POST["submit"]))
Now i see another problem is my $result because it comes array.
Thank you for your tips and help.

Problem trying to add multiple choose to mysql

I’m facing a big problem here. My hotel company purchased a Booking system made with PHP from a South African company, their contract expired now they are now charging a lot of money to update the contract and add new functions that we need, so now all the burden is left for me… etc. So I have this table:
and I want to add a multiple type of room at each room. E.g.: Room 1, Type: Simple, Two Beds, Three Beds
Table coode:
<div class="table-responsive">
<table id="datatable-fixed-header" class="table datatable-show-all table-striped table-bordered">
<thead>
<tr>
<th>Nº</th>
<th>Floar</th>
<th>Room No.</th>
<th>Price per Night ( In <?php echo $currency_symbol; ?> )</th>
<th>Price per Week ( In <?php echo $currency_symbol; ?> )</th>
<th>Type</th>
<th>State</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if (count($room_details) > 0) {
$s_no = 1;
foreach ($room_details as $room) {
echo'<tr>
<td>' . $s_no . '</td>
<td>' . $room["property_name"] . '</th>
<td>' . $room["room_number"] . '</th>
<td class="text-right format_currency_td">' . $room["room_standard_night_rate"] . '</td>
<td class="text-right format_currency_td">' . $room["room_standard_weekly_rate"] . '</td>
<td>' . $room["room_type_name"] . '</td>';
if ($room["room_status"] == "AVAILABLE")
echo'<td><span class="label label-success">' . $room["room_status"] . '</span></td>';
else if ($room["room_status"] == "OCUPADO")
echo'<td><span class="label label-info">' . $room["room_status"] . '</span></td>';
else if ($room["room_status"] == "MANUNTEINCE")
echo'<td><span class="label label-warning">' . $room["room_status"] . '</span></th>';
else if ($room["room_status"] == "OUT OF SERVICE")
echo'<td><span class="label label-danger">' . $room["room_status"] . '</span></td>';
echo'<td>';
if ($room["room_status"] != "OUT OF SERVICE")
echo'<div class="th_options_kit">
<span class="th_options btn bg-teal-400 btn-icon btn-rounded btn_edit_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Edit Room" data-placement="left"><i class="icon-pen"></i></span>
<span class="btn bg-danger-400 btn-icon btn-rounded btn_delete_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Delete Room" data-placement="left"><i class="icon-bin"></i></span>
</div>';
else if ($room["room_status"] == "OUT OF SERVICE")
echo'<div class="th_options_kit">
<span class="th_options btn bg-teal-400 btn-icon btn-rounded btn_edit_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Room is out of service" data-placement="left"><i class="icon-pen"></i></span>
</div>';
echo'</td>
</tr>';
$s_no++;
}
}else {
}
?>
</tbody>
</table>
</div>
Php Code:
public function getRoom($where = FALSE) {
$this->db->select('room.`room_id`, '
. 'room.`property_id`, '
. 'room.`room_number`, '
. 'room.`room_type`,'
. 'room.`room_standard_night_rate`,'
. 'room.`room_standard_weekly_rate`,'
. 'room.`room_status`,'
. 'prop.`property_name`,'
. 'room_type.`room_type_name`'
);
$this->db->from('hm_room room');
$this->db->join('hm_room_type room_type', 'room.`room_type` IN (room_type.`room_type_id`)');
$this->db->join('hm_property prop', 'room.`property_id` IN (prop.`property_id`)');
if ($where !== FALSE) {
foreach ($where as $whr) {
$this->db->where_in($whr["column_name"], $whr["data"]);
}
}
$this->db->order_by("prop.`property_name`", "ASC");
$this->db->order_by("room.`room_number`", "ASC");
$query = $this->db->get();
return $query->result_array();
}
Help me solve this please.
I hope i understand your question, your table is currently just showing Room Type e.g. Simple. Is what you want it to show now "Simple, Two Bed" instead of just "Simple" ?
Then what you need to do is concatenate your fields. I can't seem to see what the database field for "Two Bed", but assuming it's "room_beds", then you need to do the below:
First step is to add the database field name to the query, assuming the name of the field in the database is called "room_beds"
$this->db->select('room.`room_id`, '
. 'room.`property_id`, '
. 'room.`room_number`, '
. 'room.`room_type`,'
. 'room.`room_standard_night_rate`,'
. 'room.`room_standard_weekly_rate`,'
. 'room.`room_status`,'
. 'prop.`property_name`,'
. 'prop.`room_beds`,' -- Add the here
. 'room_type.`room_type_name`');
Then finally you need to concatenate the room_beds field to the room_type_name that you already have as below:
<td>'. $room["room_type_name"] .' ,'. $room["room_beds"] .'</td>';

Only display once in while loop

Im using a while loop to go through a DB and pull out some questions and answers. That mostly works apart from the facts the the question order isnt correct.
My question is how do I get the questions to print correctly
Eg :
Q1
Q2
A to Q1
Q3
A to Q2
Blank
A to Q3
Heres an image of what I mean:
Here is the code im using minus the query because I know it works. I think its the if statement thats wrong.
$result = mysqli_query($conn, "SELECT
q.QText, q.id AS QId, ua.id, a.AText, ca.id, ca.Answer_ID,
case when a.id = ua.Answer_ID then 'x' else NULL end as IsUserAnswer ,
case when a.id = ca.Answer_ID then 'x' else NULL end as IsCorrectAnswer
FROM user_answers ua
INNER JOIN question q ON q.id = ua.Question_ID
INNER JOIN answer a ON a.Question_ID = q.id
INNER JOIN correct_answer ca ON ca.Question_ID = q.id
WHERE ua.Test_ID=1
ORDER BY q.ID") or die(mysqli_error($conn));
$lastQuestionID = 0;
while ($data2 = mysqli_fetch_array($result))
{
if ($data2['QId'] != $lastQuestionID)
echo '<p>Q. ' . $data2['QText'] . '</p>
<table class="striped centered">
<thead>
<tr>
<th>Answer</th>
<th>Your Answer</th>
<th>Correct Answer</th>
</tr>
</thead>';
$lastQuestionID = $data2['QId'];
echo '
<tr>
<td>' . $data2['AText'] . '</td>
<td>' . $data2['IsUserAnswer'] . '</td>
<td>' . $data2['IsCorrectAnswer'] . '</td>
</tr>';
}
echo "</table>";
Your table closing is defined incorrectly. Do this instead:
$result = mysqli_query($conn, "Query") or die(mysqli_error($conn));
$lastQuestionID = 0;
$isTableOpen = false;
while ($data2 = mysqli_fetch_array($result)) {
if ($data2['QId'] != $lastQuestionID) {
if ($isTableOpen) {
echo '</table>';
}
$isTableOpen = true;
echo '<p>Q. ' . $data2['QText'] . '</p>
<table class="striped centered">
<thead>
<tr>
<th>Answer</th>
<th>Your Answer</th>
<th>Correct Answer</th>
</tr>
</thead>';
}
echo '
<tr>
<td>' . $data2['AText'] . '</td>
<td>' . $data2['IsUserAnswer'] . '</td>
<td>' . $data2['IsCorrectAnswer'] . '</td>
</tr>';
$lastQuestionID = $data2['QId'];
}
if ($isTableOpen) { // Close last open table
echo '</table>';
}

How to display multiple rows one after the other repeating a sequence depending on how many rows I get?

My idea is to create a Q&A section under a product profile, just like on eBay or Amazon or whatever. The idea is to send a question and then get the owner of the article to reply.
The table has these columns: pid (product ID), id (question ID), question, answer, date (date posted), username.
So if I post a question, I get the ID of the product in which I'm posting and create a question. The the owner just sends the answer to the row that matches the question.
Here's my PHP code to retrieve all the info from that table:
$qanda = '';
$link = mysql_connect("localhost", "youknowwhat", "youknowwhat");
mysql_select_db("youknowwhat", $link);
$qandaq = mysql_query("SELECT * FROM questions WHERE id='$id2' ORDER BY date", $link);
$count = mysql_num_rows($qandaq);
if($count >= 1){
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
}
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
} else {
$qanda = '<div id="answers" align="center">
No questions for this product.
</div>';
}
Now... what you see as a table in the variable $qanda I want to repeat it over and over again but displaying different row data but the concatenation isn't working and I can only get the last row to be displayed. I just can't seem to find out why this isn't working! Am I missing something?
All you have to do is append your divs (.=) while you're inside the while loop that mysql_fetch_array() rows.
Then you'll have a new div for each row your database returns, and you can populate it easily.
$qanda = '';
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
}

How do I pass values with a link to a new page?

What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>

Categories