Showing data correctly in html table with php - php

Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.

change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.

you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.

Related

Display all mysql data in rows one by one

I am fetching all the records from mysql but it split in to rows as shown in the image
I want that it display row wise means the column which are splitting in the column means "future of india" and GAURAV should come in line now row wise. When the body part full it auto split into new row. here is my code
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<table width="30%" border="0">
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>
</table>
Use <span> instead of<div> in the <td> and it will work
You need to move your loop.
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">
while($row = mysqli_fetch_assoc($result)) {
<tr>
<td>
<div id="rcorners2">'.$row["category"].' </div>
</td>
</tr>";
}
echo "</table>";
You should take table wrap all rows.
<?php
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">'
while($row = mysqli_fetch_assoc($result)) {
echo '
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>';
}
echo '</table>';
}
?>
If I understand correctly, I'll write pseudocode
<table>
<tr>
$query = "..."
while ($row = fetch()) {
<td>
print row data
</td>
}
</tr>
</table>

How to go through an array and get values which I want to use in an sql query and store the retrieve value in an another array?

I want to store three variable values in an array. These are HomeResult, Draw and AwayResult. Which I want to use in an another php file. In the image u can se the final website. Problem is I can get the names of the teams and also the value of rows but result is not success.
Or is there another way to achieve this goal ?
I am not sure if it is the goed way to use B,E,F and O variable form the include php.
code of final site;
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
<th>Result</th>
<th>B</th>
<th>E</th>
<th>F</th>
<th>O</th>
</tr>
</thead>
<tbody>
<?php
include 'ff.php';
include 'result.php';
foreach ($champs as $champ ) :
?>
<tr>
<td><?php echo $champ['HOME'];?></td>
<td><?php echo $champ['AWAY'];?></td>
<td></td>
<td><?php echo $champ['B'];?></td>
<td><?php echo $champ['E'];?></td>
<td><?php echo $champ['F'];?></td>
<td><?php echo $champ['O'];?></td>
</tr>
<?php
endforeach;
?>
file result.php
include 'champs.php';
$HomeWins=0;
$Draw=0;
$AwayWins=0;
foreach ($champs as $champ ) :
$B =$champ['B'];
$E=$champ['E'];
$F=$champ['F'];
$O=$champ['O'];
$sql = "SELECT * FROM England where B = '$B' AND E = '$E' AND F = '$F' AND O ='$O' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount=mysqli_num_rows($result);
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['Q'] == 1){
$HomeWins++;
}
elseif($row['Q'] == 0){
$Draw++;
}else{
$AwayWins++;
}
}
$HomeResult =round(($HomeWins/$rowcount )*100);
$DrawResult = round(($Draw/$rowcount )*100);
$AwayResult =round( ($AwayWins/$rowcount )*100);
} else {
echo "0 results";
}
endforeach;
$conn->close();
?>

Not displaying message when no records found

For some reason, the below is not showing the "No results found" error message when querying the database for a selected range, but is displaying the table header and footer.
$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
<?php
if (!$result) { echo("No results found for the selected view");
} else ?>
<table id="results">
<tr>
<th>Rec#</th>
<th>Date</th>
<th>Time</th>
<th>Reading</th>
</tr>
<?php ;
while($row = mysqli_fetch_array($result))
?>
<tr>
<td><?php echo($row['RecID']);?></td>
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
</tr>
<?php
}
?>
<tr>
<td class="footer" colspan="4">- end of report -</td></tr>
</table>
<?php
mysqli_close($con);
?>
Any assistance would be greatly appreciated as theoretically, this should work... Shouldn't it? :-)
$result is probably a result set but it might be empty. yet !$result will not be true. documentation for mysql_query:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. (source: http://php.net/mysqli_query)
you should check with mysqli_num_rows (or something similar)
you can use this condition to check the number of row in query
$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
<?php
if (mysqli_num_rows($result) === 0) {
echo("No results found for the selected view");
} else {?>
<table id="results">
<tr>
<th>Rec#</th>
<th>Date</th>
<th>Time</th>
<th>Reading</th>
</tr>
<?php ;
while($row = mysqli_fetch_array($result))
?>
<tr>
<td><?php echo($row['RecID']);?></td>
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
</tr>
<?php } ?>
<tr>
<td class="footer" colspan="4">- end of report -</td></tr>
</table>
<?php
mysqli_close($con);
?>

Why oci_fetch_array($stid) don't return the first row of oracle database table

I'm trying to make a search form that return some data from an oracle database,it works fine except that it skip the first row of table .
I used the following code :
enter code here
if(isset($_POST['search']) && !empty($_POST["search_box"])){
$name=$_POST['search_box'];
$sql='SELECT deejays.name,available_dates.data, available_dates.venue,available_dates.location FROM deejays,available_dates';
$sql .=" WHERE deejays.name ='{$name}'";
$sql .=' AND pk_id=fk_id';
$verify="SELECT deejays.name FROM deejays WHERE deejays.name ='{$name}'";
$stid = oci_parse($conn,$sql );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){
echo "<table width=\"100%\" align=\"center\" cellpadding=\"5\" cellspacing=\"5\">
<tr class=\"source\">
<td colspan=3 align=\"center\"><h1>The facebook tour dates for ".$name." are:</h1></td>
</tr>
<tr align=\"center\" class=\"source1\">
<td><strong>DATA</strong></td>
<td><strong>VENUE</strong></td>
<td><strong>LOCATION</strong></td>
</tr>";?>
<?php while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){ ?>
<tr align="center" class="rows">
<td ><?php echo $row['DATA'];?></td>
<td ><?php echo $row['VENUE'];?></td>
<td ><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
</table>
<?php } else {
$stid = oci_parse($conn,$verify );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{ echo "This artist hasn't records on facebook/bandsintown<br>
enter code here
the expected result is:
and the real result is:
The second image shows that the first row is skipped,how could I solve this?Thanks in advance
As I commented, oci_fetch_array() will return an array containing the next result-set row of a query. Which means everytime you call it, the cursor will move to the next row. I have two suggestions.
First
Load all the result into an array after oci_execute(). And count the array if it has any row.
oci_execute($stid);
$result = array();
while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
$array[] = $row;
}
//check if result is not empty
if (count($result)) {
//the table part here.
}
And when you want to display it in html table, use foreach loop to iterate the array and build the <tr>.
<?php foreach($result as $row) { ?>
<tr align="center" class="rows">
<t ><?php echo $row['DATA'];?></td>
<td><?php echo $row['VENUE'];?></td>
<td><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
SECOND
You can use oci_fetch_all() to load all the rows from the query into an array.
oci_execute($stid);
//$result will have all the result of the current query
$count = oci_fetch_all($stid, $result);
if ($count) {
//table part here
}
And building the <tr> will be the same as the first.
Another way is to use buffered query. But this will depend on many factors.

How can I delete the selected record in mySQL from my HTML/PHP page?

My idea is to click 'Delete' link and it will pass the id to another PHP page (deleteSession.php), and then execute the query in deleteSession.php. but I couldn't seems to get the id from manageSession.php
In manageSession.php,
<table align='center' border='1' cellpadding='5' cellspacing='0'>
<tr>
<th>Session Id</th>
<th>Type</th>
<th>Date & Time</th>
<th>Venue</th>
<th>Pax</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available ORDER BY dates_sessions asc";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//mysqli_close($link);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['booking_id']; ?></td>
<td><?php echo $row['booking_types']; ?></td>
<td><?php echo $row['dates_sessions']; ?></td>
<td><?php echo $row['venue_available']; ?></td>
<td><?php echo $row['room_count']; ?></td>
<td><input type="button" value="Delete"/></td>
<td><input type="button" value="Edit"/></td>
</tr>
<?php } ?>
</table>
In deleteSession.php,
<?php
include "dbFunctions.php";
include "manageSession.php";
//$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available";
//$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$bookingId = filter_input(INPUT_GET, 'booking_id');
$deleteQuery = "DELETE FROM bookings_available WHERE booking_id = '$bookingId'";
?>
I think in deleteSession.php file code should be as follows.
$bookingId = filter_input(INPUT_GET, 'id');
OR
$bookingId = $_GET['id'];
Because you are passing get parameter as follows.
deleteSession.php?id=
And also keep anchor as follows.
Delete
In the deleteSession.php you can try and replace:
$bookingId = filter_input(INPUT_GET, 'booking_id');
with the below code:
$bookingId = $_REQUEST['id'];
Finally at the last line you have to execute the query which is stored in $deleteQuery variable, which is not executed yet by using below code:
$qry = mysql_query("DELETE FROM bookings_available WHERE booking_id = '$bookingId'");
//will show you error if not able to delete
if(!$qry)
die("Error: ".mysql_error());
Added this at line 3 and it works:
mysqli_select_db($link ,$DB);
Because in the code I have not selected the mysql database and also the query was not executing as the first parameter $link was missing.

Categories