Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am going to develop a online ticket booking system. I have two database table coach_shedule and seat. coache_shedule contains the coach details information with coach_id as primary key and table seat contains seat that is booked from online.
table "coach_shedule"
coach_id cdate ctime coach_no route
table "seat"
seat_id name seat_no coach_id
I have three select field on browser that returns route, cdate and ctime
using this three field I have get coach_id from coach_shedule table using select query
and I save the seat in table seat that is booked from browser.
Every coach has 40 seat. I want to show on browser using php how many seat is booked of each coach coach.
Here is my problem,
In my code I wanted to show the number of seat of each coach that is booked and I have written code in the last column of html table named "seat booked". but it does not show the proper result. I think I explained my problem properly. Please help me. If you want to know specific point. Please ask me.
<form action="coach_time.php" method="post"><table width="100%" style="font- size:small">
<tr>
<td width="30"></td>
<td>Route</td><td>
<select name="route" size="1" class="selhead" id="cdate" onchange="this.form.submit()">
<option value="Dhaka-Barisal"<?php if($route=="Dhaka-Barisal") {echo "selected";} ?> onclick="this.parentNode.form.submit()">Dhaka-Barisal</option>
<option value="Barisal-Dhaka"<?php if($route=="Barisal-Dhaka") {echo "selected";} ?> onclick="this.parentNode.form.submit()">Barisal-Dhaka</option>
</select>
</td>
</tr></form>
<table width="100%" style="font-size:small">
<tr>
<th>Serial</th>
<th>Date</th>
<th>Time</th>
<th>Coach Type</th>
<th>Day Night</th>
<th>Fare</th>
<th>Seat booked</th>
</tr>
<?php
$sql=mysql_query("select * from coach_shedule where route='$route' and cdate='$cdate'");
$a=1;
while($data=mysql_fetch_row($sql))
{
?>
<tr>
<td align="center"><?php echo $a; ?></td>
<td><?php echo $data[1]; ?></td>
<td><?php echo $data[2]; ?></td>
<td><?php echo $data[4]; ?></td>
<td><?php echo $data[5]; ?></td>
<td><?php echo $data[6]; ?></td>
<td><?php
$coachid=$data[0];
$sql1=mysql_query("select seat_no from seat where coach_id='$coachid'");
$data1=mysql_num_rows($sql);
echo $data1;
?></td>
</tr>
<?php
$a++;
}
?>
</table>
You got an error here:
'$sql1=mysql_query("select seat_no from seat where coach_id='$coachid'");
$data1=mysql_num_rows(**$sql**);'
should be
$data1=mysql_num_rows(**$sql1**);'
You are using the wrong SQL there.
Edit: more explanation
Related
An HTML table is given in which there are people (agents in this case) who have the following information displayed Id, Name, Rank, Location, and Status. Status can have 3 values. Online, Offline, Disconnected. Based on the status that one of the agents has I want to put 3 buttons as such Online, Offline, and Disconnected, and when a button is pressed to hide the other rows with different values. For example, if I press Online, table rows on the HTML side that contain the status Offline and Disconnected disappear, this goes for the others too the other way around. I have no idea to achieve what I said earlier and I am open to any solution that is deemed to resolve my problem.
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
$result = mysqli_query($con,"SELECT * FROM agents");
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
Try using the below logic : Create 3 buttons with <a> and pass status value as GET to the same page then while fetching check whether it has GET or not
Note:This is not a answer :passing direct GET value to sql query may cause some security issues
Online//put the status value based on your status
Offline//put the status value based on your status
Disconnected//put the status value based on your status
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
if(isset($_GET['status']))
{
$result = mysqli_query($con,"SELECT * FROM agents where agentStatus='".$_GET['status']."'");
}
else
{
$result = mysqli_query($con,"SELECT * FROM agents");
}
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
I'd use javascript within a tag or jQuery since this sounds like you need dynamic functionality on a static webpage.
For your button pass in the name of the function you're creating inside your tag
<button onclick="yourFunctionName(yourString)"></button>
Inside your tag have the function accept the yourString paramater
function yourFunctionName(yourString){
if(yourString === "whatever"){
$('tbody tr').hide()
$('tbody tr').show()
}
etc! Let me know if you need any more clarification on anything
Output echo an attribute on each row <tr data-status="online"> etc.
Then in script use document.querySelectorAll("tr[data-status='online'] to find all rows with that status, loop thru the array and toggle each row style.display to 'table-row' or 'none' as required.
You could also do the same using CSS by assigning a class <tr class="online"> etc. Then in script dynamically altering the CSS definition of that class, changing the display to 'table-row' or 'none' as required.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I will try my best to explain what I am trying to do. Hopefully you can help me out. I have a database that holds links, these links are displayed in a table. I have the entries output properly in order from Points, but I am trying to add a rank number to the side so it says 1, 2, 3, 4, etc going down the page per entry.
Here is my attempt.
<table width = "1000" style='table-layout:fixed;'>
<tr>
// These are the numbers I need to add
<th>Rank</th>
// All these work fine.
<th>Host</th>
<th>Location</th>
<th>Points</th>
</tr>
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<tr>
<td><?php echo $row1[0]; ?></td>
<td><?php echo $row1[1]; ?></td>
<td><?php echo $row1[2]; ?></td>
</tr>
<?php endwhile; ?>
</table>
Hopefully you understand what I am trying to do, if you need any more information let me know.
Thanks for your time.
you mean? :
<?php $rankId=0; while($row1 = mysqli_fetch_array($result1)){ ?>
<tr>
<td><?=$rankId++ ?></td>
<td><?=$row1[0] ?></td>
<td><?= $row1[1] ?></td>
<td><?=$row1[2] ?></td>
</tr>
<?php } ?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I m trying to run two loops the first query in retrieves the column .The second query takes the primary id of the first table and executes the query.
Final result should be like given in the image
Im not able to give the proper alignment
Below is the code
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas= mysql_fetch_assoc($mast))
{
?>
<tr>
<td><?php echo $res_mas['col1'];?></td>
<?php
$room= mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
while($res_room= mysql_fetch_assoc($room))
{
?>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</td>
</tr><tr>
<?php }?>
</tr>
<?php } ?>
</table>
This should do it:
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas = mysql_fetch_assoc($mast)) {
$room = mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
$count = 0;
while($res_room= mysql_fetch_assoc($room)) {
$count += 1;
?>
<tr>
<td><?php echo $count === 1 ? $res_mas['col1'] : "";?></td>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</tr>
<?php } } ?>
</table>
(untested)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to sum of a column
<table width="240" border="1">
<tr>
<td width="94">Name</td>
<td width="130">Current Amount</td>
</tr>
<?php
do {
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php
}
while ($row_Record = mysql_fetch_assoc($Record));
?>
<tr>
<td colspan="2">Total = </td>
</tr>
</table>
I don't know how to do this kind of sum.
Please any one help me.
Thank you.
By using Jquery, you can do something like that
$(document).ready(function(){
var sum = 0
$(".sum").each(function(){
sum += parseFloat($(this).text());
});
alert(sum);
});
Jsfiddle
Alternatively, you can sum all the values by using PHP when fetching the values.
Something like that:
$sum = 0;
while($read = mysqli_fetch_array()){
$sum += $read['row'];
}
echo $sum; //total
<table width="240" border="1">
<tr>
<td width="94">Nmae</td>
<td width="130">Current Amount</td>
</tr>
<?php
$tmp=0;
do {
$tmp+=$row_Record['current_amount'];
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php } while ($row_Record = mysql_fetch_assoc($Record)); ?>
<tr>
<td colspan="2">Total = <?php echo $tmp; ?></td>
</tr>
</table>
I have two tables that I want to use for viewing my reports which I can get after inputting a date.
Here are my tables: for customers - customer_date, lastname, firstname
for services - room_number, date_in, date_out
Here is my code now : it seems that it can't get any rows from my table
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db('irm',$conn);
if(isset($_GET['Submit'])){
$customer_date = $_GET['customer_date'];
}
?>
<form method="get">
<table width="252" border="0">
<tr>
<td width="98">Choose Date:</td>
<td width="144"><label>
<input onclick="ds_sh(this);" name="customer_date" id="customer_date" readonly="readonly" style="cursor: text" />
</label></td>
</tr>
<tr>
<td align="right"><input type="submit" value="Submit" /></a></td>
<td></td>
</tr>
</table>
</form>
<form>
<?php
$tryshow = "SELECT * FROM customers,services WHERE customer_date = '$customer_date' ";
$result = #mysql_query($tryshow,$conn)
or die("cannot view error query");
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print...";
}
while($row=mysql_fetch_assoc($result)){
?>
<table width="700" border="0">
<tr>
<td width="100">Customer Date:</td>
<td width="100">Last Name</td>
<td width="100">First Name</td>
<td width="100">Room Number</td>
<td width="100">Date In</td>
<td width="100">Date Out</td>
</tr>
<tr>
<td><?php echo $row["customer_date"]; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['room_number']; ?></td>
<td><?php echo $row['date_in']; ?></td>
<td><?php echo $row['date_out']; ?></td>
</tr>
</table>
<?php }?>
</form>
With this I can get a report of any customer who checks in on that date.
I need some advice. Hope you can answer me soon.
You don't appear to have any fields in common between the two tables. How do you store fact that customer A was in room B on date C? To do an SQL join, the tables being joined have to have at least one field in common.
As well, instead of just saying die("cannot view error query"), which is utterly useless for debugging purposes, try doing die(mysql_error(), which will give you the exact reason the query failed.
As well, if the query DOES work, then you're outputting an entire HTML table for each row found. You should have the table headers and footers data OUTSIDE of the fetch loop.
You need to relate the two tables with a JOIN. Based on the information given, customers.customer_date to services.date_in seems to be the most likely candidate. This assumes that the date columns hold only a date and not a date/time.
Also notice that I'm not using select * in my query and neither should you. ;-)
SELECT c.customer_date, c.lastname, c.firstname,
s.room_number, s.date_in, s.date_out
FROM customers c
INNER JOIN services s
ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date'
When your making query to database make sure your date format is yyyy-mm-dd
Mysql understand date in this format only so that you have to compare the date format in this format only.
your $customer_date should be in the yyyy-mm-dd format
As an aside, I'd change customer_date to something more meaningful such as "date_in." (It's a good thing when the names are predictable!) You don't need to specify that it's the customer since it's in the customer table already.