Hello Fellow Stack Users, I have an issue. I am running a booking system and I'm in the process on changing some of the code to ajax so that the user does not have to be redirected. My database has colums. one date and the other one time. the code querys the database and gets one result for each date. december 1, december 2 etc and doesn't show the times. The issue i am having is that i can not run code which i can click the date and it will query the database to find all the time slots associated with that date.
some code below
index.php
$sql = "SELECT DISTINCT day, hour FROM availability WHERE booked='' and day >= CURDATE()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$next = date("M jS ", strtotime("".$row["day"] .""));
echo "<a class='button' style='margin:10px;' href=fetch.php?pageid=" . $row["day"] . "> <p style='font-size:20px; margin:0px;'>$next</p> ". $row["hour"]. "</a>";
}
} else {
echo "You are booked up for the week";
}
$conn->close();
fetch.php
$sql = "SELECT DISTINCT day FROM availability WHERE booked='' and day >= CURDATE()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<a class='button' style='margin:10px;' href=book_process.php?pageid=" . $row["id"] . "> <p style='font-size:20px; margin:0px;'>$next</p> ". $row["hour"]. "</a>";
}
} else {
echo "You are booked up for the week";
}
$conn->close();
in fetch you should click a time and then it should update the row in the process page i have not included. I am not very fluent coding ajax but i thought that now is the time to learn.
Related
I'm trying to fetch and display all user information from mysql database but it doesn't able to show the time on row, but the rest information such as User ID and Username can be displayed. If I run the system in my windows localhost it's just working fine but when I run it using web hosting it doesn't show the date.
Database :
Database Record :
My current coding :
<?php
$sql_select = "SELECT * FROM tbluserinfo";
$result = $conn->query($sql_select);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['user_id']."</td>";
echo "<td>".$row['user_name']."</td>";
echo "<td>".$row['user_created_date']."</td>";
echo "</tr>";
}
}
else
{
echo "<td colspan='3' style='color: red;'>No record found.</td>";
}
?>
You could try :
echo date('j F, Y', strtotime($row['user_created_date']));
and make sure your $row['user_created_date'] is not null otherwise it will give you this date:
1 January, 1970
I'm trying to get rows from my database between two dates. I've used other questions here to get to this point, but I don't know how to move forward from here.
Here is the code, and underneath are the screenshots of the output and a photo running the query in SQL.
Thanks for the help in advance!
--
This first section of code outputs the ID, name, etc.
<?php
$sql = "SELECT * FROM songs ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This section of code outputs "0 Results"
<?php
$from_date = '2015-01-01';
$to_date = '2017-04-04';
$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date . "' AND '" . $to_date . "' ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This is what the page looks like with the above code.
I've successfully selected the rows in SQL.
EDIT: I made a mistake when writing the code into the question for the IF ELSE statement on the 2nd block of code. I just updated it to what I actually have on the site. I didn't change anything based on the solutions, and am still not getting the rows to print.
You done everything right, except using while/else ;
Your code (2-nd) section should look like this:
if ($result->num_rows > 0) {
while () {
//print your rows
}
} else {
//Say that there's 0 results
}
Your results are listed - as you can see from your screenshot. Problem: While along with else does not make sence.
your last example is missing a } so it says: while { } else { echo "0 results"}
But the echo "0 results" should be related to the num_rows-check, not to the while.
Ah man, I just needed to had to comment out this line from the first block:
$link->close();
I apologize to everyone who answered if it wasn't clear that the blocks of code followed one another. Anyways, for anyone else with the same problem, don't close the connection -_-
Here's what is output when I comment out the line from the first block
I'm making a survey tracking website and I'm having trouble. I want to display all surveys that have been completed in the last 7 days. I'm using mysqli_fetch_row to see if any rows are retrieved, and if they are display them. If they aren't any compelted in the last 7 days, I want it to display the words "no recently compelted surveys to show."
<?php
require('db/connect.php');
if (!isset($_GET['sort'])) {
$sort = 'client_id';
} else {
$sort = $_GET['sort'];
}
if ($result = $db->query("SELECT client_id, date_added, client, email, date_sent, date_completed FROM clients NATURAL JOIN surveys WHERE date_completed BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() ORDER BY $sort")) {//shows surveys completed in the last 7 days
if (mysqli_fetch_row($result) == 0) {
echo "No recently completed surveys to show.";
} else {
echo "<table>";
echo "<tr><th><a href='portal.php?sort=client_id'>ID</a></th><th><a href='portal.php?sort=date_added'>Date Added</a></th><th><a href='portal.php?sort=client'>Client</a></th><th><a href='portal.php?sort=email'>Email</a></th><th><a href='portal.php?sort=date_sent'>Sent</a></th><th><a href='portal.php?sort=date_completed'>Completed</a></th>";
$rows = $result->num_rows;
for ($num = 0; $num < $rows; ++$num) {
$row = $result->fetch_array(MYSQLI_NUM);
$client_id = $row[0];
$date = $row[1];
$client = $row[2];
$email = $row[3];
$sent = $row[4];
$completed = $row[5];
echo "<tr>";
echo "<td>$client_id</td>";
echo "<td>$date</td>";
echo "<td>$client</td>";
echo "<td>$email</td>";
echo "<td>$sent</td>";
echo "<td><a href='survey/completed/index.php?id=$client_id'>$completed</a></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
When I remove the if clause for mysqli_fetch_row, it displays ALL of the recently completed surveys, but if I leave it in, it ALWAYS leaves one out. Can anyone help?
You are discarding the first when you do the test. You can get the number of rows like this instead:
if ($result->num_rows == 0) {
echo "No recently completed surveys to show.";
} else {
// ....
}
Docs : mysqli_result::$num_rows
I'm really stuck on this guys, and I know where the fault is but I can't seem to fix it.
The while loop itself works it's the if that is causing all the trouble.
Whenever there are 3 groups in the database it only displays 2 the strange part it will only display the items in the while loop. But the first item is somehow tied to the if statement.
public function get_group($user_id){
$sql3="SELECT * FROM groups WHERE user =
$user_id";
$results = mysqli_query($this->db, $sql3);
$user_data = mysqli_fetch_array($results);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()){
echo "hello";
echo "group :" . $row["group"] . "<br>";
echo "groupdesc:" . $row["groupdesc"] . "<br>";
echo "<a class='btn btn-primary' href='project.php?groupid=" . $row["groupid"] . "'>></a>";
}
}
else {
echo "0 results";
}
}
It seems it can't output the first one the first one just keeps hidden.
You fetch your first row in this line:
$user_data = mysqli_fetch_array($results);
And you do nothing with it.
And please do not mix procedural and object-oriented usage of mysqli.
My apologies if this has been addressed - I have been searching all day and haven't found anything that meets my needs.
I have a foreach loop in php that is repeating information ad nauseum, and I am not sure how to fix it. I have tried grouping, array_unique, etc., and haven't found a solution. No row is a true duplicate of another when all variables are taken into account.
I have a table where each line represents a winning ticket. Each line has a unique ID, a date, a tier (1,2,3), and some other variables worth of information. I would like to organize this by date, ascending by tier number, where the date is listed once and each ticket is listed once. Right now, each row is listed as a ticket, but repeats multiple times. Here is my code - I have removed the displays in between as those work fine:
$selectResults = "SELECT * FROM mytable WHERE YEAR(date) = 2014 ORDER BY date DESC, tier ASC";
$getResults = #mysqli_query($connect, $selectResults) or die('query error: ' . mysqli_error($connect));
if(mysqli_num_rows($getResults) == 0){
echo "There are no tickets to display.";
}else{
echo "<table><tr><th>Date</th><th>Tier</th><th>Points</th><th>Prize Amount</th></tr>";
while($row = mysqli_fetch_array($getResults)){
extract($row);
foreach($row as $ticket => $date){
echo "<tr><td>" . date('n/j/Y', strtotime($date)) . "</td><td> </td><td> </td><td> </td></tr>";
if (1 == $tier)
{
(Display Tier 1 tickets for date)
}
if (2 == $tier)
{
(Display Tier 2 tickets for date)
}
if (3 == $tier)
{
(Display Tier 3 tickets for date)
}
}
echo "<tr class='bottomRow'><td colspan='4' /></tr>";
}
echo "</table>";
Get rid of the foreach loop:
foreach ($row as $ticket => $date)
You're already looping over the rows with the while loop. $row just contains a single row, with each column as an element of the array.
Your code should look like:
$last_date = null;
while ($row = mysqli_fetch_assoc($getResults)) {
extract($row);
if ($date != $last_date {
echo "<tr><td>" . date('n/j/Y', strtotime($date)) . "</td><td> </td><td> </td><td> </td></tr>";
$last_date = $date;
}
// Display ticket details
}