i have php code it's for reading excel file (.xlsx) and i want to make plugin for Wordpress ( just a simple plugin ). tested on localhost Wordpress it's work perfectly, but when i uploaded to my site it's not work. when i click submit button just appear blank page.
<form method="post" action="">
Number : <input type="text" name"number" /> </br>
<input type"submit">
</form>
function find(){
if (isset($_POST['number']) {
$number = $_POST["number"];
require_once ( plugin_dir_path(__FILE__). 'includes\classes\PHPExcel.php');
$tmpfname = ( plugin_dir_path(__FILE__). 'number.xlsx');
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$excel_arr = $worksheet->toArray(null,true,true,true);
for ($row=1;$row <=$lastRow;$row++){
if ($excel_arr[$row]["A"] == $number ) {
echo $excel_arr[$row]["A"];
break;
}
}
}
}
add_shortcode('show_number', 'find');
Insert HTML form code inside "find()" function. So form show with shortcode "show_number" and and get result.
function find(){
if (isset($_POST['number']) {
$number = $_POST["number"];
require_once ( plugin_dir_path(__FILE__). 'includes\classes\PHPExcel.php');
$tmpfname = ( plugin_dir_path(__FILE__). 'number.xlsx');
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$excel_arr = $worksheet->toArray(null,true,true,true);
for ($row=1;$row <=$lastRow;$row++){
if ($excel_arr[$row]["A"] == $number ) {
echo $excel_arr[$row]["A"];
break;
}
}
}
?>
<form method="post" action="">
Number : <input type="text" name"number" /> </br>
<input type"submit">
</form>
<?php
}
add_shortcode('show_number', 'find');
?>
Related
Here is my code. It is a simple html form with two inputs. All data is saved into xml file using DOMDocument a then all data from XML file is inserted into the table below the form. I added two buttons edit and delete (x). Now I can edit and delete any user/player from the table. And here is my problem. I need to remove query string from URL after deleting or editing. I want to add a new user/player into the table after deleting. When I delete manually query string from URL everything works fine again. But I want to delete query string automatically after deleting some user. Sorry for my English hoping you understand me. Thanks in advance!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$xml = new DOMDocument("1.0", "utf-8");
if (is_file('database.xml')) {
$xml->load("database.xml");
$db = $xml->getElementsByTagName('db')->item(0);
} else {
$db = $xml->createElement("db");
$xml->appendChild($db);
$db = $xml->getElementsByTagName('db')->item(0);
}
$newPlayer = $xml->createElement("player");
foreach ($_POST as $key => $value) {
$playerStuff = $xml->createElement($key, $value);
$newPlayer->appendChild($playerStuff);
}
$db->appendChild($newPlayer);
$xml->save("database.xml");
}
$name = $number = "";
if (isset($_GET['action']) && $_GET['action'] == 'edit') {
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);;
$name = $player->getElementsByTagName('name')->item(0)->nodeValue;
$number = $player->getElementsByTagName('number')->item(0)->nodeValue;
}
if (isset($_GET['action'])) {
switch ($_GET['action']){
case 'edit':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$nameEl = $player->getElementsByTagName('name')->item(0);
$numberEl = $player->getElementsByTagName('number')->item(0);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$newName = $xml->createElement("name", $_POST["name"]);
$nameEl->parentNode->replaceChild($newName, $nameEl);
$newNumber = $xml->createElement("number", $_POST["number"]);
$numberEl->parentNode->replaceChild($newNumber, $numberEl);
$db = $xml->getElementsByTagName('db')->item(0);
$lastElement = $db->lastChild;
$lastElement->parentNode->removeChild($lastElement);
}
$xml->save("database.xml");
break;
case 'delete':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
//$xml = $dokument->getElementsByTagName('xml')->item(0);
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$player->parentNode->removeChild($player);
$xml->save("database.xml");
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>PLAYERS</title>
<link rel="stylesheet" href="stylesss.css">
</head>
<body>
<form action="" method="POST">
<input type="text" name="name" value="<?php echo $name ?>">
<input type="text" name="number" value="<?php echo $number ?>">
<input type="submit" value="INSERT">
</form>
<?php
$xml = new DOMDocument("1.0", "utf-8");
if (is_file('database.xml')) {
$xml->load('database.xml');
$players = $xml->getElementsByTagName("player");
echo "<table>";
echo "<tr><th>" . "Player Name" . "</th><th>" . "Number". "</th></tr>";
foreach($players as $key => $player){
echo "<tr>";
foreach($player->childNodes as $data) {
echo "<td>". $data->nodeValue ."</td>";
}
echo "<td>
<a href='?id={$key}&action=edit' class='buttons edit'>EDIT</a>
<a href='?id={$key}&action=delete' class='buttons delete'>✖</a>
</td>";
echo "</tr>";
}
}
echo "</table>";
?>
</body>
</html>
I would say to redirect header("Location: your-page.php"); The page you redirect to can be the same page but without the query string. It means that if someone refreshes you will not repeat the same action. So, something like this. You have to use the header function before anything is written to the page, it will not work if it is used after HTML or after something is print or echo to the page.
if (isset($_GET['action'])) {
switch ($_GET['action']){
case 'edit':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$nameEl = $player->getElementsByTagName('name')->item(0);
$numberEl = $player->getElementsByTagName('number')->item(0);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$newName = $xml->createElement("name", $_POST["name"]);
$nameEl->parentNode->replaceChild($newName, $nameEl);
$newNumber = $xml->createElement("number", $_POST["number"]);
$numberEl->parentNode->replaceChild($newNumber, $numberEl);
$db = $xml->getElementsByTagName('db')->item(0);
$lastElement = $db->lastChild;
$lastElement->parentNode->removeChild($lastElement);
}
$xml->save("database.xml");
break;
case 'delete':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
//$xml = $dokument->getElementsByTagName('xml')->item(0);
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$player->parentNode->removeChild($player);
$xml->save("database.xml");
break;
}
header("Location: your-page.php");
}
can anyone tell me why this code doesn't give me any records and display my "else" error message instead ? table which I'm trying to get data from( attached) uses two foreign keys ( emp number ,and project code) from two other tabels
( Please note I'm new to PHP )
$emp_no="";
$project_code="";
$p_hours="";
require_once 'connect.php';
function getposts()
{
$posts= array();
if (isset($_POST['EMPNo']))
{
$posts[0] = $_POST['EMPNo'];
}
if (isset($_POST['ProjectCode']))
{
$posts[1] = $_POST['ProjectCode'];
}
if (isset($_POST['Hours']))
{
$posts[2] = $_POST['Hours'];
}
return $posts;
}
if(isset($_POST['search']))
{
#$data = getposts();
#$searchquery = "SELECT * FROM `enrolment` WHERE `EMPNo`='$data[0]' AND
`ProjectCode`='$data[1]'";
#$search_Result =mysqli_query($connect, $searchquery);
if($search_Result)
{
if(mysqli_num_rows($search_Result))
{
while($raw = mysqli_fetch_array($search_Result))
{
$emp_no = $raw ['EMPNo'] ;
$project_code = $raw ['ProjectCode'] ;
$p_hours = $raw ['Hours'] ;
}
}else {
echo 'Unable to find the record please check input data!';
}
}else {
echo ' Result Error ';
}
}
//html part
<Form action="updateenrolment.php" method="post" style="color:blue;margin-
left:500px;">
<input type="text" name ="empno" placeholder="Employee No" value="<?php
echo $emp_no;?>"><br><br>
<input type="text" name ="pcode" placeholder="Project Code" value="<?php
echo $project_code;?>"><br><br>
<input type="number" name ="hours" placeholder="Hours" value="<?php echo
$p_hours;?>"><br><br>
<div>
<input type="submit" name ="search" value="Find" >
mysqli_fetch_array print the array result but you are fetching result using string element. you need to change this.
while($raw = mysqli_fetch_array($search_Result))
{
$emp_no = $raw [1] ;
$project_code = $raw [2] ;
$p_hours = $raw [3] ;
}
To replace it.
while($raw = mysqli_fetch_array($search_Result))
{
$emp_no = $raw ['EMPNo'] ;
$project_code = $raw ['ProjectCode'] ;
$p_hours = $raw ['Hours'] ;
}
I need to be able to update my form data once the submit button is pressed on the same page.
I have a csv file. And at the moment, I have worked out how to edit data inside it, and save it in the csv file (through a form).
-(I have attached the main code below, but even any suggestions would be helpful)
The form and php execution is on the same page, but once the user edits the values and presses submit, the data goes back to the original data. So it updates in the csv file, but not in the form.
This is the form:
for ($i=0; $i<200; $i++) {
if(empty($SearchResults[$i][0]) == false){ //If its full
?>
<form method="post" action="">
Full Name: <input name = "Name2" type="text" value="<?php echo $SearchResults[$i][0] ?>"/>
Weight (kg): <input name="Weight2" type="number" value="<?php echo $SearchResults[$i][1] ?>"/>
Weight of belongings (kg): <input name="WeightOfBelongings2" type="number"value="<?php echo $SearchResults[$i][2] ?>"/>
<input name="submit" type="submit" />
</form>
<?php
$i++;
}else if (empty($SearchResults[$i][0]) == true){ //If it is empty
$i =201;
}
}
This is what happens when the submit button is pressed:
if (isset($_POST['submit'])) {
$FullName = $_POST['Name2'];
$Weight = $_POST['Weight2'];
$WeightOfBelongings = $_POST['WeightOfBelongings2'];
//Creates a new felon from the class in felen.php and assigns it to this variable $Felon
$Felen = new felen;
//This refers to a function in the class Felen. From this information it calculates the costs, ammounts etc. And saves it to a variable that can be called.
$Felen -> CreateFelen($FullName, $Weight, $WeightOfBelongings);
//This is a for loop that checks when there is an avaliable line to put the data into.
if ($FullName != "") {
$i = 0;
for ($i=0; $i<200; $i++) {
if(empty($csv[$i][0]) == false){ //If its full
if($csv[$i][0] == $_POST['Name2']){
//its only printing it if it is in the first row?
$csv[$i][0] = $FullName;
$csv[$i][1] = $Weight;
$csv[$i][2] = $WeightOfBelongings;
$csv[$i][3] = $Felen->FelenTotalWeight;
$csv[$i][4] = $Felen->UnassembliumRequired;
$csv[$i][5] = $Felen->ReassembliumRequired;
$csv[$i][6] = $Felen->CostOfUnassemblium;
$csv[$i][7] = $Felen->CostOfReassemblium;
$csv[$i][8] = $Felen->TotalCostOfJourney;
$i = 201;
}
}
}
//Saves the previous data and new changes to the csv file
//This opens to file as a write file
$fp = fopen('data.csv', 'w');
//This takes the array that has had data ddded to it and adds it to the file
foreach ($csv as $fields) {
fputcsv($fp, $fields);
}
//This closes the file
fclose($fp);
}
}
Thank you very much!
hello to all at stackoverflow. (my first post) : )
i wrote this script
$file = glob("*.php"); // list all .php files
$findfile = array_search("index.php", $file); // search array for index.php
unset($file[$findfile]); // remove index.php from array
sort($file, SORT_NUMERIC); // sort array
foreach($file as $file){ include_once($file);} // include files in page
the files are 1.php,2.php,3.php etc
everytime i run it the files get included at the top of the page.
i need the files in the middle,
what am i doing wrong.
Thats the whole page as it looks for now
<?php
if(isset($_POST['ta'])){
$ta = $_POST['ta'];
if($ta != "" && strlen($ta) > 1 ){
$ta = preg_replace('#[^a-z0-9 !?.,]#i', '', $ta);
$usertextinput = '<p class="important">'.$ta.'</p>';
$pho = count(glob('*.php'));
$username_file = ($pho + 1) . ".php";
$createuser = fopen($username_file, 'w');
fwrite($createuser, $usertextinput);
header("location: index.php#bottom");}}
$userpage = '<p class="important"><span>Posts\'s:</span><br />
[username] Has 1 post's To Date.</p>';?>
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){ include_once($file);}
$userpage .= '<form name="text" method="post" action="">
<textarea name="ta" placeholder=" Enter Your Comment\'s Here ">
</textarea><br />
<a name="bottom"></a>
<p class="sub"><input type="submit" value="Post To Page" /></p>';
?>
im building a post wall (no database)
Give this a go. It will have the form's input on top,
then the Posts's: [username] Has 1 post's To Date. followed by the included files.
It's basically a placement issue.
<?php
if(isset($_POST['ta'])){
$ta = $_POST['ta'];
if($ta != "" && strlen($ta) > 1 ){
$ta = preg_replace('#[^a-z0-9 !?.,]#i', '', $ta);
$usertextinput = '<p class="important">'.$ta.'</p>';
$pho = count(glob('*.php'));
$username_file = ($pho + 1) . ".php";
$createuser = fopen($username_file, 'w');
fwrite($createuser, $usertextinput);
header("location: index.php#bottom");
}
}
$userpage = '<form name="text" method="post" action="">
<textarea name="ta" placeholder=" Enter Your Comment\'s Here ">
</textarea><br />
<a name="bottom"></a>
<p class="sub"><input type="submit" value="Post To Page" /></p>';
$userpage .= '<p class="important"><span>Posts\'s:</span><br />[username] Has 1 post\'s To Date.</p>';
echo $userpage;
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){
include_once($file);
}
?>
The reason why it isn't working, is because you are terminating the php code here:
[username] Has 1 post's To Date.</p>';?>
Remove the ?> and escape the single quote near post's; so the code block will look like this:
$userpage = '<p class="important"><span>Posts\'s:</span><br />
[username] Has 1 post\'s To Date.</p>';
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){ include_once($file);}
I have a chart and a text file that is taking in some data. I would like to organize the data I have by putting the user with the highest score on the top of the table and coloring everything in their row blue. Any idea how I could do this?
file one:
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>High Score</title>
</head>
<body>
form action="data.php" method="POST">
<table border="1">
<tr><td>Player Name</td><td><input type="text" name="name"</td></tr>
<tr><td>Score</td><td><input type="text" name="score"</td></tr>
<tr><td colspan="2" align="center"><input type="submit"></td></tr>
</table>
</form>
</body>
</html>
Data.php:
<?php
$name = $_POST['name'];
$score = $_POST['score'];
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];
# $fp = fopen("$DOC_ROOT/../phpdata/highscore.txt","ab");
if(!$fp) {
echo 'Error: Cannot open file.';
exit;
}
fwrite($fp, $name."|".$score."\n");
?>
<?php
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];
$players = file("$DOC_ROOT/../phpdata/highscore.txt");
echo "<table border='2'>";
echo "<tr> <td>Name</td> <td>Score</td> </tr>";
for($i = 0; $i < sizeof($players); $i++) {
list($name,$score) = explode('|', $players[$i]);
echo '<tr><td>'.$name.'</td><td>'.$score.'</td></tr>';
}
echo '</table>';
?>
Format all players/scores into arrays like array('name' => 'Bob', 'score' => 42):
foreach ($players as &$player) {
list($name, $score) = explode('|', $player);
$player = compact('name', 'score');
}
unset($player);
Sort the array by score (PHP 5.3 syntax):
usort($players, function ($a, $b) { return $b['score'] - $a['score']; });
Output the results, setting a class on the first row:
$first = true;
foreach ($players as $player) {
$class = $first ? ' class="highlight"' : null;
$first = false;
printf('<tr%s><td>%s</td><td>%s</td></tr>', $class, htmlspecialchars($player['name']), htmlspecialchars($player['score']));
}
Now highlight that class using CSS (or do it directly in the HTML, or whatever else you want to do).