Friday, June 26, 2009

Php- Table editable dynamic form with edit + selection (select / option) + checkbox.

EditTable.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head>
    <title>Editable Table - by Fabio Pintore </title>
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="it" />
    <meta name="Robots" content="All" />
    <meta name="Description" content="Editable Table" />
    <meta name="Keywords" content="Tabella Editabile con campi fissi (edit) + selezione (select/option) + checkbox" />
    <meta name="Owner" content="Fabio Pintore (f.pintore@tiscali.it)" />
    <meta name="Author" content="Fabio Pintore (f.pintore@tiscali.it)" /> 
    <meta name="Copyright" content="Fabio Pintore (f.pintore@tiscali.it)" />
    <link rel="stylesheet" href="EditTable.css" />
    <script type="text/javascript" src="EditTable.js"></script>
</head>
<body>
<h1>Editable Table</h1>
<form name="FA">
<table cellpadding="0" cellspacing="0" border="0" class="editable" id="editable">
    <thead>   
        <tr>
            <th width="200px">Nome</th>
            <th width="200px">EMail</th>
            <th width="90px">Sesso</th>
            <th width="30px">Socio</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td>Aldo Rossi</td>
        <td>a.rossi@emailserver.it</td>
        <td>Maschio</td>
        <td>Si</td>
        </tr>
       
        <tr>
        <td>Maria Verdi</td>
        <td>m.verdi@emailserver.it</td>
        <td>Femmina</td>
        <td>No</td>
        </tr>
       
        <tr>
        <td>Marco Bianchi</td>
        <td>marcobianchi@unaltroemailserver.it</td>
        <td>Maschio</td>
        <td>Si</td>
        </tr>

    </tbody>
</table>
<br/>
<br/>
<script type="text/javascript">
var elArray = makeArray();
var editable=new table.editable(elArray);
editable.init();   
</script>


    <button type="button" onclick="editable.Insert();" name="Insert" value="INSERISCI">
        <b>Inserisci</b>
    </button>
   
    <button type="reset" onclick="editable.Annulla();">
        <b>Annulla</b>
    </button>
       
    <button type="button" onclick="editable.Apply();" name="Apply" value="APPLY">
        <b>Salva</b>
    </button>

</form>
<br/>
<br/>
<a href="http://www.shooney.com/htm/examples/edittable/EditTable.zip">Download this example (Zip File)</a>
</body>
</html>

EditTable.js
******************************************************************************************************************************************************
//  Author    : Fabio Pintore (f.pintore@tiscali.it)
//  Copyright : Fabio Pintore (f.pintore@tiscali.it)
//  Date      : 18 Giugno 2009
//  Descr     : Editable Table
//              Tabella Editabile con campi fissi (edit) + selezione (select/option) + checkbox
//  Note      : [ IT IS FREEWARE ] Lo script ט liberamente e gratuitamente scaricabile e migliorabile.
//              Mi piacerebbe sapere se vi ט stato di aiuto.         
// *******************************************************************************************************************************************************
function oIElText(nameEl){    this.nameEl=nameEl}
oIElText.prototype.Draw = function(val, w0) { return ('<input class="inp" style="width:'+w0+'px" type="text" value="'+val+'" name="'+this.nameEl+'" />'); }
oIElText.prototype.GetValue = function() { return (eval('document.FA.'+this.nameEl+'.value') ); }

function oIElCheck(nameEl){ this.nameEl=nameEl; }
oIElCheck.prototype.Draw = function(val, w0) {
    var app='';
    if (val=='Si') { app='checked="checked"' }
    return ('<input class="inp" type="checkbox" '+app+' value="'+val+'" name="'+this.nameEl+'" />');
}   
oIElCheck.prototype.GetValue = function() { return ( eval('document.FA.'+this.nameEl+'.checked') ? 'Si':'No'); }
   
function oIElComboOpt(dbcode, dbdescr){    this.dbcode=dbcode;    this.dbdescr=dbdescr;}
function oIElCombo(nameEl, dbopt){this.nameEl=nameEl; this.dbopt=dbopt; }
oIElCombo.prototype.Draw = function(val, w0) {
    var app=''; resHTML='';
    resHTML='<select class="inp" style="width:'+w0+'px" name="'+this.nameEl+'" />';
    var cb=this.dbopt;
    for(var j=1;j<cb.length;j++){
        app='';
        if (cb[j].dbdescr == val) {app=' selected '}
        resHTML+='<option value="'+cb[j].dbcode+'" '+app+' >'+cb[j].dbdescr+'</option>';
    }   
    resHTML+='</select>';
    return (resHTML);
}   
oIElCombo.prototype.GetValue = function() {
    var ret='';
    var app=eval('document.FA.'+this.nameEl+'.value');
    var cb=this.dbopt;
   
    for(var j=1;j<cb.length;j++){
        if (cb[j].dbcode == app) {ret=cb[j].dbdescr;}
    }   
    return (ret);
}

function makeArray(){
    var elArray = new Array();
    var optArray = new Array();
   
    optArray[1] = new oIElComboOpt('M','Maschio');
    optArray[2] = new oIElComboOpt('F','Femmina');

    elArray[0]=new oIElText('dbNome');
    elArray[1]=new oIElText('dbEMail');
    elArray[2]=new oIElCombo('dbSesso', optArray);
    elArray[3]=new oIElCheck('dbSocio');
   
    return (elArray);
}

var table=function(){
   
    function editable(elArray){
        this.t; this.w;
        this.Clik=1; this.savedTX=0;this.savedValue=[];this.savedClassTX='';
        this.elArray=elArray; this.Action='';
    }
   
    editable.prototype.init=function(){
        this.t=document.getElementById('editable'); this.w=this.t.rows[0].cells.length;
        for(var i=1;i<this.t.rows.length;i++){    this.t.rows[i].onclick = new Function('editable.doEdit(this)');    }   
    }
   
    editable.prototype.doEdit=function(tx){
        var resHTML=''; w0=0;
       
        if (this.Clik==1){
            this.Clik=0; this.Action="UPD"; this.savedTX = tx;     this.savedClassTX = tx.className;
           
            tx.className='clik';
            tx.onclick='';
   
            for (var i=0;i<this.w;i++){
                w0 = tx.cells[i].offsetWidth;
                this.savedValue[i]=tx.cells[i].childNodes[0].nodeValue;
                resHTML='';
                resHTML = this.elArray[i].Draw(this.savedValue[i], w0);
                tx.cells[i].innerHTML = resHTML;
            }   
        }else{
            if (this.savedTX != tx) { alert ("devi salvare o annullare !") }
        }   

    }   
   
    editable.prototype.Annulla=function(){
        if (this.Action=="UPD"){
            for (var i=0;i<this.w;i++){ this.savedTX.cells[i].innerHTML=this.savedValue[i]; }
            this.savedTX.className=this.savedClassTX;
            this.savedTX.onclick = new Function('editable.doEdit(this)');
        }else {
            if (this.Action=="INS") {  this.t.deleteRow(-1); }   
        }
        this.Action=""; this.Clik=1;
    }
   
    editable.prototype.Apply=function(){
        // FUTURE :  Validate Field - AjaxRequest - ....
        this.TransactOK();
    }
   
    editable.prototype.Insert=function(){
        var z = Array(); resHTML=''; w0=0;
        if (this.Clik==1){
            this.Clik=0; this.Action="INS";
           
            var x=this.t.insertRow(-1);
            this.savedTX = x; this.savedClassTX=x.className;
            x.className='clik';
           
            for (var i=0;i<this.w;i++){
                z[i]=x.insertCell(i);
                w0 = this.t.rows[0].cells[i].offsetWidth;
                resHTML='';
                resHTML = this.elArray[i].Draw('', w0);
                z[i].innerHTML = resHTML;
            }
           
        }else{ alert ("devi salvare o annullare !"); }
    }
   
    editable.prototype.TransactOK=function(){
        for (var i=0;i<this.savedTX.cells.length;i++){     this.savedTX.cells[i].innerHTML=this.elArray[i].GetValue(); }
        this.Clik=1; this.Action=''; this.savedTX.className=this.savedClassTX;
        this.savedTX.onclick = new Function('editable.doEdit(this)');
    }

    return{editable:editable}
   
}();





EditTable.css

body {font:12px Verdana,Arial}

.editable{border:1px solid #ccc; border-bottom:none}
.editable th {padding:4px 6px 6px; background:#444; color:#fff; text-align:left; color:#ccc}
.editable td {padding:2px 4px 4px; background:#fff; border-bottom:1px solid #ccc}
.editable tr:hover {color:#ff0000;outline-style:solid;}
.editable .clik td {background:#00ff80}

.inp {font:12px Verdana,Arial}

Php - Blacklist IPs - Simple way to blacklist IP's, good for instance when you want to stop certain IP ranges known to be spammers.

?php

function IP_Blacklist($minimum = null, $maximum = null)
{
   
$ip = ip2long($_SERVER['REMOTE_ADDR']);

    if (
is_null($minimum) === false)
    {
       
$minimum = explode('.', $minimum);

        foreach (
$minimum as $key => $value)
        {
           
$minimum[$key] = ($value == '*') ? 0 : $value;
        }

       
$minimum = ip2long(implode('.', $minimum));

        if (
$ip >= $minimum)
        {
            return
false;
        }
    }

    if (
is_null($maximum) === false)
    {
       
$maximum = explode('.', $maximum);

        foreach (
$maximum as $key => $value)
        {
           
$maximum[$key] = ($value == '*') ? 255 : $value;
        }

       
$maximum = ip2long(implode('.', $maximum));

        if (
$ip <= $maximum)
        {
            return
false;
        }
    }

    return
true;
}
?>



Usage $_SERVER['REMOTE_ADDR'] = 195.22.23.24:


 
<?php
IP_Blacklist
('195.22.23.*'); // false, blacklisted
IP_Blacklist('195.0.0.0', '195.255.255.255'); // false, blacklisted
IP_Blacklist('195.22.23.25', '195.22.23.255'); // true, clean

if (IP_Blacklist('195.22.23.*', '195.22.23.*') === false)
{
    die(
'Nice try spammer.');
}
?>

Monday, June 22, 2009

PHP- Google Currency Converter - Convert variable amounts between currencies with a little help from our friend Google.

<?php

function Google_Currency($amount, $from, $to)
{
$matches = array();

if (
preg_match('~<b>.+? = (.+?)</b></h2>~', file_get_contents('http://www.google.com/search?q=' . urlencode(floatval($amount) . ' ' . strtoupper($from) . ' to ' . strtoupper($to))), $matches) > 0)
{
if (
array_key_exists(1, $matches) === true)
{
return
floatval($matches[1]);
}
}

return
false;
}
?>





Usage:

<?php
Google_Currency
(1, 'USD', 'EUR'); // 0.768816791
Google_Currency(1, 'USD', 'GBP'); // 0.686671702
Google_Currency(1, 'EUR', 'USD'); // 1.3007
Google_Currency(1, 'EUR', 'GBP'); // 0.893153883
Google_Currency(1, 'GBP', 'USD'); // 1.4563
Google_Currency(1, 'GBP', 'EUR'); // 1.11962789
?>

Link Submition - Allow your visitors to submit links to the site.

Install Instructions.
---------------------

1) Add your information in the variables.php, which is fully commented to make
it easy for you. You should not have to change the first 2 variables. The other
variables are for the Page Title, Text and links above and below on the page.

2) Upload linklist.php, footer.php, header.php, submit.php, variables.php, index.php,
emails.txt and links.txt in ASCII mode to the directory /linklist. Then, upload
browser.gif to the directory /images in BINARY or Auto mode.

3) CHMOD emails.txt and links.txt 777.

3) Your done, to view your Link List page, go to www.yourdomain.com/links/index.php.
To add a link, go to www.yourdomain.com/links/submit.php.


footer.php

<center>
<br>
<font >
<?php
echo $text3 ?>
<br>
<?php echo $text4 ?><br>
<a href="<?php echo $url2 ?>" target=_blank">
<?php
echo $urltext2 ?>
</a></font> <br>
</center><br>
<div align="center"><a href="submit.php"><font size="3" face="Verdana, Arial, Helvetica, sans-serif">Click
Here To Submit Your Site</font></a></div><br>
</html>


index.php

<?php
include "header.php";
?>
<?php
$data
= file('links.txt');
$data = array_reverse($data);
foreach(
$data as $element) {
$element = trim($element);
$pieces = explode("|", $element);
echo
$pieces[2] . $pieces[1] . "" . $pieces[0] . "";
}
?>
<?php
include "footer.php";
?>



header.php

<?php
require "variables.php";
?>
<html>
<head>
<title><?php echo $title ?> </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#006699" text="#FFFFFF" link="#FFFFFF" vlink="#FFFF00" alink="#FF0000"><font color="#FFFFFF" size="1" face="verdana">
<br>
<center><font size="4">
<?php echo $text1 ?>
<br>
<?php echo $text2 ?>
<br>
<a href="<?php echo $url1 ?>" target=_blank">
<?php
echo $urltext1 ?>
</a><br></font>
</center><br>

<div align="center"><a href="submit.php"><font size="3" face="Verdana, Arial, Helvetica, sans-serif">Click Here To Submit Your Site</font></a></div><br>


submit.php

<?php
require "variables.php";
?>
<html>
<head>
<title><?php echo $title ?> </title>
</head>
<body bgcolor="#006699" text="#FFFFFF" link="#FFFFFF" vlink="#FFFF00" alink="#FF0000">
<center><font size="4">
<?php
echo $text1 ?>
<br>
<?php echo $text2 ?><br>
<a href="<?php echo $url1 ?>" target=_blank">
<?php
echo $urltext1 ?>
</a><br>
<font color="#000000">Submit you site for an Instant Free Listing</font></font>
</center> <FORM METHOD="POST" ACTION="linklist.php">
<table width="500" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><div align="right"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Website
Name:</font></strong> </div></td>
<td> <div align="left">
<INPUT TYPE="text" NAME="wsname" SIZE="30">
</div></td>
</tr>
<tr>
<td width="105"><div align="right"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Website
URL:</font></strong> </div></td>
<td width="195"> <div align="left">no http:// just www.&nbsp;
<INPUT TYPE="text" NAME="wsurl" SIZE="24">
</div></td>
</tr>
<tr>
<td><div align="right"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Description:</font></strong></div></td>
<td><textarea name="description" cols="26"></textarea></td>
</tr>
<tr>
<td><div align="right"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email:</font></strong></div></td>
<td><input type="text" NAME="email"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="center">By Submitting your link you agree to recieve mailings
from us.<br>
<INPUT TYPE="submit" value="Submit!">
</div></div>
</td>
</tr>
</table>
</form>

<center><font size="4">
<?php
echo $text1 ?>
<br>
<?php echo $text2 ?><br>
<a href="<?php echo $url2 ?>" target=_blank">
<?php
echo $urltext2 ?>
</a></font>
</center>
</html>


linklist.php

<?php
require "variables.php";
if (
$wsname == "") {
echo
"You must fill out a site name.";
exit;
}
else
if (
$wsurl == "") {
echo
"You must fill out a site URL.";
exit;
}
else
if (
$description == "") {
echo
"You must put a description.";
exit;
}
else
if (
$email == "") {
echo
"You must enter your email.";
exit;
}
else
$textfile = ("links.txt");
$textfile2 = ("emails.txt");
$fp = fopen($textfile, "a+");
fputs ($fp, "<center><table><tr><td bgcolor=666666 width=500 height=20><font size=2><img src=$imagedir/browser.gif><strong><a href=http://$wsurl target=_blank><font size=4>$wsname</font></a></strong></td></tr><tr><td bgcolor=cccccc width=400><font size=2><font color=000000>$description</font></td></tr></table></center><br><br>\n");
fclose($fp);
$fp = fopen($textfile2, "a+");
fputs ($fp, "$email\n");
fclose($fp);
echo
"<html> <title>$title</title><body bgcolor=006699 text=FFFFFF link=FFFFFF vlink=FFFF00 alink=FF0000><font color=FFFFFF size=3 face=verdana><br><center><br> $text1<br>$text2 <br><a href=\"$url1 \" target=_blank\"> $urltext1</a><br><br>Your site has been successfully added. <a href=$viewpage>Click here</a> to view the Link List page.</center></html>";
?>





variables.php

<?php

// This is the page where you will put your free for fall links on. This is a URL!
$viewpage = "index.php";

// The directory to where you will upload your images. DO NOT END WITH TRAILING SLASH!
$imagedir = "images";

// Website Title
$title = "Title Of Link Page";

// Text 1 for top of pages
$text1 = "Your Text Here";

// Text 2 for top of pages
$text2 = "Second Line of Text";

// Link for Top Of Page
$url1 = "http://www.yahoo.com";

//URL 1 Text
$urltext1 = "Text Of URL";

// Text 1 for bottom of pages
$text3 = "Your Text Here";

// Text 2 for bottom of pages
$text4 = "Second Line of Text";

// Link for Top Of Page
$url2 = "http://www.google.com";

//URL 1 Text
$urltext2 = "Text Of URL";
?>