afidegnum
Posts: 16
|
| Posted: 03/01/2008, 3:53 PM |
|
Hello good morning all
please I am currently developing a project using Codecharge. this is what it does. it is about imporing csv file into mysql. I was looking for ways and means to get it right. and secondly, I am looking for teh file name as well as hte directory location I can hook it and make the function work but I need assistance from your part too.
actually, I have took an example for converting a file to pdf that I would like to modify. below are going to be a pdf conversion and pdf conversion code. I will be grateful have your assistance:
----------------------------------in a form event, Add Action, convert file to pdf.-------------------------------
<?php
//BindEvents Method @1-B7EE17E1
function BindEvents()
{
global $import;
$import->FileUpload1->CCSEvents["AfterProcessFile"] = "import_FileUpload1_AfterProcessFile";
}
//End BindEvents Method
//import_FileUpload1_AfterProcessFile @3-A00A9FB4
function import_FileUpload1_AfterProcessFile(& $sender)
{
$import_FileUpload1_AfterProcessFile = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $import; //Compatibility
//End import_FileUpload1_AfterProcessFile
//Get Original Filename @8-10353880
$control_value = $Component->GetValue();
$original_filename = CCGetOriginalFileName($control_value);
$Component->SetValue($original_filename);
//End Get Original Filename
//Convert Page to PDF @7-B62AF1B0
if (CCGetFromGet("_convert")) {
global $main_block;
$Command = '"' . "" . '"';
$Options = "{InputFile} -f {ResultFile} -root {Root}";
$TempFolder = isset($_ENV["TEMP"]) ? $_ENV["TEMP"] : getenv("TEMP");
if (CCSubStr($TempFolder, -1) != DIRECTORY_SEPARATOR && CCSubStr($TempFolder, -1) != "/" && CCSubStr($TempFolder, -1) != "\\") $TempFolder .= DIRECTORY_SEPARATOR;
$TempFileName = $TempFolder . "pdf" . session_id() . mt_rand(100000, 999999) . ".html";
$ResultFileName = $TempFolder . "pdf" . session_id() . mt_rand(100000, 999999) . ".pdf";
$WindowsQuote = DIRECTORY_SEPARATOR == "\\" ? '"' : "";
if ($TempFileName && $TmpFH = @fopen($TempFileName, "wb")) {
fwrite($TmpFH, $main_block);
fclose($TmpFH);
$Options = str_replace("{InputFile}", escapeshellarg($TempFileName), $Options);
$Options = str_replace("{Root}", escapeshellarg(getcwd()), $Options);
if (strpos($Options, "{ResultFile}") !== false && !file_exists($ResultFileName)) {
$Options = str_replace("{ResultFile}", escapeshellarg($ResultFileName), $Options);
exec($WindowsQuote . $Command . " " . $Options . $WindowsQuote);
if ($fh = @fopen($ResultFileName , "rb")) {
$now = time();
$ServerDate = gmdate("D, d M Y H:i:s", $now) . " GMT";
$ExpireDate = gmdate("D, d M Y H:i:s", $now + 60) . " GMT";
Header("Cache-control: ");
Header("Pragma: ");
Header("Date: " . $ServerDate);
Header("Expires: " . $ExpireDate);
Header("Content-Type: application/pdf");
while($str = fread($fh, 1024)) echo $str;
fclose($fh);
unlink($ResultFileName);
$import_FileUpload1_AfterProcessFile = false;
}
} elseif ($fh = popen($WindowsQuote . $Command . " " . $Options . $WindowsQuote, "rb")) {
$firstPass = true;
while($str = fread($fh, 1024)) {
if ($firstPass) {
$now = time();
$ServerDate = gmdate("D, d M Y H:i:s", $now) . " GMT";
$ExpireDate = gmdate("D, d M Y H:i:s", $now + 60) . " GMT";
Header("Cache-control: ");
Header("Pragma: ");
Header("Date: " . $ServerDate);
Header("Expires: " . $ExpireDate);
Header("Content-Type: application/pdf");
$firstPass = false;
}
echo $str;
}
pclose($fh);
$import_FileUpload1_AfterProcessFile = false;
}
unlink($TempFileName);
}
}
//End Convert Page to PDF
//Close import_FileUpload1_AfterProcessFile @3-D0979D85
return $import_FileUpload1_AfterProcessFile;
}
//End Close import_FileUpload1_AfterProcessFile
---------------------------------------------------------------------------------------------------------------------------
=========================================================================
now the file that will output the csv data to mysql
-----------------------------------------------------------------------------------------------------------------------------
$link_id = mysql_connect("host_name", "username", "password") or die("Could not connect.");
if(!mysql_select_db("your_database_name",$link_id)) die("database was not selected.");
$file_handle = fopen("my_csv_file.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query="INSERT into my_table_name(user_name,user_address,user_class) values('$line_of_data
[0]','$line_of_data[1]','$line_of_data[2]')";
mysql_query($line_import_query) or die(mysql_error());
}
-----------------------------------------------------------------------------------------------
what I am looking for is to integrate the script so after uploading, it can import the csv data into mysql. thanks in advance.
sincerely,
PS. before I continue, I would like to know how possible can we create a cron job with Codecharge ? specially sending out mails
thanks
|
 |
 |
magus
Posts: 98
|
| Posted: 03/01/2008, 6:42 PM |
|
Greetings,
I did something similar to this recently in a job where the client wanted to update a mysql "Web Directory" database by uploading an Excel file.
I made a form with a file upload component. I processed the file at the onValidate stage - see sample code below. My habit is to add custom functions near the top of the _events page, other people call a separate file .
Anyway, you would put your processing in the event function below or into a function that is called by the event code.
The only problem I had was getting the page to redirect properly after processing. For this I had to hack the operations method just following the query as to which button was pressed.
if($this->PressedButton == "Button_Insert") {
// Changed to Effect the correct redirect
// $Redirect = "dir_update.php?mode=completed&count=".$GLOBALS['insertCount']."" . "?" . CCGetQueryString("QueryString", array("ccsForm"));
$Redirect = "dir_update.php?mode=completed&count=".$GLOBALS['insertCount'];
Below is the onValidate sample code.
As for the chron job question, it is possible to call php from the command line if it has been built with the cli option. If not, you might script an http call to the page that generates the emails.
Cheers,
Don
//update_OnValidate @2-8E0F53CC
function update_OnValidate(& $sender)
{
$update_OnValidate = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $update; //Compatibility
//End update_OnValidate
//Custom Code @6-2A29BDB7
// -------------------------
global $skippedCount, $insertCount;
$skippedCount = 0;
$insertCount = 0;
// echo "Update on Validate<br>";
// echo "DataFile ".$Component->Updater->TemporaryFolder . $Component->Updater->GetValue();
if(!strlen($Component->Updater->GetValue())) {
$update->Errors->addError("No Upload File - Please try again ") ;
} else {
if(update_Directory($Component->Updater->TemporaryFolder.$Component->Updater->GetValue()) ) {
// redirect with sucess parameter
// global $Redirect;
// $update->Errors->addError("Successful Update") ;
// $Redirect = "index.php?mode=completed&count=".$insertCount;
}
}
// -------------------------
//End Custom Code
//Close update_OnValidate @2-674B4B26
return $update_OnValidate;
}
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/01/2008, 9:56 PM |
|
Thanks Don for your prompt reply,
but actually, the function I would like to build is to read a file from the temp folder, output a csv, (comma separated values) and insert it straight away to the database,
here we will be using fgetcsv, which is an in build function in php to read csv files, and update it to teh database,. now, my main concern is getting the file name, as well as teh directory. I mean hooking to the file, and how to formuate the query t make that functionality easier ?
thanks once again for your replay
|
 |
 |
magus
Posts: 98
|
| Posted: 03/01/2008, 10:29 PM |
|
Hi There,
The code shows how to find the temporary file.
$Component->Updater->TemporaryFolder.$Component->Updater->GetValue()
update_Directory() is the name of the function I used to read the excel file and do the inserts. I assumed you had code to do that. Just replace "update_Directory" with your code or your function name.
if(!strlen($Component->Updater->GetValue()))
{
$update->Errors->addError("No Upload File - Please try again ") ;
} else {
if(!update_Directory($Component->Updater->TemporaryFolder.$Component->Updater->GetValue()))
{
$update->Errors->addError("Error Processing File - Please try again ") ;
}
}
Regards,
Don A
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/01/2008, 11:10 PM |
|
thanks Don,
I am writing the function now and trying it. I had an error but I am trying again,.
but secondly, I would like to know about writing your own code in the event properties,
do you have any documentation about the guidelines we should follow, in terns of knowing the variable and the functions that are already into play? I am trying to understand the whole process, but it is still confusing.. because this is a generated code. which is a great job.,
if I write my own application, even while sleeping, I can know all the functions methods and properties.that are into play, and you can wake up and modify them how you want it.
now, we are working with a different job doing a heavy job for us. but in order for performance tuning, we still have to know the important elements that are working
I hope you got what I mean.
do you know any documentation that can help in case you are writing your own code, where do we start from, and where do we hook into so we can be sure of running the applications confidently,
thanks
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/01/2008, 11:28 PM |
|
for a very simple example here, I have a sample file upload, on which I have click the upload compoment on, and I add event to it, where I want to optionaly add code:
here is a generated code I have got.
----------------------------------------------------------------------------------------------------------------
<?php
//BindEvents Method @1-B7EE17E1
function BindEvents()
{
global $import;
$import->FileUpload1->CCSEvents["AfterProcessFile"] = "import_FileUpload1_AfterProcessFile";
}
//End BindEvents Method
//import_FileUpload1_AfterProcessFile @3-A00A9FB4
function import_FileUpload1_AfterProcessFile(& $sender)
{
$import_FileUpload1_AfterProcessFile = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $import; //Compatibility
//End import_FileUpload1_AfterProcessFile
//Custom Code @7-2A29BDB7
// -------------------------
// Write your own code here.
// -------------------------
//End Custom Code
//Close import_FileUpload1_AfterProcessFile @3-D0979D85
return $import_FileUpload1_AfterProcessFile;
}
//End Close import_FileUpload1_AfterProcessFile
?>
------------------------------------------------------------------------------------------------------
here I am finding difficult to locate the file name, I would like to know the classes that are in play so I can add my custom code accordingly, or it possible to write a custom code without the library if code in place?
|
 |
 |
magus
Posts: 98
|
| Posted: 03/02/2008, 12:18 AM |
|
Greetings,
First, you insert your code where it says to:
//Custom Code @7-2A29BDB7
// -------------------------
// Write your own code here.
// -------------------------
//End Custom Code
Try this:
if(!strlen($Component->FileUpload1->GetValue()))
{
$import->Errors->addError("No Upload File - Please try again ") ;
} else {
if(!YOURFUNCTION NAME($Component->FileUpload1->TemporaryFolder.$Component->FileUpload1->GetValue()))
{
$import->Errors->addError("Error Processing File - Please try again ") ;
}
}
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/02/2008, 9:07 PM |
|
hi don,
the script succesfuly was able to upload teh file but it shows all the validation errors such as file not uploaded and file exceeded the maximum size.
secondly, I added my code to it. but unfortunately, it has bot been able to parse the csv data to mysql . I even added additional event to retrieve the records. but nothing comes. I checked the table, nothing is there.
thanks for your helping efforts.
this is how i DId it.
--------------------------------------------------------------------------------------------------------------------------
<?php
//BindEvents Method @1-B7EE17E1
function BindEvents()
{
global $import;
$import->FileUpload1->CCSEvents["AfterProcessFile"] = "import_FileUpload1_AfterProcessFile";
}
//End BindEvents Method
//import_FileUpload1_AfterProcessFile @3-A00A9FB4
function import_FileUpload1_AfterProcessFile(& $sender)
{
$import_FileUpload1_AfterProcessFile = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $import; //Compatibility
//End import_FileUpload1_AfterProcessFile
//Custom Code @7-2A29BDB7
if(!strlen($Component->FileUpload1->GetValue()))
{
$import->Errors->addError("No Upload File - Please try again ") ;
} else {
if(!fgetcsv($Component->FileUpload1->TemporaryFolder.$Component->FileUpload1->GetValue(), 1000,","))
{
$import->Errors->addError("Error Processing File - Please try again ") ;
}
}
while ($line_import_data = fgetcsv($Component->FileUpload1->TemporaryFolder.$Component->FileUpload1->GetValue(), 1000,",")){
$line_import_query = "INSERT INTO prospects (pros_fname,pros_lname,pros_email) values('$line_import_data
[0]','$line_import_data[1]','$line_import_data[2]')";
mysql_query($line_import_query) or die(mysql_error());
}
//End Custom Code
//Retrieve number of records @8-ABE656B4
$Component->SetValue($Container->DataSource->RecordsCount);
//End Retrieve number of records
//Close import_FileUpload1_AfterProcessFile @3-D0979D85
return $import_FileUpload1_AfterProcessFile;
}
//End Close import_FileUpload1_AfterProcessFile
?>
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/02/2008, 9:11 PM |
|
that was the error message posted:
Unable to upload the file specified in import comma separated address in a form of First name, last name and email - upload folder doesn't exist.
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/02/2008, 9:30 PM |
|
I solved the folder errors. I have to manually create the folder in the directory. but when I preview it. to upload the files, it doesn't not show the upload file and even to parse it.
using the same posted function.
thanks
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/02/2008, 9:33 PM |
|
I have also noticed something now.
on the url address, it point to an empty variable request
here it is.
http://localhost/responser/import_contacts.php?
|
 |
 |
magus
Posts: 98
|
| Posted: 03/03/2008, 2:55 PM |
|
Hi There,
I think it isn't working because you haven't opened the file with fopen before trying to read a line with fgetcsv.
I can't see your example because localhost always points to the local machine.
Regards,
Don A
Here is an usage example from the php manual.
<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/03/2008, 11:14 PM |
|
hello,
I have got it working but another funny error appears.
Fatal error: Call to a member function GetValue() on a non-object in C:\wamp\www\responser\prospects_imp_events.php on line 21
after writing this code.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
<?php
//BindEvents Method @1-113D0652
function BindEvents()
{
global $importForm;
$importForm->FileUpload1->CCSEvents["BeforeProcessFile"] = "importForm_FileUpload1_BeforeProcessFile";
}
//End BindEvents Method
//importForm_FileUpload1_BeforeProcessFile @3-DBCE0183
function importForm_FileUpload1_BeforeProcessFile(& $sender)
{
$importForm_FileUpload1_BeforeProcessFile = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $importForm; //Compatibility
//End importForm_FileUpload1_BeforeProcessFile
//Custom Code @9-2A29BDB7
if(!strlen($Component->FileUpload1->GetValue()))
{
$import->Errors->addError("No Upload File - Please try again ") ;
} else {
$file = "$Component->FileUpload1->TemporaryFolder.$Component->FileUpload1->GetValue()";
{
$import->Errors->addError("Error Processing File - Please try again ") ;
}
$db = new clsDBConnection1();
$handle = fopen($file,"r");
$row = 1;
$handled = fopen("$handle", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
$SQL = "INSERT INTO prospects (pros_fname,pros_lname,pros_email) values('$data[0]','$data[1]','$data[2]')";
$db->query($SQL);
$db->close();
}
}
fclose($handled);
}
//End Custom Code
//Close importForm_FileUpload1_BeforeProcessFile @3-F4C24C40
return $importForm_FileUpload1_BeforeProcessFile;
}
//End Close importForm_FileUpload1_BeforeProcessFile
?>
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
|
 |
 |
afidegnum
Posts: 16
|
| Posted: 03/03/2008, 11:15 PM |
|
that happened after I have uploaded the text file
|
 |
 |
|