User Manual
Samples

Introduction    Configure grid look    Grid general features

User types    Data from the DB / CSV / XML    Data Saving

Using XML    Grid reference    What's new?    Standard vs PRO

123Guide   

Order PRO Now   

CODETHATGRID USER MANUAL

Grid Data Saving

With CodeThatGrid you can store your data on the web server by using one of three ways:

Store data at the database

Store data at the csv file

Store data and format configuration at the XML file

At the examples below we use MySQL as database platform and PHP as web-script language. But you can choose your own favorite database platform and web-script language. Here we just try to show basic principles for CodeThatGrid data storing.

Save Data from the Grid at the Database

First of all we create a tables - test_data2 - for a data set. Table structure is listed below:

CREATE TABLE test_data2
(
id INT AUTO_INCREMENT NOT NULL,
username VARCHAR(255) NOT NULL,
regdate DATE,
kind INT,
PRIMARY KEY (id)
); 

Of course, when you create your real CodeThatGrid script, tables will contain more fields and data.

Then we create .js file to describe CodeThatGrid. Look any example of CodeThatGrid generation in case you need to remember how to create this script.

Next step - we create .php script. It contains:

CodeThatGrid initialization part

Form for data export and saving

PHP-functions for DB storing

Here is an example code for form. Defenitely you can create another form.

Export button action is processed by CodeThatGrid script.

<FORM name=exportForm method=post>
<BR>
<TEXTAREA style="OVERFLOW: auto; WIDTH: 660px" 
          name=csv rows=15 cols=90 
          onselect="g.clip = this.value;">
</TEXTAREA> 
<BR><BR>
<INPUT onclick="exportForm.csv.value = g.toCSV()" 
       type=button value="Export Data"> | 
<INPUT type=submit 
       value="Save on server DB" name=dbsave> 
</FORM>

There is a php code for saving data at the database:

<?
function my_split($buffer)
{
	$length = strlen($buffer);
	$i = 0;
	while ($length > = 1)
	{
		$pos2 = strpos($buffer, ';');
		if (!$pos2)
		{
			$fields[$i] = $buffer;
			$length = - 1;
		}
		else
		{
			$fields[$i] = substr($buffer, 0, $pos2);
			$buffer = substr($buffer, ($pos2 + 1));
			$length = strlen($buffer);
		}
		$i++;
	}
	return $fields;
}
if ($dbsave)
{
	if (!$csv)
	{
		print_error("You should click 'Export Data' button
			before save data on server.");
	}
	else
	{
		$pos1 = 0;
		$pos2 = 1;
		while ($pos2)
		{
			$pos2 = strpos($csv, "\r\n");
			$buffer = substr($csv, $pos1, $pos2);
			$csv = substr($csv, ($pos2 + 1));
			if (!$buffer) $buffer = $csv;
			$fields = my_split($buffer);
			$name = $fields[1];
			$dt = $fields[2];
			$kind = $fields[3];
			$sql = "insert into test_data2 (username, regdate, kind)
				values('$name', '$dt', $kind)";
			$result = db_query($sql);
			$Error = mysql_error($db);
			if ($Error != "")
			{
				print_error($Error);
			}
		}
		echo "<p>All CodeThatGrid data have been successfully stored
			at the server database!";
	}
}
?>

That's all. When your user clicks 'Save on Server DB' button, all the data will be stored on server.

To learn how to create and/or refresh CodeThatGrid by using data from the server database read Grid Generation from the Database

Example - Data saving at the database

You can see an example and complete code here - Data saving at the database [popup]

Save Data from the Grid at the Csv File

The first several steps are the same as for database saving:

We create .js file to describe CodeThatGrid. Look any example of CodeThatGrid generation in case you need to remember how to create this script.

Create initialization part for CodeThatGrid script.

Create form for data export and saving

Here is an example code for form. Defenitely you can create another form.

Export button action is processed by CodeThatGrid script.

<FORM name=exportForm method=post>
<BR>
<TEXTAREA style="OVERFLOW: auto; WIDTH: 660px" 
          name=csv rows=15 cols=90 
          onselect="g.clip = this.value;">
</TEXTAREA> 
<BR><BR>
<INPUT onclick="exportForm.csv.value = g.toCSVFile()" 
       type=button value="Export Data"> | 
<INPUT type=submit 
       value="Save on server CSV file" name=file_save> 
</FORM>

Then we create a php-code that will be store exported data at the server file.

You should create a constant where you write a path to your .csv file.

PHP code:

<?php
define("filename", "csv.xml");
if ($file_save)
{
	if (!$csv)
	{
		print_error("You should click 'Export Data' button
		before save data on server.");
	}
	else
	{
		// In our example we're opening filename in append mode.
		// The file pointer is at the bottom of the file hence
		// that's where $csv will go when we fwrite() it.
		if (!$handle = fopen(filename, 'w'))
		{
			print "Cannot open file (filename)";
			exit;
		}
		// Write $csv to our opened file.
		if (!fwrite($handle, $csv))
		{
			print "Cannot write to file (filename)";
			exit;
		}
		echo"<p>All CodeThatGrid data have been successfully
		stored at the CSV file on server!<p>";
		fclose($handle);
	}
}
?>

Note: Defenitely you should set a datatype "CSV FILE" (datatype: 3) when creating CodeThatGrid. At this case all your changes at the grid, that you save on server, will be applied after page refreshing.

Please, pay attention: To save data to CSV file you should strip slashes from the data you've sent to server -

$csv = stripslashes($csv);

Example - Data saving at the csv file

You can see an example and complete code here - Data saving at the csv file [popup]

Save Data from the Grid at the XML File

The first several steps are the same as for database saving:

We create .js file to describe CodeThatGrid. Look any example of CodeThatGrid generation in case you need to remember how to create this script.

Create initialization part for CodeThatGrid script.

The form for data export would be slightly differ from the form for database and CSV storing:

<FORM name=exportForm method=post>
<BR>
<TEXTAREA style="OVERFLOW: auto; WIDTH: 660px" 
          name=csv rows=15 cols=90 
          onselect="g.clip = this.value;">
</TEXTAREA> 
<BR><BR>
<INPUT onclick="exportForm.csv.value = g.toXML()" 
       type=button value="Export Data"> | 
<INPUT type=submit 
       value="Save on server DB" name=dbsave> 
</FORM>

Then we create a php-code that will be store exported data at the server file.

You should create a constant where you write a path to your .xml file.

PHP code:

<?php
define("filename", "xml.xml");
if ($file_save)
{
	if (!$csv)
	{
		print_error("You should click 'Export Data' button
		before save data on server.");
	}
	else
	{
		if (!$handle = fopen(filename, 'w'))
		{
			print "Cannot open file (filename)";
			exit;
		}
		// Write $csv to our opened file.
		$csv = stripslashes($csv);
		if (!fwrite($handle, $csv))
		{
			print "Cannot write to file";
			exit;
		}
		echo"<p>All CodeThatGrid data have been successfully
		stored at the XML file on server!<p>";
		fclose($handle);
	}
}
?>

Please, pay attention: To save data to XML file you should strip slashes from the data you've sent to server -

$csv = stripslashes($csv); 

Note: Defenitely you should set a datatype "XML FILE" (datatype: 2) when creating CodeThatGrid. At this case all your changes at the grid, that you save on server, will be applied after page refreshing.

Example - Data saving at the XML file

You can see an example and complete code here - Data saving at the XML file [popup]

Read more about CodeThatGrid >>

© CodeThat.com, 2003-2005
Design by XTLabs, Inc.