Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-02-23 16:02:15

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Values from external file (help from php experts)

Scenario: A small company in precious metals business wants his website to display a table with metals quotations. The values are updated every hour into a simple external text file.

So, in a textpattern page I’m including some php code (see enclosed file). It works as expected.
In short, it opens the remote text file (dati.txt) and gets the updated data, opens a html file (basically a formatted table), fills in the data and finally returns the textpattern page containing the table filled with data.

Two problems
  1. first a minor problem. A php Warning: number_format() expects parameter 1 to be double. I have no idea what it means, since I’m a php noob.
  2. a major problem. In a MLP environment (only Italian and English) there is no way to translate the table, since it is rendered by the php code.

I am sure there must be a more direct elegant way to achieve the data from dati.txt and inject them directly into the textpattern page.
This way I could use a few MLP snippets, leaving the data as is.

Hopefully someone can help me. Thanks

<?php
$lang = $_GET['lang'];
$dati = '';
$file = fopen ("http://my_example.com/dati.txt", "r");
//$file = fopen ("./dati.txt", "r");
if (!$file) {
    echo "<p>Impossibile aprire il file remoto.\n";
    exit;
}
while (!feof ($file)) {
    $dati .= fgets ($file, 1024);
}
fclose($file);
//-----------------------------------------------------------------------------
$filehtml = "./quotazioni.html";
if (file_exists($filehtml)) {
	$fp=fopen($filehtml, "rb") or die("non riesco a leggere il file html");
	$pagina=fread($fp, filesize($filehtml));
	fclose($fp);
} else {
	echo "pagina html non disponibile";
}
//-----------------------------------------------------------------------------
function formatta ($numero, $decimali=2) {
	global $lang;
	if (strpos($numero,",")) {
		$i = strpos($numero,",");
		$numero = substr($numero,0,$i).".".substr($numero,$i+1,strlen($numero));
	}
	switch ($lang) {
		case "en":
			return number_format($numero,$decimali,".",",");
		case "it":
		default:
			return number_format($numero,$decimali,","," ");
	}
}
//-----------------------------------------------------------------------------
preg_match(">ORARIO=(.*)>",$dati,$temp);
$pagina=preg_replace(">@ora>", $temp[1], $pagina);
preg_match(">STERLINA/DOLLARO=(.*)>",$dati,$temp);
$pagina=preg_replace(">@sd>", formatta($temp[1],4), $pagina);
preg_match(">DOLLARO/FRANCO SVIZZERO=(.*)>",$dati,$temp);
$pagina=preg_replace(">@fd>", formatta($temp[1],4), $pagina);
preg_match(">DOLLARO/YEN=(.*)>",$dati,$temp);
$pagina=preg_replace(">@yd>", formatta($temp[1]), $pagina);
preg_match(">EURO/DOLLARO=(.*)>",$dati,$temp);
$pagina=preg_replace(">@ed>", formatta($temp[1],4), $pagina);
preg_match(">ORO/DOLLARI/ONCIA=(.*)>",$dati,$temp);
$pagina=preg_replace(">@od>", formatta($temp[1]), $pagina);
preg_match(">ARGENTO/DOLLARI/ONCIA=(.*)>",$dati,$temp);
$pagina=preg_replace(">@ad>", formatta($temp[1]), $pagina);
preg_match(">PLATINO/DOLLARI/ONCIA=(.*)>",$dati,$temp);
//--------- AND SO ON .......
$pagina=preg_replace(">@rae>", formatta($temp[1]), $pagina);
$pagina=preg_replace(">@ee>", formatta(1,4), $pagina);
//-----------------------------------------------------------------------------
preg_match(">DATA=(.*)/(.*)/(.*)>",$dati,$temp);
$tempo_unix=mktime (0,0,0,$temp[2],$temp[1],$temp[3]);
switch ($lang) {
	case "en":
		$data=date("l j F Y",$tempo_unix);
		break;
	case "it":
	default:
		switch (date("w",$tempo_unix)) {
			case 0:
				$giorno="Domenica";
				break;
			case 1:
				$giorno="Lunedì";
				break;
// ........AND SO ON ...............................
		}
		switch (date("n",$tempo_unix)) {
			case 1:
				$mese="Gennaio";
				break;
			case 2:
				$mese="Febbraio";
				break;
//........ And so on .............
		}
		$data=$giorno." ".date("j",$tempo_unix)." ".$mese." ".date("Y",$tempo_unix);
}
//$data=date ("j / n / Y",$tempo_unix);
$data=htmlentities($data);
$pagina=preg_replace(">@data>", $data, $pagina);
echo $pagina;
?>

Offline

#2 2016-02-23 18:57:46

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Values from external file (help from php experts)

Can you post a representative sample of the text file and what bits of it you want to extract? Oleg’s etc_query is extraordinarily powerful at extracting precise bits of a page if it is available in a structured form, and I’m sure he could give you some pointers. You might then be able to do away with the script altogether and just use that… Oleg can say more…

Regarding your problems with the existing script:

number_format() expects parameter 1 to be double

looks like $numero is a text string and number format is expecting $numero to be a number. You could try adding an extra line to convert the assembled number string into a number, e.g. add floatval afterwards:

$numero = substr($numero,0,$i).".".substr($numero,$i+1,strlen($numero));
 $numero = floatval($numero);

a MLP environment (only Italian and English) there is no way to translate the table, since it is rendered by the php code

The php code looks like it has an in-built method for translations, changing the decimal point to a comma and comma to a space and also outputting localised strings for Jan/Feb, etc.

How are you including / calling the code? If you’re including it as an URL, try adding ?lang=it" or ?lang=en to the end of the url e.g. yourscript.php?lang=it. If you’re including the php directly in your page, try setting the language with a variable by setting a variable on your page to either “en” or “it” and then altering the beginning of your script to use the txp variable instead of the urlvar (the GET bit). Something like:

<txp:variable name="current-lang">en</txp:variable>
<txp:php>
global $variable;
$lang = $variable['current_lang'];
$dati = '';
…

but I’m not sure if it will work to include the code that way.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2016-02-23 19:01:25

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Values from external file (help from php experts)

Change the two lines

number_format($numero,$decimali ...

into:

number_format((float)$numero,$decimali...

As for the second question: you need a custom-made plugin.
Since you’re Italian, make me an offer I can’t refuse ;)

Offline

#4 2016-02-24 09:47:48

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Re: Values from external file (help from php experts)

ruud wrote #297974:

Since you’re Italian, make me an offer I can’t refuse ;)

Exactly I’m Sicilian, so I’m really entitled to make such an offer. LOL
What about a dish of “spaghetti alla Norma” together with a glass of “Nero d’Avola”?

By the way, I tested your (float) suggestion and the error disappeared. So, by now you’ll have pizza and a good beer, when you come in Italy. ;)

Now I am going to try Jakob suggestions.

Thank you guys.

Offline

#5 2016-02-24 14:36:22

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Re: Values from external file (help from php experts)

jakob wrote #297973:

Can you post a representative sample of the text file and what bits of it you want to extract? Oleg’s etc_query is extraordinarily powerful at extracting precise bits of a page if it is available in a structured form, and I’m sure he could give you some pointers. You might then be able to do away with the script altogether and just use that… Oleg can say more…

Here the text file

DATA=24/02/2016
ORARIO=12:51
ORO/DOLLARI/ONCIA=1236,88
ORO/EURO/GRAMMO=36,2625
ARGENTO/DOLLARI/ONCIA=15,361
ARGENTO/EURO/GRAMMO=0,4504
PLATINO/DOLLARI/ONCIA=950,25
PLATINO/EURO/GRAMMO=27,8625
...

the value after = will replace the values “@data”, “@ora”, “@od”, “@oe” … in two html files, one for each language, that will be included.
Maybe I can request to generate a more structured file (xml or JSON) instead of a text file, and then see if I can use Oleg’s etc_query only, getting rid of the whole php code. Then I could just translate just some MLP snippets of text.
Or maybe etc_query can extract the needed bits from the text file… Do you think it can?

In the current form the php code works partially i.e. if manually add ?lang=it or ?lang=en in the browser URL address bar I get the correct language, otherwise I get italian only.
Unfortunately MLP gives a URL in the form of mysite.com/en/quotazioni whereas mysite.com/quotazioni/?lang=en would work.

I can call/include the code directly in the page, or call it by <txp:php> include('myscript.php'); </txp:php>
Either way the problem is the same: I do not know how to get mysite.com/quotazioni/?lang=en

Thanks for your help

Offline

#6 2016-02-24 15:48:42

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Values from external file (help from php experts)

I think you probably have a lot of options there.

etc_query may work and you could then drop your external script entirely. Maybe Oleg can chime in here.

Otherwise, if using php include as you’ve shown already works in your txp page, then you can use the MLP language instead of the url ?parameter to tell the script which language you’re using, e.g.:

<!-- store current MLP language in a txp variable -->
<txp:variable name="current_lang"><txp:l10n_get_lang /></txp:variable>
<txp:php>
  // make txp variables accessible in php context
  global $variable;
  // set $lang to be the first two chars of MLP page's current_lang and make lowercase
  $lang = strtolower(substr($variable['current_lang'], 0 , 2));
  // call external script
  include('myscript.php');
</txp:php>

and modify the first line of myscript.php at the top to this:

<?php
  // check if $lang variable already exists. If not, use the GET url parameter as before.
  if (!isset($lang)) $lang = $_GET['lang'];
  // rest of script
  $dati = '';
  …

I can’t profess to being a php expert but I think that should allow you to pass the current page’s MLP language when calling the script, while still retaining the ability to call it with ?lang=en, if you need it another way. You can leave out the comments :-) I’m not familiar with MLP, but as far as I can tell from the example pages I think that’s the correct tag.

And a third way: If all else fails, you can also include your script in an iframe within your txp page. That does allow you call the script with ?lang=en or ?lang=it appended:

<iframe src="http://www.mydomain.com/myscript.php?lang=en"></iframe>

Maybe one of those helps.


TXP Builders – finely-crafted code, design and txp

Offline

#7 2016-02-24 17:59:55

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Values from external file (help from php experts)

Or replace:

$lang = $_GET['lang'];

with:

$lang = strtolower(substr($_SERVER["REQUEST_URI"], 1, 2));

Offline

#8 2016-02-25 08:34:41

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Re: Values from external file (help from php experts)

jakob wrote #297984:

I can’t profess to being a php expert but I think that should allow you to pass the current page’s MLP language when calling the script, while still retaining the ability to call it with ?lang=en, if you need it another way. You can leave out the comments :-) I’m not familiar with MLP, but as far as I can tell from the example pages I think that’s the correct tag.

I choose this option and, with your code, it is working perfectly. To me, you ARE a php expert. Thanks.
And replacing the $lang snippet, as per Ruud, it works as well. Thanks both of you.
Problem solved.

At this point, I am curious to try etc_query and see if it works. I will ask for Oleg’s help in his plugin thread.

Offline

#9 2016-02-25 13:33:35

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Values from external file (help from php experts)

Phew! Glad it worked :-) Let’s see what Oleg says.


TXP Builders – finely-crafted code, design and txp

Offline

#10 2016-02-25 23:01:33

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Values from external file (help from php experts)

jakob wrote #298002:

Let’s see what Oleg says.

Me says judge for yourself.

Offline

Board footer

Powered by FluxBB