hotels_data table
hotel_rooms table
MYSQL QUERY
$pdo = $dbo->prepare('SELECT distinct hotels_data.*, hotel_rooms.hid, hotel_rooms.room_name, hotel_rooms.price
FROM hotels_data
LEFT OUTER JOIN hotel_rooms
ON hotels_data.hotel_id = hotel_rooms.hid
');
OUTPUTING THE DATA
i loop the Hotel rooms
$row = $pdo->fetchAll(PDO::FETCH_OBJ);
foreach($row as $rows){
print '
<!-- Listing -->
<div class="list_hotels_box">
<ul class="list_hotels_ul">
<li style="display:block;">
<img src="../img/hotel_img.png" alt="" style="width: 180px; height:180px;"/>
</li>
<li style="width: 100%;">
<h3 class="clear-top">'.$rows->hotel_name.'<span><?php print $star; ?></span></h3>
<p>'.$rows->hotel_address.', '.$rows->hotel_state.''.$rows->hotel_country.' </p>
<div id="rooms">
<p>
<table class="table table-condensed table-striped table-hover table-bordered">
';
foreach($row as $td){
if($td->hotel_id == $rows->hid){
print
'
<tr>
<td>'.$td->room_name.'</td>
<td align="right" style="color: darkorange;"><strong><sup>RM</sup>'.$td->price.'</strong></td>
</tr>
';
};
} ;
print '
<tr>
<td colspan="2">See All Rooms</td>
</tr>
</table>
</p>
</div>
</li>
<div align="right">
<li>
<h4><small>Rating</small> <strong>8.5</strong></h4>
<span class="help-block clear">"0" Reviews</span>
<button onclick="alert(\'Not operational yet\')" class="btn btn-sm btn-info">Book Now</button>
</li>
</div>
</ul>
</div>
<!-- Listing -->
';
Here is the output
i have Hotel Sample & Hotel Sample 2 but Hotel Sample has outputted 2
i think its bcoz of the 2 rooms
so my problem is how do i remove the duplicate data
Please assist me. TQ
sorry if i didn't make myself very clear
One way would be to simply split to 2 queries, like:
SELECT * FROM hotels_data;
And then rooms query inside your hotels loop:
SELECT hid, room_name, price FROM hotel_rooms WHERE hid = :hotel_id
This assumes you are using binded parameters and bind the $rows->hotel_id to :hotel_id parameter.
You could also use GROUP BY hotel_id, but then you won't get all the rooms for your hotels, unless you use GROUP_CONCAT() and parse the concatenated rooms values.
Related
Is there other ways to select classes with same name but in different table rows?
I use html class="className + id" Than in jquery to select $('.className'+id)... some code
I would like to avoid using the id each time. Some suggestions?
Thank you
HMTL
Data retrieved from the database
<tr class="row" id="<?= $id;?>"> <!-- id 1 -->
<td class="atyp<?= $id;?>"> <?= $v['atyp'];?> </td>
<tr class="row" id="<?= $id;?>"> <!-- id 2 -->
<td class="atyp<?= $id;?>"> <?= $v['atyp'];?> </td>
JQUERY
<script>
$('.atyp'+id).val() // some code....
</script>
So the table has multiple rows with same class name and I would like to avoid to use id to select a specific class
Don't put <?= $id;?> to your classes:
<tr class="row" id="<?= $id;?>"> <!-- id 1 -->
<td class="atyp atyp<?= $id;?>"> <?= $v['atyp'];?> </td>
<tr class="row" id="<?= $id;?>"> <!-- id 2 -->
<td class="atyp atyp<?= $id;?>"> <?= $v['atyp'];?> </td>
$('.atyp').val()
You should not give multiple HTML elements the same ID.
You can select the first td with the :first-of-type selector
$("td:first-of-type")
In general you can use the :nth-of-type selector.
$("td:nth-of-type(42)")
I am working on a website where I am getting input of semester number from the user and I want to display the subjects in the semester they entered accordingly.
For this, I planned to create tables for each semester which would have the subjects in it.
For now, I am successful to retrieve data from one table. But I am required to get subjects as per the entered semester by the student.
Following is the code,which works perfectly for semester 5. Please suggest alterations as per the requirements asked above.
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- very important -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="subject.css">
</head>
<body>
<h1 class="text-center text-capitalize font-weight-bold">Subject Information</h1>
<div class="container">
<div>
<br>
<div>
<form class="form-inline">
<!-- <div class="mx-auto" style="width: 200px;"> -->
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<br>
</div>
</form>
</div>
</div>
</div>
<div>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= "select * from sem5";
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>
Please suggest alterations. Will be highly obliged.
You need to do a HTTP GET request and use PHP to get the value. Then use PHP to make the SQL query. Here it is in its simplest form.
I've added a submit button which submits the form and executes the HTTP GET with the parameters attached to the URL. You will see ?sem_no=x after it is submitted. That's the Query String.
...
...
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<div>
<input type="submit"/>
</div>
<br>
...
...
...
...
//check the value was submitted and it is not blank
<?php if(isset($_GET['sem_no']) && !empty($_GET['sem_no'])) { ?>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= 'select * from sem'.$_GET['sem_no'];
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<?php
//closing if
} ?>
...
...
As mentioned in the comments earlier, this database design is quite poor. I know you are learning though. Have you considered having a subjects table that has a Semester column? Then you could do a query like:
SELECT * FROM subjects where semester_number = x
There are even better ways to do it beyond that but this would be a good starting point for you.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am struggling with the syntax if someone can help me.
Condition1 = if the value of $ncha['p_no_contacts']-$ncha['r_cnt'] is 1 then add an extra echo in the h1 section saying that, "You are viewing last contact details and have exhausted your membership. Please renew your membership" If the above value is more than 1 then let that one echo statement be as it is.
next thing is to update the payments table or delete the row from payments table if $inc1 is equal to $ncha['p_no_contacts']
I need a correct syntax if someone can help.
EDIT:
This is what I tried but I get error Parse error: syntax error, unexpected '>' on line 51 which is the h1 line of else statement.
Thanks
<div class="modal-dialog yoyo-large">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×
</button>
if $ncha['p_no_contacts']-$ncha['r_cnt'] = 1
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
}
else
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']) <br> You are viewing last contact details and have exhausted your membership. Please renew your membership;?>)
</h1>
</div>
<div class="modal-body">
<div class="col-sm-12 form-group">
<div class="col-sm-6" style="font-size:13px;">
<table class="table table-hover table-striped">
<tr height="30">
<td width="80">
<strong>Matri ID :
</strong>
</td>
<td>
<?php echo $fet['matri_id']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Name :
</strong>
</td>
<td>
<?php echo $fet['username']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Address :
</strong>
</td>
<td>
<?php echo $fet['address']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Phone :
</strong>
</td>
<td>
<?php echo $fet['phone']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Mobile :
</strong>
</td>
<td>
<?php echo $fet['mobile']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Email :
</strong>
</td>
<td>
<?php echo $fet['email']; ?>
</td>
</tr>
</table>
</div>
</div>
<?php
$chk1=$ncha['r_cnt'];
$inc1=$chk1+1;
If $inc1 = $ncha['p_no_contacts']
(
$dele="delete from payments where (pemail='$mid' or pmatri_id='$mid')";
$de=mysql_query($dele) or die(mysql_error());
}
else
{
$upda="update payments SET r_cnt='$inc1' where (pemail='$mid' or pmatri_id='$mid')";
$up=mysql_query($upda) or die(mysql_error());
}
$ex=mysql_query("select id from today_contact where who='$mid' and whose='$from_id'");
if(mysql_num_rows($ex)==0)
{
mysql_query("insert into today_contact (who,whose,on_date) values ('$mid','$from_id',now())");
}
else
{
mysql_query("update today_contact set on_date=now() where who='$mid' and whose='$from_id'");
}
?>
</div>
</div>
</div>
You are mixing HTML and PHP code together.
You also forgot parentheses around the condition on the first line of this code fragment.
Change:
if $ncha['p_no_contacts']-$ncha['r_cnt'] = 1
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
}
else
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
to:
<?php
if ($ncha['p_no_contacts']-$ncha['r_cnt'] = 1)
{
?>
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
<?php
}
else
{
?>
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
Other important things:
- mysql_* functions in PHP are all deprecated, and removed from the last version of PHP (PHP 7). You should use mysqli_* or PDO functions instead
- it seems you are using unescaped variables in your queries. This is very insecure and may cause your code to fail, or be the target of SQL injections. Read How can I prevent SQL-injection in PHP? for more information.
I got the answer. By mistake I added a parenthesis instead of brace bracket in the second line of the below code. Phew! It took me almost two days to figure this out. Thanks #Jocelyn for your kind assistance.
If $inc1 = $ncha['p_no_contacts']
(
$dele="delete from payments where (pemail='$mid' or pmatri_id='$mid')";
$de=mysql_query($dele) or die(mysql_error());
}
else
{
$upda="update payments SET r_cnt='$inc1' where (pemail='$mid' or pmatri_id='$mid')";
$up=mysql_query($upda) or die(mysql_error());
}
I'm running into a strange scenario involving a widget and the lay-out. I've created a layout that overrides the original column 2 lay-out. It calls a widget in the file and in that widget display a table on the side-bar of some information that I would like to show the user. The layout sidetable.php looks like this
<?php $this->beginContent('//layouts/main'); ?>
<div class="row-fluid">
<div class="span3">
<?php
$this->widget('ListSummaryWidget', array('totaldue'=>$totaldue));
?>
<table class="table">
</table>
</div><!-- sidebar span3 -->
<div class="span9">
<div class="main">
<?php echo $content; ?>
</div><!-- content -->
</div>
</div>
<?php $this->endContent(); ?>
Now all this works - displaying a datatable on the left hand column of the screen. However, something strange happens. Whenever I git rid of
<table class="table">
</table>
The whole view breaks - showing a ridiculous structure/layout that doesn't look much at all like the original. This is confusing/intriguing. In my widget I declare the exact same table and yet it does not seem to matter that I declare this table. Here is the code for my widget's view
<table class="table list_summary table-bordered">
<form action="<?php echo Yii::app()->createUrl('recipient/processpayment', array('id'=>$id)) ?>" method="post" >
<tr class="primary"><td> <h4>List Summary </h4> </td></tr>
<tr ><td>
<?php echo $numpeople; ?> Recipient(s)
</td></tr>
<tr ><td>
Total Due: <?php echo $totaldue ?> Rwf
</td></tr>
<tr> <td>
<h4> Mobile Money Accounts: </h4>
<?php
/*
foreach($accounts as $account)
{
echo $account->name; ?>: <?php echo $account->balance; ?> Rwf<br> <?php
}
*/
?>
</td> </tr>
<tr> <td>
<button class="btn btn-block btn-primary " type="submit">Pay Now</button> </td> </tr>
</td></tr>
</form>
Could anyone explain why this is happening? Though it's not the worst thing in the world - I'm really intrigued as to why I'm required to have some html when I have the same html in the widget's view.
Resolved:
I didn't end the table in my widget. I should have added at the end
</table>
I've got a new online store written in PHP and MySQL.
<div class="content-area">
<div class="page-heading">
<h1>Store</h1>
</div>
<p style="padding-top: 5px;"><strong>You are here:</strong> Home » Store</p>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<?php
$categories=mysql_query("SELECT * FROM categories WHERE parent='0' ORDER by owner ASC, title ASC");
while($categoriesRow=mysql_fetch_array($categories)) {
$categoriesSub=mysql_query("SELECT * FROM categories WHERE parent='$categoriesRow[id]'");
?>
<tr>
<td valign="top">
<div class="product_list">
<div class="image_product">
<img alt="<?php echo $categoriesRow['title']; ?>" src="<?php echo $cls->truska(true); ?>/theme_section_image.gif" border="0" style="vertical-align: middle;" />
</div>
<div>
<h3 class="product"><?php echo $categoriesRow['title']; ?> <?php if(mysql_num_rows($categoriesSub) > 0) { ?>(<?php while($categoriesSubRow=mysql_fetch_array($categoriesSub)) { }?>)<?php } ?></h3>
</div>
</div>
</td>
</tr>
<tr>
<td class="dotted_line_blue" colspan="1">
<img src="<?php echo $cls->truska(true); ?>/theme_shim.gif" height="1" width="1" alt=" " />
</td>
</tr>
<?php
}
?>
</table>
</div>
Where my second While loop is, I need to work out what is the last result, so I can omit a comma from my while loop.
It will show as 'Lego (Lego City, Lego Starwars,)' but I want it to show as 'Lego (Lego City, Lego Starwars)'.
How can I get if the current result is the last?
You can fix this by coming at it from the other direction.
Instead of appending a comma after each result except the last, try pre-pending a comma on every result except the first.
Set up a variable called $first outside your loop, and set it to 1. Inside the loop:
if ($first == 0) {
echo ",";
} else {
$first = 0;
}
don't add the comma if it is the first result, and add it before in all the next ones.
Just build your array of results and implode it. This takes care of any counting automatically:
$comma_separated = implode(",", $array);
You shouldn't use plain mysql access, look at PDO.
Answering your question, try something like this:
$items = array();
while ($row = mysql_fetch_assoc($result)) {
$items[] = $row['foo'];
}
echo implode(', ', $items);