Problem trying to add multiple choose to mysql - php

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>';

Related

PHP leaderboard not showing the correct way

I am making a leaderboard with persons out of my database. My website is build in PHP (PDO) the problem is that I cant get the users out of the database to show the correct way under each other.
The function I use to get info out of the database is:
public function get_klanten(){
$getKlant = $this->database->query("SELECT * FROM klanten ORDER BY id ASC LIMIT 1");
$klanten = $this->database->resultset();
return $klanten;
}
How it is now
The problem I get is that it puts every records after each other and not on a new line that why I added LIMIT 1 but I want that the second user in this case Amet also be out of the database.
The HTML with this is a different file and is the code down below.
<thead>
<tr>
<th>#</th>
<th>Lid</th>
<th><span class="glyphicon glyphicon-sort-by-attributes" aria-hidden="true"></span></th>
<th><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<tr>
<?php
$klantten = $app->get_klanten();
foreach ($klantten as $klant) {
echo '<td>' . $klant['id'] . ' </td>';
echo '<td>' . $klant['voornaam'] . ' ' . $klant['achternaam'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
}
?>
</tr>
<tr>
<td>2</td>
<td>amet</td>
<td>456</td>
<td>52</td>
</tr>
BONUS
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Stel jezelf voor</h2>
<h5> Laat wetn wie jij en je business zijn</h5>
</div>
</div>
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
<?php
$toppics = $app->get_topics();
$i = 0;
foreach ($toppics as $topic) {
echo '' . $topic['onderwerp'] . '';
}
?>
</div>
</div>
Function:
public function get_topics(){
$getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC");
$topics = $this->database->resultset();
return $topics;
}
I want this also to be each record in a new block. So each topic is a new block. You can see now 2 blocks I want each block to get a different record out of the database
1: https://i.stack.imgur.com/lH8oz.png
Remove limit 1 from query, then move your TR tag so it's inside your foreach loop.
<?php
$klantten = $app->get_klanten();
foreach ($klantten as $klant) {
echo '<tr>';
echo '<td>' . $klant['id'] . ' </td>';
echo '<td>' . $klant['voornaam'] . ' ' . $klant['achternaam'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '</tr>';
}
?>

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

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!

Converting to PDF using PHP

so I'm using this API (http://phptopdf.com/) to convert some php text to PDF.
But I'm getting this error :-
Everything works fine until the first name, email.
But, when I try to link in the orders, everything goes haywire.
Parse error: syntax error, unexpected T_WHILE in /home/a/public_html/pdf/packing_slip.php on line 124
Code
<?php
session_start();
include_once('phpToPDF.php') ;
include '../dbconnector.php';
include_once '../inc/inc.functions.php';
include '../dbpdo.php';
//require 'fbconfig.php';
if(!isset($_SESSION['email']))
{
//not logged in.
header('Location:http://macbuyback.co.uk/register');
exit();
}
if((isset($_SESSION['logged']) && $_SESSION['logged']=1) || $user)
{
//load the record for last orders
$user_email = $_SESSION['email'];
$date=date('Y-m-d');
try
{
$statement = $conn->prepare("SELECT * From sales where email =? and orderDate = ?");
$statement->execute(array(
$user_email,
$date));
$statement->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
//now get the customer details
try
{
$statement2 = $conn->prepare("SELECT * From users where email = ?");
$statement2->execute(array(
$user_email));
$statement2->setFetchMode(PDO::FETCH_ASSOC);
$user_info = $statement2->fetch();
}
catch(PDOException $e)
{
echo $e->getMessage();
}
// Assign html code into php variable:-
$html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Company</title>
<style type="text/css">
<!--
body {
font-family:Tahoma;
}
img {
border:0;
}
#page {
width:800px;
margin:0 auto;
padding:15px;
}
#logo {
float:left;
margin:0;
}
#address {
height:181px;
margin-left:250px;
}
table {
width:100%;
}
td {
padding:5px;
}
tr.odd {
background:#e1ffe1;
}
-->
</style>
</head>
<body>
<div id="page">
<div id="logo">
<img src="http://www.domain.co.uk/pdf/logo.png">
</div><!--end logo-->
<div id="address">
<p>Company<br />
info#company
<br /><br />
Created on ' . date("Y-m-d") . '
echo ' . $a . '
</p>
</div><!--end address-->
<div id="content">
<p>
<strong>Customer Details</strong><br />
Name: ' . $user_info['firstname'] . '<br />
Email: ' . $_SESSION['email'] . '<br />
<hr> ' . $a=1 .'
<table>
<tr>
<td><strong>Serial</strong></td>
<td><strong>Product Name</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Pickup Date</strong></td>
</tr>
' . while($row = $statement->fetch()){ . '
' if(($a%2) == 0){ . '
<tr class="odd">
<td> ' . $a . '</td>
<td> ' . getProductNameFromId($row['pid']) . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['shipmentdate'] . '</td>
</tr>
' . $a++ }else{. '
<tr class="even">
<td> ' . $a . '</td>
<td> ' . getProductNameFromId($row['pid']) . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['shipmentdate'] . '</td>
</tr>
' . $a++ }. '
' . } . '
</table>
<hr>
<p>
Thank you for your order! This transaction will appear on your billing statement as "Your Company".<br />
If you have any questions, please feel free to contact us at youremail#somewhere.com.
</p>
<hr>
<p>
<center><small>This communication is for the exclusive use of the addressee and may contain proprietary, confidential or privileged information. If you are not the intended recipient any use, copying, disclosure, dissemination or distribution is strictly prohibited.
<br /><br />
© Your Company All Rights Reserved
</small></center>
</p>
</div><!--end content-->
</div><!--end page-->
</body>
</html>';
phptopdf_html($html,'pdf', 'sample.pdf');
echo "<a href='pdf/sample.pdf'>Download PDF</a>";
}//login check
else
{
header('Location:http://domain.co.uk/register');
exit();
}
?>
What might be wrong ?
Any suggestions are welcome.
Your while loop cannot be used inside a string concatenation.
$html ='.....
</tr>';
while($row = $statement->fetch()){
if(($a%2) == 0){
$html .= '<tr class="odd">
<td> ' . $a . '</td>
<td> ' . getProductNameFromId($row['pid']) . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['shipmentdate'] . '</td>
</tr>';
$a++; }else{
$html .= '<tr class="even">
<td> ' . $a . '</td>
<td> ' . getProductNameFromId($row['pid']) . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['shipmentdate'] . '</td>
</tr>';
$a++; }
}
$html .= ' </table>
.....';

Delete from html table made by database PHP

Here is the table below that I'm trying to delete rows out of:
<form method="POST" >
<table class="sortable">
<thead>
<tr>
<th id="makehead">Make </th>
<th id="modelhead">Model </th>
<th id="idhead">Delete </th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($carArray as $k => $carInfo) {
$i++;
echo '<tr>';
if ($i % 2) {
echo '<td class="make">' . $carInfo['make'] . '</td>
<td class="model">' . $carInfo['model'] . '</td>
<td class="id"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
} else {
echo '<td class="makelight">' . $carInfo['make'] . '</td>
<td class="modellight">' . $carInfo['model'] . '</td>
<td class="idlight"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
}
}
?>
</tr>
</table>
</tbody>
<td>
<input Onclick="return ConfirmDelete();" name="delete" type="submit" id="delete" value="Delete"></input>
</td>
</table></form>
As you can see i'm using checkboxes to tick each row then the delete button will have a confirm message then should delete but it doesn't here is my if statement:
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->setdbid($dbid);
So when this wasn't working I had a look on here and on other questions people said I need a setter function so i did this: EDIT: this is my class file.
public function setdbid($dbid){
$this->dbid=$dbid;
}
for this main function to delete things:
public function delete($dbid) {
try {
$sql = "DELETE FROM cars WHERE id = '$dbid'";
$this->db->exec($sql);
echo "Car has been deleted.";
} catch (PDOException $e) {
echo $e->getMessage();
}
}
So that's all the relevant code I think, please help me if you can.
You just have to replace some piece of code in PHP :
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->delete($dbid); //Assuming delete is well a $db method, else replace it by the correct delete call
}
As you are using checkboxes with the same name, you have to change it so it's an array (and this way you'll be able to delete multiple rows at once) :
<td class="id"><input type="checkbox" name="ids[]" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
Then in your php code, treat this data as such :
if ($_REQUEST['delete']) {
foreach($_REQUEST['ids'] as $id){
$xxx->delete(intval($id)); //convert to integer to avoid sql injection.
}
}
Note that you don't need to set $db->setdbid since you pass that id as a parameter of your delete method.

Foreach php variable value create while loop

I have a table like this full of orders, each customer will have multiple orders:
Customer ponumber quantity ..
customer1 234345 56
customer2 343454 34
customer1 w34234 54
customer5 332423 54
I'm trying to loop through each DISTINCT customer and print the corresponding orders with the following code.
<?php
include("php/database_connect.php");
$query = mysql_query("SELECT DISTINCT customer FROM orders");
$customer = mysql_fetch_array($query);
foreach($customer as $customers)
{
echo ' <li><h1>'. $customers . '</h1></li>';
$result = mysql_query("SELECT * FROM orders WHERE misc='new' AND customer='$customers' ORDER BY columnpos ASC ");
while($row = mysql_fetch_array($result))
{
echo'
<li id="id_' . $row['id'] . '">
<div class="card">
<table>
<tr>
<td>' . $row['customer'] . '</td>
<tr>
<td>P/O: ' . $row['ponumber'] . '</td>
</tr>
<tr>
<td>' . $row['partnumber'] . '</td>
</tr>
<tr>
<td><b>' . $row['quantity'] . '</b> x ' . $row['foil'] . '</td>
</tr>
<tr>
<td>' . $row['daterequired'] . '</td>
</tr>
<tr>
<td>' . $row['prep'] . '</td>
</tr>
</table>
<input class="hiddenid" type="hidden" value="' . $row['id'] . '" />
<input class="hiddenquantity" type="hidden" value="' . $row['quantity'] . '" />
<input class="hiddenpartnumber" type="hidden" value="' . $row['partnumber'] . '" />
<input class="hiddenfoil" type="hidden" value="' . $row['foil'] . '" />
</div>
</li>
';
}
}
?>
However the above code currently prints:
customer1 234345
customer1 w34234
customer1 234345
customer1 w34234
which seems odd... its not going through each customer.
any help is greatly appreciated!
Make only one DB query (better performance) but process the results before looping over them:
$result = mysql_query("SELECT * FROM orders WHERE misc='new' ORDER BY customer ASC ");
$ordersByCustomer = array(); // nested array. 1. level: customers, 2. level: orders
while(($row = mysql_fetch_assoc($result))) {
if(!array_key_exists($row['customer'], $ordersByCustomer)) {
$ordersByCustomer[$row['customer']] = array();
}
$ordersByCustomer[$row['customer']][] = $row;
}
foreach($ordersByCustomer as $customer=>$orders) {
echo ' <li><h1>'. $customer . '</h1></li>';
foreach($orders as $order) {
// rest of HTML code
}
}
I also encourage you to use a better separation of HTML and PHP, like so, using the alternative syntax for control structures:
<?php foreach($ordersByCustomer as $customer=>$orders): ?>
<li><h1><?php echo $customer; ?></h1></li>
<?php foreach($orders as $order): ?>
<li id="id_<?php echo $row['id'];?>">
<div class="card">
<table>
<tr><td><?php echo $order['customer']; ?></td></tr>
<!-- and so forth -->
<?php endforeach; ?>
<?php endforeach; ?>
Btw, you are not generating valid HTML. li element must be contained in ul or ol elements.
use
while($customer = mysql_fetch_array($query){
instead of
$customer = mysql_fetch_array($query);
foreach($customer as $customers)
{

Categories