How run several IMPORT application in ONCE

Hello all,
I got many import application to run one after other.
I did them singularly and they run correctly.
Now I want to wrap up in a singular “run_import” application all stepped application in order to insert it later into a “cron” (daily run from server).

Could you suggest how to make the sintax of “run_import” application taking in consideration that I did all singular application finishing running the mySQL import query
and the sc_exit() command.

As not tech this is the way I did . I’m not sure if it can run smootly on server without stopping after first sc_exit() … ;-((
Thanks

See image that is clearer

import.png

You don’t need all those applications, but you can keep them if you need to run separately.
I run a similar application, I use a control or a blank app and put everything into the app on load
Just do commits when you are done with each import

Kevin

Well, Kevin was quicker but I present my idea anyway. :slight_smile:
It’s basically the same.

You have certainly multiple options but as Kevin already mentioned one of the easier approaches is to merge
all scripts into one script (Old Library) as functions. The only thing to pay attention to is to wrap it in a class,
otherwise the SC parser throws an error.
Just copy/paste the content of each script into the new script. At the end of each block check on success and instead of the sc_exit you return an OK or error.

In the run_imports script call those functions one by one by providing the function names in an array (hard coded or read from a table) and go through it with a simple foreach block.
After each step check the return value and proceed or exit.

Library:


class all_imports
{
	function import_customer()
	{
		... php stuff;
                return ok or error;
	}

	function import_supplier()
	{
		... php stuff;
                return ok or error;
	}
}

run_imports script:


$imports = array('import_customer', 'import_supplier', ...);
foreach($imports as $import)
{
    $cur_import = new all_imports();
    $ok = $cur_import->$import();
    if(!$ok)
    {
         throw error;
         exit;
    }
}

jsb

What? I use scripts a lot with success and don’t wrap it on a class. Can you elaborate this?

Hello all and thanks.
So… your are saying that like I did can’t work at all… ???
Do it using function for my level is bit complicated but I will to try.
To see a short sample without function is possible ?

Try calling a function as a variable.


function func_1()
{
    $ret_val = 1;
    return $ret_val;
}



$myfunction = 'func_1';
$ret_val = $myfunction(); //doesn't matter if you use call_user_func() either, with or without namespace parameter.
echo $ret_val;

This notation throws off the parser, but when you wrap your functions in a class it works fine (see above).

jsb

Hi JSB,
I didn’t use function before so for me it’s bit complicated.
Where I have to use this code ?
In each application or in a new blank code ?

Hi. No problem it’s less complicated as it sounds.

  1. Create a new script/library:
    Main menu -> Tools -> Scripts (Libraries if your SC < 8.1).
    Select the scope of the script.
    Give it a name (i.e. import_functions).
    Define a class and your first function.

class all_imports
{
   	function import_customer()
	{
		copy/paste your php stuff from your import_customer script but leave out the sc_exit()
                return ok (true) or error (false);
	}

	function import_supplier()
	{
		copy/paste your php stuff from your import_supplier script but leave out the sc_exit()
                return true (everythin is ok) or false (an error occured);
	}
}


Add a function for all your needed imports in the same way but I would start with one function first to get it working.
Save it.

  1. Edit your run_import script.
    Delete everything and put in the following:

$imports = array('import_customer', 'import_supplier', ...);//again list all your import function names here but start with one and get it working
foreach($imports as $import)
{
    $cur_import = new all_imports();
    $ok = $cur_import->$import();
    if(!$ok)
    {
         error; //maybe save $import (it contains the function name) to a table, so you know which function misbehaved.
         exit; //or omit this step if you want to import the rest of it.
    }
}

Open the programming section in the left pane, click on ‘Libraries’ and select the script created in step one.
Save, compile, pray and run (as far as you possibly can). :smiley:
Disclaimer: If your machine explodes, I have nothing to do with it!!

Good Luck
jsb

Hey JSB , GREAT !!! Tomorrow I’ll try all the stepped procedure . Thanks so much !!

Just a doubt… All my query are almost the same as names and definition but the query content obviously that is Always different. In this case $sql_customers is only here.
May I to repeat some names of variable in each function space ?? without making a mess ??

This one of these. For istance $sql_update is all the same within different queries.

$sql_customers=“SELECT CodAnag AS CustomerID, Ragione sociale AS CompanyName, NomeContatto AS ContactName, Indirizzo AS Address, Localita AS City, Provincia AS Region, CAP AS PostalCode, Nazione AS Country, Zona, NumeroTelefono AS Phone, NumeroFax AS Fax, Cell AS Mobile, PartitaIVA as VatCode, Codice Fiscale AS CodFisc, Email, Codag AS SalesmanID, Id anag AS Id_anag FROM b2b_anagrafica WHERE (((Left(CodAnag,1))=‘C’)) ORDER BY CodAnag”;

sc_lookup(rs, $sql_customers, “conn_mysql_import”);

$thevalues = 'INSERT INTO customers_temp (CustomerID,CompanyName,ContactName,Address,City,Region,PostalCode,Country,Zona,Phone,Fax,Mobile,VatCode, CodFisc, Email,SalesmanID,Id_anag) values ';

foreach($rs as $rec)
{

$thevalues1 = "(";

foreach ($rec as $field)
{
$thevalues1 = $thevalues1 ."’" . str_replace("’", “’”, $field) . “’,”;

}
$thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1);
$thevalues1 = $thevalues1 . "),";
$thevalues =  $thevalues.$thevalues1 ;

}

$thevalues = substr($thevalues, 0, strlen($thevalues)-1).";";

// echo $thevalues;

$sql_temp_table=“CREATE TEMPORARY TABLE IF NOT EXISTS customers_temp LIKE customers”;
$sql_temp_trunc=“TRUNCATE TABLE customers_temp”;

sc_exec_sql($sql_temp_table, “conn_mysql”);
sc_exec_sql($sql_temp_trunc, “conn_mysql”);

sc_exec_sql($thevalues, “conn_mysql”);

$sql_update=“INSERT INTO customers(CustomerID,CompanyName,ContactName,Address,City,Region,PostalCode,Country,Zona,Phone,Fax,Mobile,VatCode, CodFisc, Email, User, Password, UserType, SalesmanID, Active, Admist_priviledge, Id_anag)
SELECT t.CustomerID , t.CompanyName , t.ContactName , t.Address , t.City , t.Region , t.PostalCode , t.Country , t.Zona , t.Phone , t.Fax , t.Mobile , t.VatCode, t.CodFisc, t.Email , t.CustomerID, ‘12345’ , ‘CLI’, t.SalesmanID , ‘Y’, 0, t.Id_anag
FROM customers_temp t ON DUPLICATE KEY UPDATE
CompanyName = t.CompanyName ,
ContactName = t.ContactName ,
Address = t.Address ,
City = t.City ,
Region = t.Region ,
PostalCode = t.PostalCode ,
Country = t.Country ,
Zona = t.Zona ,
Phone = t.Phone ,
Fax = t.Fax ,
Mobile = t.Mobile ,
VatCode = t.VatCode,
CodFisc = t.CodFisc,
Email = t.Email ,
UserType = ‘CLI’ ,
SalesmanID = t.SalesmanID ,
Active = ‘Y’,
Admist_priviledge = 0,
Id_anag = t.Id_anag”;

sc_exec_sql($sql_update, “conn_mysql”);

[QUOTE=giovannino;39459]Just a doubt… All my query are almost the same as names and definition but the query content obviously that is Always different. In this case $sql_customers is only here.
May I to repeat some names of variable in each function space ?? without making a mess ??
[/QUOTE]

You have to think of each function as a separate script. They are executed one after another like you did with your separate scripts manually.
So you not only ‘may’ you actually have to repeat everything that’s in your scripts.

jsb

Hello JSB,
I couldn’t resist to try so I did it this evening.

I created import_functions.php with all functions inside (see attached txt) as Public

and then the blank run_import application with this inside:

$imports = array(‘import_categories’,
‘import_customer’,
‘import_orders’,
‘import_order_details’,
‘import_price_list’,
‘import_price_list_std’,
‘import_products’,
‘import_salesmen’,
‘import_suppliers’
); //again list all your import function names here but start with one and get it working
foreach($imports as $import)
{
$cur_import = new all_imports();
$ok = $cur_import->$import();
if(!$ok)
{
error; //maybe save $import (it contains the function name) to a table, so you know which function misbehaved.
exit;
}
}

Now it give me an error like this:

Fatal error: Class ‘all_imports’ not found in C:\Program Files (x86)\Scriptcase\v8_new\wwwroot\scriptcase\app\B2B_B2C\run_import\index.php on line 1054

This is the line area of error:

//
$_SESSION[‘scriptcase’][‘run_import’][‘contr_erro’] = ‘on’;
$imports = array(‘import_categories’,
‘import_customer’,
‘import_orders’,
‘import_order_details’,
‘import_price_list’,
‘import_price_list_std’,
‘import_products’,
‘import_salesmen’,
‘import_suppliers’
);
foreach($imports as $import)
{
$cur_import = new all_imports(); <---------------- 1054 line
$ok = $cur_import->$import();
if(!$ok)
{
error;
exit;
}
}

import_functions.php.txt (19 KB)

You have to check import_functions.php in Programming -> Libraries.

Also, since you do not return any value from your functions,
this will not work:

$ok = $cur_import->$import();
if(!$ok)
{
error;
exit;
}

You just do:
foreach($imports as $import)
{
$cur_import = new all_imports();
$cur_import->$import();
}

As I said, you should have started it with only one function and expand when it’s working. :slight_smile:

jsb

Hi JSB
I activated the library under Programming Libraries and now the problem of undefined class is fixed.

Running the blank application now I got problems with SC macro function like sc_exec_sql and sc_lookup.

Fatal error: Call to undefined function sc_lookup() in C:\Program Files (x86)\Scriptcase\v8_new\wwwroot\scriptcase\app\B2B_B2C\run_import\all_imports.php on line 67

It seems that it doesn’t undestand this function on class.

Does it make sense ???

Oh sh…!!! My bad. Must have been late.
This should fix it.

Open your run_import application.
Create a PHP method (all_imports).
Copy/Paste all your functions (without the class) from your all_imports.php script into the method (You could also create a separate PHP Method for each function).
Uncheck all_imports.php under Programming -> Libraries.
Edit the run_import script, change:

foreach($imports as $import)
{
$cur_import = new all_imports();
$cur_import->$import();
}

to

foreach($imports as $import)
{
$this->$import();
}

jsb

Okey I did modifications but before to run the blank application I’m wondering if sintax is good,

When you call a function from a method you have to add () after the name ? i.e. import_categories() ,
On my array I don’t use any () . Is it correct ??

$imports = array( ‘import_categories’,
‘import_customer’,
‘import_orders’,
‘import_order_details’,
‘import_price_list’,
‘import_price_list_std’,
‘import_products’,
‘import_salesmen’,
‘import_suppliers’
); //again list all your import function names here but start with one and get it working

foreach($imports as $import)
{
$this->$import();
}

OK tested without () . It works like charm !!! Many thanks
Now I have to insert it within CRON as “run_import.php”. Correct ?