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);
Related
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.
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>";
}
}
}
I have a wordpress site with a scoreboard that gets results from a view in the wp database and I ve set up the board on a custom page template.My trouble is to link the players in the scoreboard to a new page with players stats etc. I want to be able to click the player name in the scoreboard and then get to a page with stats for that player.
Here is the relevant code that displays the scoreboard:
<?php //Table with SQL Query
$connection = mysql_connect('localhost', 'root', '');
mysql_set_charset('utf8',$connection);
mysql_select_db('worldsbestblog');
$query = "SELECT * FROM Sammenlagt LIMIT 0,20";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr><td>" . $row['Dato'] . "</td>
<td>
" . $row['Navn'] . "
</td>
<td>" . $row['hull1'] . "</td><td>" . $row['hull2'] .
"</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] .
"</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] .
"</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] .
"</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] .
"</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] .
"</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] .
"</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] .
"</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] .
"</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
"</td><td>" . $row['Poeng'] . "</td></tr>"; //$row['index'] the index here is a column name
}
?>
I've seen countless similar questions where SESSIONS, $_POST and $_GET has been used though I havent been able to either understand or see how/if thats what's missing in order to make it work.
As it is now i get a URL showing the userid of player, but when clicked, wordpress cant find that page.
[edit]
OK I have done some editing and found an answer to my question. Unsure how to go about here on stackoverflow (direct me if I should do this elsewhere), but I want to show you what did the job so that the next guy will find this post with an explanation.
So here is the current code and thanks to #Cal Evans and #Hamed Momeni for helpful guidance:
Scoreboard goes:
$query = "SELECT * FROM Sammenlagt";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['Dato'] . "</td>
<td>
" . $row['Navn'] . "
</td>
<td>" . $row['hull1'] . "</td><td>" . $row['hull2'] .
"</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] .
"</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] .
"</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] .
"</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] .
"</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] .
"</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] .
"</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] .
"</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] .
"</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
"</td><td>" . $row['Poeng'] . "</td></tr>"; //$row['index'] the index here is a column name
}
?>
.../wordpress/teststats is a new page with a custom page template with the following code:
<?php
$spid = (int) filter_input(INPUT_GET,'id', FILTER_SANITIZE_NUMBER_INT);
$spillerquery = mysql_query("SELECT * FROM Sammenlagt WHERE spillerid='$spid'") or die ("Somthing went wrong!!");
if (mysql_num_rows($spillerquery) < 1){
die ("Couldnt find that player");
}
$navnequery = mysql_query("SELECT Navn FROM Sammenlagt WHERE spillerid='$spid'") or die ("Player does not exist");
$spillernavn = mysql_result($navnequery,MYSQL_BOTH);
?>
<h2>Her er <?php echo "$spillernavn"; ?> sine resultater</h2>
<?php
while($row = mysql_fetch_array($spillerquery, MYSQL_ASSOC)){
echo "<tr><td>" . $row['Dato'] .
"</td><td>" . $row['hull1'] . "</td><td>" . $row['hull2'] .
"</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] .
"</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] .
"</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] .
"</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] .
"</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] .
"</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] .
"</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] .
"</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] .
"</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
"</td><td>" . $row['Poeng'] . "</td></tr>";
}
?>
Try this.
<?php
//Table with SQL Query
$player_id = (int) filter_input(INPUT_GET,'id', FILTER_SANITIZE_NUMBER_INT);
$connection = mysql_connect('localhost', 'root', '');
mysql_set_charset('utf8',$connection);
mysql_select_db('worldsbestblog');
$query = "SELECT * FROM Sammenlagt where player_id = {$player_id} LIMIT 0,20";
$result = mysql_query($query);
Although, I do agree with #Hamed that you should be using $wpdb instead of creating your own connection. This is assuming that your table is in the same database as your WordPress tables.
If you're actually using Wordpress I suggest you use the provided $wpdb object for your Database needs.
The other suggestion is when using Wordpress you need to use template files for creating different pages. You can create different page templates for various pages inside Wordpress. That way you can use the Wordpress rerouting engine for its powerful and easy features.
For example imagine you want to create a page for player stats. First create a file with the name tpl-player_stats.php inside your theme dir with the following header:
<?php
/*
Template Name: Player Stats Page
*/
And then inside Wordpress dashboar create a page and assign the above template to it. After that you can go to your page by going to http://example.com/your_page_slug/?id=player_id.
Generally this is how it's done with Wordpress. So I suggest you dig into that.
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
}
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>