21 Aug 2009 @ 2:56 AM 








sometime we need export data to excel file. i have simple helper for write data to xls file . this script writing By Cleiton Wasen (wasenbr).

1. create file xls.php to  app/views/helpers/xls.php


2. create view templates file app/views/…/xls.thtml

setHeader('text_'.date('Y_m_d').'.xls');
// XLS Data Cell
$xls->BOF();
$xls->writeLabel(1,0,"Student Register");
$xls->writeLabel(2,0,"COURSENO : ");
$xls->writeLabel(2,1,"123");
$xls->writeLabel(3,0,"TITLE : ");
$xls->writeLabel(3,0,"BlaBlaBla");
$xls->writeLabel(4,0,"SETION : ");
$xls->writeLabel(6,0,"NO");
$xls->writeLabel(6,1,"ID");
$xls->writeLabel(6,2,"Gender");
$xls->writeLabel(6,3,"Name");
$xls->writeLabel(6,4,"Lastname");
$xls->writeLabel(7,0,"NO");
$xls->writeLabel(7,1,"ID");
$xls->writeLabel(7,2,"Gender");
$xls->writeLabel(7,3,"Name");
$xls->writeLabel(7,4,"Lastname");
$xls->EOF();

exit();
?>


Tags Tags: , , ,
Categories: CakePHP
Posted By: asbin
Last Edit: 10 Jun 2010 @ 12 17 AM

EmailPermalinkComments (2)
 20 Aug 2009 @ 11:52 PM 








this script read the numeric and writing to the sentence. the data numeric can be covert using function. if you will use this function,  you must call function with parameter included. output this script for indonesian format sentence…

terbilang.php


using function :


          
Tags Tags: , , , ,
Categories: php
Posted By: asbin
Last Edit: 11 Jun 2010 @ 12 15 AM

EmailPermalinkComments (0)
 19 Aug 2009 @ 7:54 AM 

ajax tree,… we need that if we have parent and child field data on database. and we showing this data to tree form. ok we have simple script to do that.

follow this instruction :

1. create javascript code and place to javascript directory

ajax.js

		//

class.database.php

rows = 0;
if($_SERVER['HTTP_HOST'] == 'localhost' || ereg('^192\.168\.0\.[0-9]+$', $_SERVER['HTTP_HOST'])) {
/* Local connetion vars */
$this->host = "localhost";
$this->password = "root";
$this->user = "root";
$this->database = "database";
} else {
/* Internet connetion vars */
$this->host = "server";
$this->password = "password";
$this->user = "user";
$this->database = "database";
}
} // Method : end

function OpenLink()  { // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password); // or die (print "Class Database: Error while connecting to DB (link)");
if(mysql_error()) {
$this->connected = false;
$this->error = mysql_error();
} else {
}
return $this->connected;
} // Method : end

function SelectDB() { // Method : begin
if(!@mysql_select_db($this->database,$this->link)) {   //; or die (print "Class Database: Error while selecting DB");
$this->connected = false;
$this->error = mysql_error();
} else {
$this->connected = true;
}
} // Method : end

function CloseDB() { // Method : begin
mysql_close();
} // Method : end

function Query($query) { // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query");
$this->error = mysql_error();
// $rows=mysql_affected_rows();

if(ereg("SELECT",$query)) {
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end

// Return true if there was an error
function is_error() {
return (!empty($this->error)) ? true : false;
}

function fetchRow() { // Method : begin
return mysql_fetch_array($this->result);
$this->error = mysql_error();
} // Method : end

function getResult($row,$field) { // Method : begin
return mysql_result($this->result,$row,$field);
} // Method : end

function getError() { // Method : begin
return mysql_error();
} // Method : end

function getNumRows() { // Method : begin
return $this->rows;
} // Method : end

} // Class : end

?>

class.tree.php

<?phpid = $id;
$this->database = new Database();
if(!isset($this->id))
$this->id = 0;
}

// ********************** METODO LIST

function countSub($id) {
$sql =  "SELECT COUNT($this->fieldId) AS subOptions FROM $this->table";
$sql.= " WHERE $this->fieldFrom = $id;";
//echo $sql;
$this->database->rows = $this->database->getResult($this->database->query($sql),"subOptions");
}

function listSub($id) {
$sql =  "SELECT $this->fieldId,$this->fieldView FROM $this->table";
$sql.= " WHERE $this->fieldFrom = $id;";
//echo $sql;
return $this->database->query($sql);
}

function display() {
$this->listSub($this->id);
if($this->database->getNumRows() > 0) {
while($row=$this->database->fetchRow()) {

$tree_sub=new Tree($row[$this->fieldId]);
$tree_sub->countSub($row[$this->fieldId]);

echo "
"; echo (($tree_sub->database->getNumRows() > 0)?"fieldId].")\">[+]":"[-]"); echo " ".$row[$this->fieldView]."\n"; echo "
fieldId]."\" class=\"fld_ln\" style=\"display:none;\">
"; echo "

"; } } } } // clase : fin ?>

3. create tree.php and tree_sub.php code

tree.php













display();
?>


tree_sub.php


display();
?>
CREATE TABLE `carpetas` (
`idCarpeta` int(11) NOT NULL auto_increment,
`chrNombre` char(20) default NULL,
`intDeCarpeta` int(11) default NULL,
`intEstado` tinyint(4) NOT NULL default '0',
PRIMARY KEY  (`idCarpeta`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `carpetas` VALUES (1, '/raiz', 0, 1);
INSERT INTO `carpetas` VALUES (2, 'primera', 1, 1);
INSERT INTO `carpetas` VALUES (3, 'tercera', 2, 1);
INSERT INTO `carpetas` VALUES (4, 'cuarta', 1, 1);
INSERT INTO `carpetas` VALUES (5, 'quinta', 2, 1);
INSERT INTO `carpetas` VALUES (6, 'sexta', 3, 1);
INSERT INTO `carpetas` VALUES (7, 'septima', 5, 1);
INSERT INTO `carpetas` VALUES (8, 'octava', 6, 1);
INSERT INTO `carpetas` VALUES (9, 'novena', 2, 1);
INSERT INTO `carpetas` VALUES (10, 'decima', 7, 1);
INSERT INTO `carpetas` VALUES (11, 'onceava', 5, 1);
INSERT INTO `carpetas` VALUES (12, 'doceava', 7, 1);
INSERT INTO `carpetas` VALUES (13, 'treceava', 12, 1);
INSERT INTO `carpetas` VALUES (14, 'catorceava', 7, 1);

6. to complete download code you can go to this link

Tags Tags: , , ,
Categories: ajax
Posted By: asbin
Last Edit: 11 Jun 2010 @ 03 56 AM

EmailPermalinkComments (0)
 04 Aug 2009 @ 4:30 AM 

we have discused about dumping structure table into sql file before. now we can discus about dumping data table script using php.

this is a function to export table to data sql  file :


PHP Example :

<?


function _mysqldump_table_data($table)
{

$sql=”select * from `$table`;”;
$result=mysql_query($sql);
if( $result)
{
$num_rows= mysql_num_rows($result);
$num_fields= mysql_num_fields($result);

if( $num_rows > 0)
{
echo “/*d*/\n”;

$field_type=array();
$i=0;
while( $i < $num_fields)
{
$meta= mysql_fetch_field($result, $i);
array_push($field_type, $meta->type);
$i++;
}

//print_r( $field_type);
echo “insert into `$table` values\n”;
$index=0;
while( $row= mysql_fetch_row($result))
{
$row[0]=”””;
echo “(“;
for( $i=0; $i < $num_fields; $i++)
{
if( is_null( $row[$i]))
echo “null”;
else
{
switch( $field_type[$i])
{
case ‘int’:
echo $row[$i];
break;
case ‘string’:
case ‘blob’ :
default:
echo “‘”.mysql_real_escape_string($row[$i]).”‘”;

}
}
if( $i < $num_fields-1)
echo “,”;
}
echo “)”;

if( $index < $num_rows-1)
echo “,”;
else
echo “;”;
echo “\n”;

$index++;
}
}
}
mysql_free_result($result);

echo “\n”;
}


?>

Usage Example:

PHP Example :

<?
$con= mysql_connect ( “127.0.0.1″, “root”, “”);


header(‘Content-type: text/plain’);
header(‘Content-Disposition: attachment; filename=”dumpdata.sql”‘);
//echo “/*mysqldump.php version $mysqldump_version */\n”;
_mysqldump_table_data(‘table’);

die();

?>

for usage, you must connect to the database, then calling _mysqldump_table_data(‘table’); and enter the  table name into parameter function.

header(‘Content-type: text/plain’);
header(‘Content-Disposition: attachment; filename=”dumpdata.sql”‘);

header script used to create file sql and saved on your directory.

Tags Tags: , ,
Categories: php
Posted By: asbin
Last Edit: 04 Aug 2009 @ 04 30 AM

EmailPermalinkComments (1)
 04 Aug 2009 @ 4:12 AM 

Sometimes you might need a function to create sql table structure to entire sql file . This function will help you do that.

this is a function to export table to structure sql  file :

PHP Example :
<?

function _mysqldump_table_structure($table)
{
echo “/* Table structure for table `$table` */\n”;
if( isset($_REQUEST['sql_drop_table']))
{
echo “DROP TABLE IF EXISTS `$table`;\n\n”;
}
//if( isset($_REQUEST['sql_create_table']))    {

$sql=”show create table `$table`; “;
$result=mysql_query($sql);
if( $result){
if($row= mysql_fetch_assoc($result))
{
echo $row['Create Table'].”;\n\n”;
}
}
mysql_free_result($result);
//}

}

?>

Usage Example:

PHP Example :

<?

$con= mysql_connect ( “127.0.0.1″, “root”, “”);


header(‘Content-type: text/plain’);
header(‘Content-Disposition: attachment; filename=”dumpstructure.sql”‘);
//echo “/*mysqldump.php version $mysqldump_version */\n”;
_mysqldump_table_structure(‘table’);

die();

?>

for usage, you must connect to the database, then calling _mysqldump_table_structure(‘table’); and enter the  table name into parameter function.

header(‘Content-type: text/plain’);
header(‘Content-Disposition: attachment; filename=”dumpstructure.sql”‘);

header script used to create file sql and saved on your directory.

Tags Tags: , , ,
Categories: php
Posted By: asbin
Last Edit: 04 Aug 2009 @ 04 21 AM

EmailPermalinkComments (0)
 04 Aug 2009 @ 3:34 AM 

Author : Ravi Valmikam
this is script function import sql to the database

we have 2 function
1. mysql_import_file : simply imports a file as is
2. mysql_install_db : creates a given DB as well as imports a given SQL file for that DB

PHP Example :

<?
/* Accepts a filename and imports the SQL script in it.

Returns: true if all is well
false if something is wrong
(error message is embedded in $errmsg)

One can also use mysql_error() if this function
returns an error.

*/

function mysql_import_file($filename, &$errmsg)
{
/* Read the file */
$lines = file($filename);

if(!

$lines)
{
$errmsg = "cannot open file $filename";
return
false;
}

$scriptfile = false;

/* Get rid of the comments and form one jumbo line */
foreach($lines as $line)
{
$line = trim($line);

if(!

ereg('^--', $line))
{
$scriptfile.=" ".$line;
}
}

if(!

$scriptfile)
{
$errmsg = "no text found in $filename";
return
false;
}

/* Split the jumbo line into smaller lines */

$queries = explode(';', $scriptfile);

/* Run each line as a query */

foreach($queries as $query)
{
$query = trim($query);
if(
$query == "") { continue; }
if(!
mysql_query($query.';'))
{
$errmsg = "query ".$query." failed";
return
false;
}
}

/* All is well */
return true;
}

/* Installs a DB with a given name with the help of a given
.sql file

Returns: true if all is well
false if something is wrong
(error message is embedded in $errmsg)

One can also use mysql_error() if this function
returns an error.

*/

function mysql_install_db($dbname, $dbsqlfile, &$errmsg)
{
$result = true;

if(!

mysql_select_db($dbname))
{
$result = mysql_query("CREATE DATABASE $dbname");
if(!
$result)
{
$errmsg = "could not create [$dbname] db in mysql";
return
false;
}
$result = mysql_select_db($dbname);
}

if(!

$result)
{
$errmsg = "could not select [$dbname] database in mysql";
return
false;
}

$result = mysql_import_file($dbsqlfile, $errmsg);

return

$result;
}
?>

Usage Example:

PHP Example :

<?

$link

= mysql_connect ( "127.0.0.1", "root", "");

if(

mysql_install_db("testdb", "testdb.sql", $errmsg))
{
echo
"Success!!";
}else
{
echo
"failure: ".$errmsg."<br/>".mysql_error();
}
?>


this script is simple. you can try use on cake php by placing that function on your controller. you must call the function use $this->mysql_import_file(); if the script use on cake php controllers.

Tags Tags: , , , ,
Categories: php
Posted By: asbin
Last Edit: 04 Aug 2009 @ 03 34 AM

EmailPermalinkComments (0)
\/ More Options ...
Change Theme...
  • Users » 1
  • Posts/Pages » 49
  • Comments » 50
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.