I have a CSV file that contains many data.
I use PHP to read and view it in WEB. I need to have a functionality like ADD, EDIT and DELETE. I already done the ADD using the a or append. I will add data in HTML Table and automatically it will add also in CSV file. I didn't use database here.
My problem now is the EDIT. What if i want to edit some data in CSV file using the HTML Table and like the ADD i want to automatically update it to my CSV file. How can i do that?
I already tried the w, w+, r+ but it didn't work. By the way I am using CODEIGNITER here.
Thank you in advance for the help.
I would keep the logic on server and do all the csv rendering eventdriven.
Here you have a good csv lib: https://github.com/goodby/csv
So the process would be something like this:
Client: Load csv preformated (via php) from server and display in html
Client: Do MVVM bindings etc.
Client: If an event is called (like add, update etc.) send
eventrequest to server
Server: Recieve event -> Process (with transactions if needed) -> reformat csv, save it
and send back to client
Client: Retrieve and apply MvvM bindings
Otherwise you need redundant csv logic in client and server whats not a fine approach.
Do you know what I mean or should I do you an example?
The csv library supports complex csv binding and easy appending like:
$csv = new parseCSV();
$csv->parse('yourcsv.csv');
$csv->data[1] = array('updatekey' => 'new Value');
$csv->save();
Client side example:
//retrieve your csv (view rendered or async with ajax
var csv;
var mutableDataArray = $.csv.toArray(csv);
//do manipulation here
//for example
mutableDataArray[0].name = 'A new name to first record';
//serialize back
var newCsv = "";
for(var i in mutableDataArray) {
var data = mutableDataArray[i].join(",");
newCsv += i < mutableDataArray[i].length ? data + "\n" : data;
}
//do something with newCsv
Related
i am seeking help with the following
So im using PHP 7.1 and MSSQL 2017, and IIS
i have some tables that are to large to hard-code or create a text out of them to input in a SQL table , so i created a separate file with a query to display the table.
but when i try to add to "<?php require'table1.php';?>"
as a Text or string in another Table, and is set in the browser it gets comment out by the browser
Is there any way to execute the require with out being comment it out?
for now i have just create a link for the tables to open in a new tab, but i really what then to open inside the original document.
So file 1 is the main query where it displays the information.
File 2 runs another query for the Table, i would like to set the <?php require'table1.php';?> on the main query so it displayed File 2 contents in File 1, rather than opening a new tab.
Main table would be like this
Title - ID - Message
Something - RK1 - Something Important
Something 2 - RK2 - <?php require'table1.php';?>
Something 3 - RK3 - Something Kinda Importan
i apologize if its confusing, let me know.
Any help is appreciated, tyvm.
It's never a good idea to store code in the database because if you're database gets compromised whoever managed to get access to your DB will also have access to running php code on your server and can cause even more damage.
I think what you're trying to achieve could be done within your php files alone.
Why don't you just require table1.php file in your main file?
Edit:
In your main php file you can can create a part that your ajax script can communicate with that allows you to retrieve the necessary table contents like so:
<?php
if(isset($_POST['tableChange'])) {
$tableToDisplay = $_POST['tableChange'];
switch($tableToDisplay) {
case 'A':
$realTableName = 'TableA';
$isValidTable = true;
break;
case 'B':
$realTableName = 'TableB';
$isValidTable = true;
break;
default:
$isValidTable = false;
break;
}
if($isValidTable) {
$tableContents = mysql_fetch_array(mysql_query("SELECT * FROM `".$realTableName."`"));
//Then you can retrieve what ever rows you want from the current table with the array you create with mysql_fetch_array
$row1 = $tableContents['name_of_row1'];
$row2 = $tableContents['name_of_row2'];
echo 'row1: '.$row1.' row2: '.$row2;
}
}
?>
Then you can make an ajax POST request to the main php file -- that passes a piece of data named "tableChange" that holds a string representing the table's identifier. In this case it would have to be set to either "A" or "B" in order for it to be accepted.
Here's an example with jQuery:
$.ajax({
url: 'main.php',
type: 'POST',
data: {tableChange: 'B'},
dataType: 'html',
complete: function(dataReturned) {
//Now print the contents of the dataReturned variable to the screen how ever you want
}
});
Wizzards :)
I really want to use Quill richtext editor but I have no real knowledge of JSON or how is parses the so called "Deltas".
Basically, I need to collect the page from the editor and process it with php before storing it in the mysql database, and, from the database back to the editor if user wishes to update an existing post. It needs to be able to parse text and multiple images...
I don't have 3 months a piece to learn js/json, can anyone help me out with this one? Thank youuu!
So acording to the Quill documentation (https://quilljs.com/docs/api/) you can use var delta = quill.getContents(); to collect the data. Would it be something like this with an onclick() function? I dont even know with js if it has to be within a form? remember I don't know js :)
<script>
onclick(does this); // or can it be updated as the user creates the document?
var delta = quill.getContents();
// what then?
</script>";
The php would require a different variable if each image to be processed, perhaps in an array?
<?php
$images = array();
$images[0] = 'json/delta/quill image?';
$images[1] = 'etc...';
$images[2] = 'etc...';
$images[2] = 'wtf...';
?>
Why on earth they don't have a simple example of how to do this I don't know, its an all too common problem with these things.
I have a simple HTML form comprising of check boxes and text fields.
When I submit this form it submits the data back to the same page where I capture the data and write it to a text file.
What I'd like to try and do is write some of the check box and field data to one text file and the rest to another.
Can this be done ? if it can how do I say which fields are written to which file ?
Thanks
you send your form and after that you handle the data. For example:
$dog = $_POST["dog"];
$cat = $_POST["cat"];
$fileForDogs = fopen('path://to/first/file', 'a+'); //a+ means if not exst, create one and write on the end of file
fwrite($fileForDogs, $dog);
fclose($fileForDogs);
$fileForCats = fopen('path://to/second/file', 'a+'); //a+ means if not exst, create one and write on the end of file
fwrite($fileForCats, $cat);
fclose($fileForCats);
I would highly recommend to look at databases for example mysql to save data from form.
I've got a Minecraft Software written in C# that I want to send a heartbeat to my site. I've got the way to send the beat already written.
if (Server.Uri == null) return;
string uri = "http://GemsCraft.comli.com/Heartbeat.php";
// create a request
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
// turn request string into a byte stream
byte[] postBytes = Encoding.ASCII.GetBytes(string.Format("ServerName={0}&Url={1}&Players={2}&MaxPlayers={3}&Uptime={4}",
Uri.EscapeDataString(ConfigKey.ServerName.GetString()),
Server.Uri,
Server.Players.Length,
ConfigKey.MaxPlayers.GetInt(),
DateTime.UtcNow.Subtract(Server.StartTime).TotalMinutes));
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
request.ContentLength = postBytes.Length;
request.Timeout = 5000;
Stream requestStream = request.GetRequestStream();
// send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Flush();
requestStream.Close();
/* try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Logger.LogToConsole(new StreamReader(response.GetResponseStream()).ReadToEnd());
Logger.LogToConsole(response.StatusCode + "\n");
}
catch (Exception ex)
{
Logger.LogToConsole("" + ex);
}*/
}
Now, I want to be able to retrieve the heartbeat in PHP, upload it to the SQL database, and then display each user's server in a table that will be displayed on the webpage
How do I do this?
portforwardpodcast's answer isn't very well-suited for your purposes, here's a process for you to ponder
Server accesses the following page: heartbeat.php?port=25565&maxplayers=25&players=2&name=Cheese_Pizza_Palace
Your PHP script will then do the following...
Go through each value, making sure they're all the types you want them to be (integers/strings)
Connect to the database
Update the server in the database if it already exists, create it if it doesn't
Return some value so the server knows that it completed successfully.
And to display the servers
Fetch all 'active' servers
Loop through them and display each one.
Things you'll need to figure out:
How to determine uptime
How to determine "active" servers
How to update/create MySQL entries
How to (properly) connect to a database. I would suggest using PDO since you're using PHP. It's a bit difficult to learn, but it's much more secure than writing the queries directly.
How to loop through all the GET variables.
Good hunting!
I would create a simple php page accept a get variable. something like www.site.com/beat.php?lasttime=123456&serverid=1 where the number us the unix timestamp. Then you need to re-work your c# to do a simple get request on a website. Finally your php should insert into a mysql table with a column for id, timestamp, server_id etc.
First you need to pull the data from the request. The $_REQUEST variable in php is nice because it works for both GET and POST:
http://php.net/manual/en/reserved.variables.request.php
Start out by var_dump or echo the fields you want. Once you can get the needed data into variables you are done with the first part. For the next part you need to create a database and table in MySQL. The best tool for this is phpmyadmin. If you have a host like godaddy (or some others) you can get at this from the control panel. If not you may need to install upload the phpmyadmin files yourself. It's a pretty simple tool to use:
http://www.youtube.com/watch?v=xxQSFHADUIY
Once your database has the correct columns, you need to insert the data from your php file. This page should help:
http://www.w3schools.com/php/php_mysql_insert.asp
I'm using CKeditor but I'm not working out how to create the php file that stores editing textareas in MySql DB.
I'm quite newbie in php/mysql..
javascript code of CKeditor calls is:
$(’#my_div’).ckeip({
e_url: ’test.php’,
data: {
example_var : ’example_value’,
example_var2 : ’example_value2’
}
)};
What I have to write in test.php to making it store data of ckeditor in MySql?
Should I create a new table in database first ?
thanks a lot
I think the best way to handle this is create a new MYSQL table with two columns. One as an id for the textarea and then the other column to store the text. For a basic tutorial about MYSQL and PHP check out W3School http://www.w3schools.com/PHP/php_mysql_intro.asp
I am a newbie to CKEditor, too. But it seems like it would be quite easy once you get the data from the editor into a javascript variable, like:
var editorData = $('my_div').ckeditorGet().getData();
You can then put editorData into a hidden input field and then submit it to the PHP file that you're going to create. On that PHP file you would take it with something like:
$theData = $_GET["name_of_the_hidden_input_field"];
//here comes the PHP code for inserting $theData into mysql