I want to include a function upon a click of a button. The code basically retrieves rows of data from database and each row has an X button. I hope my explanation is clear!
( "</td><td><a href ='delete.php?id=".
$row['trade_id']."'>X</a>
</td></tr>"; )
. How do I do an action( include function) upon click of the ahref X ? How do i do this? example:
if (buttonclicked)
{
require 'function.php';
}
Php
while($row = mysql_fetch_assoc($result)){
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>" . $profit .
"</td><td><a href ='delete.php?id=".
$row['trade_id']."'>X</a>
</td></tr>";
echo "</table><br>";
delete.php
if(isset($_GET["id"])){
require 'function.php';
}
if(isset($_GET['id'])) { //Equals your "button clicked."
$id = intval($_GET['id']); //Thats the ID you provided through "?id=1234".
require 'function.php';
}
in you delete.php file :
if(isset($_GET['id']) && $_GET['id'] != null && $_GET['id'] != ""){
require_once 'function.php';
$myFunction = new myFunctionClass();
$myFunction->DoMyDeleteLogic($_GET['id']); // protect data from get before
//or static class
MyFunctionClass::DoMyDeleteLogic($_GET['id']);
}
else{
echo('datas missing...'); //put a throw exception
}
Related
Without wasting time on a long intro, I have a major issue: I am currently stuck at one particular place in my project where a user can search for a job by entering keywords. After doing so, user will see matching jobs posted with an option to apply by clicking a button that is next to that job.
Here is the problem: how do I make each button correspond to THAT particular job and post the applicants details to an appropriate row in the database, say, where Job_ID = xyz?
This was the approach that I tried - assigning a unique Job_ID to a button based on the job that the button is linked to (making that the "name" of the button), but I have no idea how to pass that on and actually use it to achieve a result.
Help would be seriously appreciated. I have been stuck on this for a while now.
Here is my PHP code (embedded inside HTML) that outputs the search results in a table:
<?php
if (!empty($_POST['search-submit'])) {
if (!preg_match("[/^$|\s+/]", $keywords)) {
echo "<tr><td>" . "<b>Job Title</b>" . "</td><td>" . "<b>Job Description</b>" . "</td><td>" . "<b>Job Date</b>" . "</td></tr>";
foreach ($search_query as $row) {
echo "<tr><td>" . $row['JobTitle'] . "</td><td>" . $row['JobDescription'] . "</td><td>" . $row['JobDate'] . "</td><td>" . '<input class = "btn btn-primary btn-sm" type = "submit" value = "Apply" name = ' . $row['Job_ID'] .'>' ."</td></tr>";
}
} elseif (preg_match("[/^$|\s+/]", $keywords)) {
echo "<p>" . "No matches found." . "</p>";
}
}
?>
The solution was to add a form for each loop of the result produced from the search query and add a hidden field with the Job_ID attribute as its value.
Based on which button is clicked, the value assigned to the hidden field would be posted and later on accessible by default queries such as $_POST['name_of_field'].
<?php
if (!empty($_POST['search-submit'])) {
if (!preg_match("[/^$|\s+/]", $keywords)) {
echo "<tr><td>" . "<b>Job Title</b>" . "</td><td>" . "<b>Job Description</b>" . "</td><td>" . "<b>Job Date</b>" . "</td></tr>";
foreach ($search_query as $row) {
echo '<form method = "post" action = "">';
echo '<input type = "hidden" name = "applyjob_id" value = "' . $row['Job_ID'] . '"> ';
echo "<tr><td>" . $row['JobTitle'] . "</td><td>" . $row['JobDescription'] . "</td><td>" . $row['JobDate'] . "</td><td>" .
'<input class = "btn btn-primary btn-sm" name = "applyjob" type = "submit" value = "Apply" >' ."</td></tr>";
echo '</form>';
}
} elseif (preg_match("[/^$|\s+/]", $keywords)) {
echo "<p>" . "No matches found." . "</p>";
}
}
if (!empty($_POST['applyjob'])) {
$applyjob_id = isset($_POST['applyjob_id']) ? $_POST['applyjob_id'] : '';
$stick_info = "UPDATE `userjob` SET `Seeker_ID` = '$seekerjobid' WHERE `userjob`.`Job_ID` = '$applyjob_id'";
mysqli_query($conn, $stick_info);
}
?>
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.
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);
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>
I am trying to run a mysql query within some php and have the results echoed as HTML. I got it working but now I would like to insert an id into the links so I can use a page to GET the id. Does anyone know how to do this. What I have tried (and is not working) is below. And at the bottom of the post is what was working originally, but without an id on the link...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
$uniqueid = $_GET["$row['User_ID']"];
echo $uniqueid;
This is from the first page that worked...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
The problem that you are having is that you are trying to access an array element from within double quotes. You could make this work by wrapping it in curly braces {$row['User_ID']}.
To make your code more readable, and to avoid this problem, just concatenate or use a list of values for echo. I also recommend the usage of htmlspecialchars() to ensure you are creating valid HTML.
echo '<tr><td>',
'<a href="2012week1.php?id=',
htmlspecialchars($row['User_ID']),
'">',
htmlspecialchars($row[User_ID]),
'</a>',
'</td><td>'
//etc.