How to display MySQL data based on a column field - php

I am trying to make an appointment maker for potential customers. The hours that are available will depend greatly on personal schedules, so I don't want a generic calendar type appointment maker. What I was trying was an SQL DB with 4 columns [ID, Made, Date, Time] with the following types [INT, BIT, VARCHAR, VARCHAR].
When SELECTing my data and displaying it, I am attempting to have an IF statement (PHP) determine if "Made" is "00" or "01" - 00 being "appointment available", 01 being "taken".
The output does display all the rows; however, it is showing all the rows as "appointment available". One of my rows has "01" in the "Made" column, and it is still showing as available.
PHP/SQL Script after connection:
$sql = "SELECT * FROM $tname";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["Made"] = '00') {
echo "<tr><td>" . $row["ID"] . "</td><td>" . 'Make An Appointment' . "</td><td>" . $row["Date"] . "</td><td>" . $row["Time"] . "</td></tr>";
}
elseif ($row["Made"] = '01') {
echo "<tr><td>" . $row["ID"] . "</td><td>" . 'Reserved' . "</td><td>" . $row["Date"] . "</td><td>" . $row["Time"] . "</td></tr>";
}
}
}
This is the link to the output on the website:
http://www.jpegchaos.com/appointment.php
I will continue to re-upload the .php with any attempts to see if new information displays.
Line 2 should show "Reserved" while 1 and 3 should be "Make an Appointment"
Thank you in advance

You need to use ==, not = when you're checking the $row["Made"]. As you have it now, the first if statement is always true, so it never gets to the elseif. Try this:
$sql = "SELECT * FROM $tname";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["Made"] == '00') {
echo "<tr><td>" . $row["ID"] . "</td><td>" . 'Make An Appointment' . "</td><td>" . $row["Date"] . "</td><td>" . $row["Time"] . "</td></tr>";
}
elseif ($row["Made"] == '01') {
echo "<tr><td>" . $row["ID"] . "</td><td>" . 'Reserved' . "</td><td>" . $row["Date"] . "</td><td>" . $row["Time"] . "</td></tr>";
}
}
}

Related

reading seconds from mysql and convert to hours

So im basically a complete beginner in php/mysql this is the current code:
$sql = "SELECT id, userid, date, rig, hash, miner_hashes, temp, fanrpm, rack_loc,ip, uptime, gpus, driver, cpu_temp, miner, miner_version, mem, powertune, fanpercent, voltage, miner_secs, version FROM hash WHERE rig='bbf3ec' ORDER BY ID DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["version"]. "</td><td>"
. $row["driver"] . "</td><td>"
. $row["gpus"] . "/8</td><td>"
. $row["miner"] . "</td><td>"
. $row["miner_version"] . "</td><td>"
. $row["miner_secs"] . "</td><td>"
. $row["rig"] . "</td><td>"
. $row["rack_loc"] . "</td><td>"
. $row["ip"] . "</td><td>"
. $row["cpu_temp"] . "c</td><td>"
. $row["hash"] . "</td><td>"
. $row["miner_hashes"] . "</td><td>"
. $row["temp"] . "</td><td>"
. $row["voltage"] . "</td><td>"
. $row["fanpercent"] . "</td><td>"
. $row["powertune"] . "</td><td>"
. $row["mem"]. "</td></tr>";
}
echo "";
} else { echo "0 results"; }
Currently I'm just printing miner_secs value as INT from database, but I want it to convert to hours first and then to insert it into a row, any idea on how to do that?
Just once again I'am a complete beginner in this stuff, every explanation is highly appericated!
You can still do calculations on the minor_secs even if it is a string.
. round(($row['miner_secs']/60)/60,2) . "</td><td>"
I'm dividing the output by 60 and then 60 again. Then rounding it to avoid insane precision.

PHP MySQL HTML Table - Freeze Panes (Multiple Frozen Columns)

Really stuck on this one. Spent a couple hours fiddling and no results so asking here...
Tried solutions from html/CSS to Javascript and jQuery, but couldn't get anything to work. Basically trying to accomplish freeze panes like in Excel with the below table. For this I am most interested in freezing the first 4 columns. I might be interested in freezing the first 'header' column too, but not as important.
This is an example, but I will likely use this is a situation where I have multiple tables on a single page (so thinking if we had a frozen header it would need to stop at the end of each table).
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "
SELECT fac_id, abc_id, provider_id, fac_name, status, lob, service_level, rate, start_date, end_date, fiscal_month_end, month_start, region, area, bd, pd, phone_pd, ps, total_beds, licensed_beds, address, city, state, zip, phone_office, logo_1, logo_2
FROM table_db
WHERE status = 'ACTIVE' AND lob = '123' AND area = 'Smith'
ORDER BY region ASC, area ASC, fac_name ASC
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table cellpadding=5><tr><th>Fac ID</th><th>ABC ID</th><th>Provider ID</th><th>Fac Name</th><th>Service Level</th><th>Rate</th><th>Start Date</th><th>Fiscal Month End</th><th>Month Start</th><th>Region</th><th>Area</th><th>BD</th><th>PS</th><th>PD</th><th>PD Phone</th><th>Total Beds</th><th>Licensed Beds</th><th>Address</th><th>City</th><th>State</th><th>Zip</th><th>Office Phone</th><th>Logo 1</th><th>Logo 2</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["fac_id"]. "</td><td>" . $row["abc_id"]. "</td><td>" . $row["provider_id"]. "</td><td>" . $row["fac_name"]. "</td><td>" . $row["service_level"]. "</td><td>" . $row["rate"]. "</td><td>" . $row["start_date"]. "</td><td>" . $row["fiscal_month_end"]. "</td><td>" . $row["month_start"]. "</td><td>" . $row["region"]. "</td><td>" . $row["area"]. "</td><td>" . $row["bd"]. "</td><td>" . $row["ps"]. "</td><td>" . $row["pd"]. "</td><td>" . $row["phone_pd"]. "</td><td>" . $row["total_beds"]. "</td><td>" . $row["licensed_beds"]. "</td><td>" . $row["address"]. "</td><td>" . $row["city"]. "</td><td>" . $row["state"]. "</td><td>" . $row["zip"]. "</td><td>" . $row["phone_office"]. "</td><td><a href='/logo/" . $row["logo_1"]. "'>" . $row["logo_1"]. "</a></td><td><a href='/logo/" . $row["logo_2"]. "'>" . $row["logo_2"]. "</a></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

PHP while statement issues

I am having issues with my PHP while statement. It is:
$row = mysqli_fetch_array($result);
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
Yes, I do realize I'm defining $row twice, but I'm not sure how to change that. If I remove $row = mysqli_fetch_array($result) from the while statement, the page will not load and it returns errors. As of now, the page loads but the table is empty, even though I know for a fact there is one record in there that was displaying in the table before I added my military time converter. Any help is appreciated.
Try this
while ($row = mysqli_fetch_array($result)) { //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>";
}
Every time you call mysqli_fetch_array it fetches a row from table, so since you said your table has one record, the one record will be returned in the first mysqli_fetch_array call and eventually the second call to mysqli_fetch_array returns NULL since there is no more records to fetch in the table
Try moving the $time_in_12_hour_format into the while loop.
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
}
This will also solve defining $row twice, which is what causes the issue in the first place. Because you already called mysqli_fetch_array($result) your while loop is starting at row 2.
The $time_in_12_hour_format should be set for each row, right? If yes, then try to move it inside the while loop. Your current code will only start to show row 2 and row1 will be ignored. The modified code should look like:
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
}

Always inserting the last row of data into database (2 variables)

The code just keep retrieving the last row to insert into database. (Images below) I chose and remove USDand CA and the profit and loss and current price in the row don't match when inserted into DB.
It appears as 98.570 and 1.17 when it is supposed to be 1.04930 and -0.72.
The problem is it keeps getting the last row of value in the while loop and inserting into DB. How do i fix this so the variable retrieves from the right row?
While loop for echo
while($row = mysql_fetch_assoc($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['trade_id'] .
"</td><td>" . $row['selection'] .
"</td><td>" . $row['date'] .
"</td><td>" . $row['type'] .
"</td><td>" . $row['size'] .
"</td><td>" . $row['bidprice'] .
"</td><td>" . $row['offerprice'] .
"</td><td>" . $row['stoploss'] .
"</td><td>" . $row['takeprofit'] .
"</td><td>" . $closedb .
"</td><td>" . $profitandloss .
"</td><td><a href ='delete.php?id=".
$row['trade_id']."'>X</a>
</td></tr>";
}
function callvariable($closedb,$profitandloss)
{
$variable=array('$closedb','$profitandloss');
return $variable;
}
SQL
$var=callvariable($closedb,$profitandloss);
$mysqli-> query("UPDATE `trade_history1` SET `dateclose` = CURRENT_TIMESTAMP,
`close` = '{$closedb}',
`profitandloss` = '{$profitandloss}'
WHERE `trade_id`= ".$trade_id);

How to insert button for every row so that the data for that particular row can be duplicated into the database

I have read through several questions on stackoverflow before asking this. I have very little knowledge on php and mysql so much help is needed.
So, what I needed is to create a button for every table row so that when user hits the "copy" button, the data for that row will be copied into the database. How can I do this?
demotable.php (//EDIT//)
<?php
require('connect.php');
$query = "SELECT * FROM trade_history1 ";
$result = mysql_query($query);
echo "<table border = '1px'>"; // start a table tag in the HTML
echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."Copy"."</td></tr>" ; //$row['index'] the index here is a field name
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
<html>
View
</html>
copytrade.php
<?php
require ('connect.php');
$mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database");
$stmt = $mysqli->prepare("INSERT INTO trade_history1 (size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss)
SELECT size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
FROM trade_history1
WHERE id = ?");
$stmt->bind_param("i", $id); //
$successfullyCopied = $stmt->execute();
$stmt->close();
$mysqli->close();
?>
By the way, whenever I click the "copy button" on demotable.php, the link will be this: http://localhost/1103242B/demo/copytrade.php?copy=copy. May I know which part of the code did I missed out or did I do wrongly? Thanks.
You have to put all your table design in the form tag. And then for each row you have to add a copy link with url something like http://localhost/1103242B/demo/copytrade.php?id=id. Here id is the record id from database. You are generating the table above and then in below form you don't have any id reference to copytrade.php page.
That way also you can do that but in each row you can put some checkbox and then when user clicks on checkbox set the id in a hidden field inside the form and then you can able to post that id to copytrade.php.
Both way it will work.
Try to edit your page like below.
<html>
<form method = "GET" action = "copytrade.php">
<?php
require('connect.php');
$query = "SELECT * FROM trade_history1 "; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px'>"; // start a table tag in the HTML
echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."Copy"."</td><td>Copy</td></tr>" ; //$row['index'] the index here is a field name
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td><td>a href='copytrade.php?id=" .$row['id'].'">copy</a></td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
<input type = "submit" name = "copy" value = "copy"/></form>
</html>

Categories