PRACTICAL PHP BY EXAMPLE Jan Leth-Kjaer
Scope and subjects • This is not a PHP upcode class. • This is not a Zend class. • This is not an Power specific class. • This is not about installation of Apache. • This is not about work management of Pase, PHP or related areas. • This is not a PHP security class.
So what is it then? • I presume you all know some type of programming and SQL. • I presume you know HTML at some level. • I presume you can or know a bit of PHP or likewise programming languages. • We will running through a number of practial PHP examples and see the frontend as well as the backend = code.
Data management • MySQL – the database. • Retrieve some data. • Show the data. • Give user functionality to enter/update data. • Update the data in database.
Data management – go get it <?php session_start(); require("access.php"); // Variabler til mysql hentes $resultat = mysql_query("SELECT * FROM customer WHERE customernumber = '$number'"); while ($row = mysql_fetch_array($resultat)) { $CompanyName = $row['CompanyName']; $Name = $row['Name']; $email = $row['email']; $Address1 = $row['Address1']; $Address2 = $row['Address2']; .... ?> ...... <form action='index.php?page1=CustomerValidate.php method='post' enctype='multipart/form-data'> .... <tr> <td width="193"><font face="Verdana" size="2"><b>Companyname:<font color="#FF0000">*</font></b></font></td> <td colspan="2"><input type="text" name="T1" size="50" tabindex="1" value="<?php echo $CompanyName ; ?>" ></td> </tr>
Data management – validate/update $CompanyName = $_POST[‘CompanyName ']; ..... // Validate... if(empty( $CompanyName ) ) { $error1 = '<br> Companyname is empty '; } .... if (preg_match("/^([w|.|-|_]+)@([w||-|_]+).([w|.|-|_]+)$/i", $email-address)) { } else { $error4 = "<br> Email is not correctt!"; } .... Echo $error1 . $error4; ..... mysql_query("UPDATE Customer SET CompanyName = '$CompanyName '..... Where....
File management • File management on server. <table border="0" width="50%" cellspacing="10" cellpadding="20"> <?php $path = « foldername"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || substr($file, 0, 10) != 'Nyhedsbrev') continue; $aar = substr($file, 11, 4); $month_number = substr($file, 16, 2); $month_name = date( 'F', mktime(0, 0, 0, $month_number) ); echo "<tr><td width='32' align='left' valign='top'><font face='Verdana' size='2'>"; echo "<font face='Verdana' size='2'>" . $aar . "</td><td><font face='Verdana' size='2'>" . $month_name . "</td><td>"; echo "<font face='Verdana' size='2'><a href='/ foldername /$file'>$file</a></td></tr>"; } // Close closedir($dir_handle); ?> </table>
PDF creation • PHP plugin. <?php require('fpdf.php'); http://www.fpdf.org // Get some data – to use.. $resultat1 = mysql_query("SELECT * from meetings where active=‘NEXT'") or die(mysql_error()); while ($row = mysql_fetch_array($resultat1)) { $firma[$p] = substr($row['CompanyName'], 0, 44); $pdf->AddPage(P,A4); // Create some frame – including some text $pdf->SetFont('Arial','B', ‘$CompanyName’); $pdf->SetTextColor(0,0,0); $pdf->SetXY(10,10); $pdf->SetDrawColor(240,240,240); $pdf->Cell(90,54,"$pnavn[$a]",1,1,C);
PayPal • Free to have - not free to use. • You need an account. Hidden API buildup. https://cms.paypal.com/us/cgi-bin/?cmd=_render- • content&content_ID=developer/howto_html_landing • Create buttons – onsite/offsite. • Mostly European creditcards. • Transaction-information related to e.g. invoice number. • PayPal – developer site. (https://www.x.com/developers/paypal)
PayPal – standard call <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="FLFFSFSFSDFL"> <input type="hidden" name="on0" value="Faktura: <table> <tr><td nowrap="nowrap"> <font face="Verdana" size="2">Invoice number</font> <font face="Verdana" size="1"> (Price 5000€.)</font></td></tr><tr><td><input type="text" name="os0" maxlength="200" value=" <?php echo $workfaknr; ?> " disabled></td></tr> <tr><td>&nbsp;</td></tr> </table> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/da_DK/i/scr/pixel.gif" width="1" height="1"> </form>
PayPal – build up <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="PAYPAL ACCOUNT NAME"> <input type="hidden" name="item_name" value=« Invoice total"> <table> <tr><td><input type="hidden" name="on0" value="Faktura"><font face="Verdana" size="2">Pick Invoice</font></td></tr><tr><td><select name="os0"> <?php while ($row = mysql_fetch_array($result1)) { ... Get invoices... } <tr><td>&nbsp;</td></tr> </table> <?php Echo "<option value='" . $worknr . "'>" . $worknr . ": " . money_format('%.2n', $Total) . "</option>"; $harray[$worki] = "<input type='hidden' name='option_select" . $worki . "' value='" . $worknr . "'><input type='hidden' name='option_amount" . $worki . "' value='" . sprintf("%01.2f", $Ptotal) . "'>"; $worki++; } ?> <input type="hidden" name="option_index" value="0"> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/da_DK/i/scr/pixel.gif" width="1" height="1"></p> </form>
SMS API • External function to call. • You may see different setup. • Parameter call – cross site • API object creation. • Call back – with delayed information. • Other functions as API – e.g. Google.
SMS API $smsnumber = $wmobil[$i]; $bruger = $_SESSION['navn']; $nu = date("YmdHis"); $work = $i; $smsid = $bruger . $nu . $work; // Build Parameters (RETRY=TRUE som default) $strParameters = "?username=USERNAME!!!!" . "&password=CODE!!!" . "&dialcode=45" . "&recipient=$smsnumber" . "&text=" . urlencode(utf8_encode("$sms-text")) . "&from=Common+CZ" . "&callback_url=" . urlencode("http://www.gs-gruppen.org/GS-SMSs.php?smsid=$smsid&password=RETURNPASSWORD"); //Echo $strParameters; //Send sms and save output in $strOutput $strOutput = file_get_contents("http://api.textreactor.com/legacy/sms.php" . $strParameters);
Thank you for your attention! Questions

Practical PHP by example Jan Leth-Kjaer

  • 1.
    PRACTICAL PHP BYEXAMPLE Jan Leth-Kjaer
  • 2.
    Scope and subjects •This is not a PHP upcode class. • This is not a Zend class. • This is not an Power specific class. • This is not about installation of Apache. • This is not about work management of Pase, PHP or related areas. • This is not a PHP security class.
  • 3.
    So what isit then? • I presume you all know some type of programming and SQL. • I presume you know HTML at some level. • I presume you can or know a bit of PHP or likewise programming languages. • We will running through a number of practial PHP examples and see the frontend as well as the backend = code.
  • 4.
    Data management • MySQL – the database. • Retrieve some data. • Show the data. • Give user functionality to enter/update data. • Update the data in database.
  • 5.
    Data management –go get it <?php session_start(); require("access.php"); // Variabler til mysql hentes $resultat = mysql_query("SELECT * FROM customer WHERE customernumber = '$number'"); while ($row = mysql_fetch_array($resultat)) { $CompanyName = $row['CompanyName']; $Name = $row['Name']; $email = $row['email']; $Address1 = $row['Address1']; $Address2 = $row['Address2']; .... ?> ...... <form action='index.php?page1=CustomerValidate.php method='post' enctype='multipart/form-data'> .... <tr> <td width="193"><font face="Verdana" size="2"><b>Companyname:<font color="#FF0000">*</font></b></font></td> <td colspan="2"><input type="text" name="T1" size="50" tabindex="1" value="<?php echo $CompanyName ; ?>" ></td> </tr>
  • 6.
    Data management –validate/update $CompanyName = $_POST[‘CompanyName ']; ..... // Validate... if(empty( $CompanyName ) ) { $error1 = '<br> Companyname is empty '; } .... if (preg_match("/^([w|.|-|_]+)@([w||-|_]+).([w|.|-|_]+)$/i", $email-address)) { } else { $error4 = "<br> Email is not correctt!"; } .... Echo $error1 . $error4; ..... mysql_query("UPDATE Customer SET CompanyName = '$CompanyName '..... Where....
  • 7.
    File management • Filemanagement on server. <table border="0" width="50%" cellspacing="10" cellpadding="20"> <?php $path = « foldername"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || substr($file, 0, 10) != 'Nyhedsbrev') continue; $aar = substr($file, 11, 4); $month_number = substr($file, 16, 2); $month_name = date( 'F', mktime(0, 0, 0, $month_number) ); echo "<tr><td width='32' align='left' valign='top'><font face='Verdana' size='2'>"; echo "<font face='Verdana' size='2'>" . $aar . "</td><td><font face='Verdana' size='2'>" . $month_name . "</td><td>"; echo "<font face='Verdana' size='2'><a href='/ foldername /$file'>$file</a></td></tr>"; } // Close closedir($dir_handle); ?> </table>
  • 8.
    PDF creation • PHPplugin. <?php require('fpdf.php'); http://www.fpdf.org // Get some data – to use.. $resultat1 = mysql_query("SELECT * from meetings where active=‘NEXT'") or die(mysql_error()); while ($row = mysql_fetch_array($resultat1)) { $firma[$p] = substr($row['CompanyName'], 0, 44); $pdf->AddPage(P,A4); // Create some frame – including some text $pdf->SetFont('Arial','B', ‘$CompanyName’); $pdf->SetTextColor(0,0,0); $pdf->SetXY(10,10); $pdf->SetDrawColor(240,240,240); $pdf->Cell(90,54,"$pnavn[$a]",1,1,C);
  • 9.
    PayPal • Free tohave - not free to use. • You need an account. Hidden API buildup. https://cms.paypal.com/us/cgi-bin/?cmd=_render- • content&content_ID=developer/howto_html_landing • Create buttons – onsite/offsite. • Mostly European creditcards. • Transaction-information related to e.g. invoice number. • PayPal – developer site. (https://www.x.com/developers/paypal)
  • 10.
    PayPal – standardcall <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="FLFFSFSFSDFL"> <input type="hidden" name="on0" value="Faktura: <table> <tr><td nowrap="nowrap"> <font face="Verdana" size="2">Invoice number</font> <font face="Verdana" size="1"> (Price 5000€.)</font></td></tr><tr><td><input type="text" name="os0" maxlength="200" value=" <?php echo $workfaknr; ?> " disabled></td></tr> <tr><td>&nbsp;</td></tr> </table> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/da_DK/i/scr/pixel.gif" width="1" height="1"> </form>
  • 11.
    PayPal – buildup <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="PAYPAL ACCOUNT NAME"> <input type="hidden" name="item_name" value=« Invoice total"> <table> <tr><td><input type="hidden" name="on0" value="Faktura"><font face="Verdana" size="2">Pick Invoice</font></td></tr><tr><td><select name="os0"> <?php while ($row = mysql_fetch_array($result1)) { ... Get invoices... } <tr><td>&nbsp;</td></tr> </table> <?php Echo "<option value='" . $worknr . "'>" . $worknr . ": " . money_format('%.2n', $Total) . "</option>"; $harray[$worki] = "<input type='hidden' name='option_select" . $worki . "' value='" . $worknr . "'><input type='hidden' name='option_amount" . $worki . "' value='" . sprintf("%01.2f", $Ptotal) . "'>"; $worki++; } ?> <input type="hidden" name="option_index" value="0"> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/da_DK/i/scr/pixel.gif" width="1" height="1"></p> </form>
  • 12.
    SMS API • External function to call. • You may see different setup. • Parameter call – cross site • API object creation. • Call back – with delayed information. • Other functions as API – e.g. Google.
  • 13.
    SMS API $smsnumber =$wmobil[$i]; $bruger = $_SESSION['navn']; $nu = date("YmdHis"); $work = $i; $smsid = $bruger . $nu . $work; // Build Parameters (RETRY=TRUE som default) $strParameters = "?username=USERNAME!!!!" . "&password=CODE!!!" . "&dialcode=45" . "&recipient=$smsnumber" . "&text=" . urlencode(utf8_encode("$sms-text")) . "&from=Common+CZ" . "&callback_url=" . urlencode("http://www.gs-gruppen.org/GS-SMSs.php?smsid=$smsid&password=RETURNPASSWORD"); //Echo $strParameters; //Send sms and save output in $strOutput $strOutput = file_get_contents("http://api.textreactor.com/legacy/sms.php" . $strParameters);
  • 14.
    Thank you foryour attention! Questions