OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 
PHP Code Snippets: Formatting a Field in Smarty
By admin ( 2006-12-26  7508 views. )
I have slid by avoiding using field formatting in Smarty templates for some time. However, I just ran across a case where a dollar value HAD to be formatted correctly for display.

It turns out it is pretty easy to use formatting. In the actual template you have code that looks like the template code below. We have a field called bid that is being formatted according to the number_format function.

In the smarty/libs/plugins directory we create the file modifier_number_format.php. This tells smarty (when it loads) to pull in the modifier (formatting) function as defined in the file. That way our template can address it at will.

As you recall, all we are doing is calling the php number_format function with a bunch of arguments. It helps to remember that Smarty really is a bunch of PHP code under the covers.

So, in the specific case we are formatting a number ($number), with 2 decimals (2), a decimal point (".") and a thousands separator (",").

Using this as a model you can move on to some really interesting formatting using all kinds of PHP tools at you disposal.
 

 
PHP Code Snippets: PHP HTTP Mail
By admin ( 2006-11-10  7139 views. )
Most mail readers are HTML compliant these days. I had a client that needed to send HTML. It turns out a number of sites have it wrong. Here is some code that works.

The code:
- determines the correct eol for the os in question
- defines a mime boundary just using a random hash - just needs to be something specific
- sets the message headers. of note, you MUST have a FROM or TO tag in the headers or this won't work.
- since we are sending HTML we put the HTML into a MIME object. the mailer needs to know how to delineate the MIME components of your messsage. hence the
- define your addressee, subject line and messag as always
- the 2nd trick is to wrap your message around a MIME descriptor as shown
- send the message away
 

 
PHP Code Snippets: Keep your options open with FTP file uploads using PHP
By PHPBuilder ( 2006-05-12  7177 views. )
With PHP, there's always more than one way to accomplish a particular task. Take file upload, for example. Sure, you can do it the traditional way, using HTTP file upload and transferring the file directly to a location on your Web server's disk. Or you can do it the more exotic way, and use the FTP protocol to upload in a two-step process: from your local disk to a Web server, and then to an FTP server.

PHP supports both FTP and HTTP upload methods natively, leaving it to you to make the best decision based on the design requirements of your application. Transferring a file using PHP's FTP functions is almost the same as doing it using a traditional FTP client--as you'll see even the function names are similar to standard FTP commands.

Reams have already been written about HTTP file upload, which is why this brief tutorial focuses on FTP-based file uploads instead (in the example that follows, though, you'll see both in action). Note that this tutorial assumes that you have a working PHP/Apache installation, with both HTTP file upload and FTP functions active.

Create a simple HTML form that asks the user for critical parameters--FTP server access information, the server directory to upload to, and the full path and name of the file to be uploaded. Here's a sample (Listing A) of what this form might look like.

Here, the input type=file... element renders as a file selection dialog box, from which the user can select the file to be uploaded. The form enctype=... element then forces the form data to be encoded as a multi-part submission, which makes it easier for PHP to identify the file component of the submission.

Create the PHP upload processor

Once the form has been submitted to the Web server, the next (and final) step is to use PHP's FTP functions to transfer it to the target FTP server, using the access credentials supplied by the user. Here's the script (upload.php) that does all the work (Listing B).

This might appear complicated, but it's actually fairly straightforward. Here's what's happening:

Once the form has been submitted, the credentials supplied by the user in the various form input fields are extracted into regular PHP variables. Information about the uploaded file also now becomes available through PHP's special $_FILES array.

The $_FILESarray is made up of multiple sub-arrays, one for each uploaded file. The keys of each sub-array hold information about the size, MIME type, original name and temporary name of the corresponding uploaded file. This information is used by the move_uploaded_file() function to transfer the file from the system's temporary directory to the working directory. Remember to alter the value of $workDir to reflect a valid file path on your system.

Tip: It's usually a good idea to check the type and size of the uploaded file before calling move_uploaded_file(), to ensure that it satisfies any upload constraints the application might have. For example, if the application requires files to be in ZIP format for upload, this can be checked at this stage (from the MIME type) to ensure that files in other formats are filtered out.

The ftp_connect() and ftp_login() functions are used to initiate a connection to the named FTP host, and log in to it using the supplied credentials.

Assuming a successful login, the ftp_put() function is used to upload the file from the working directory to the remote directory specified by the user and rename it to its original form. Note the addition of the special FTP_BINARY argument to ftp_put(), to specify that the file be transferred in binary (not ASCII) mode. Depending on the result code returned by the ftp_put() function, an error or success message is displayed to the user.

The ftp_close() function is used to end the FTP session, and the unlink() function is used to clean up by deleting the local copy of the file that was created in step (2).

Simple, isn't it? Go on, try it out for yourself and see!

http://builder.com.com/5100-6371_14-6069684.html?part=rss&subj=bldr
 

 
PHP Code Snippets: How-to: Convert a RSS feed to JSON in PHP
By admin ( 2006-05-12  7149 views. )
Here are a quick set of directions to build your own RSS to JSON proxy using only PHP. You need to download and install two libraries, MagpieRSS for RSS and JSON-PHP for JSON. With these two libraries you can easily create a simple proxy to suit your needs. Here is an example of the code.

Things to think about:
- You could easily restrict the RSS feeds to a predefined array or a database table.
- You should carefully cache the RSS and JSON content.
- There might be nefarious issues with importing Javascript from untrusted sources, so be careful.

http://www.engrowe.com/?p=67
 

 
PHP Code Snippets: Expose your development to more possibilities by using PHP with SQLite
By Melonfire ( 2006-05-05  7140 views. )
One of the innovations in PHP 5.x is the inclusion of the SQLite database engine. SQLite is a fully self-contained, portable file-based database engine that can be used for most SQL operations without incurring the overhead of client-server communication. This SQLite API is activated by default in PHP 5.x, which means that you can get up and running with SQLite instantly.

This tutorial will show you how to use PHP to interact with anSQLite database, by introducing you to the important methods supported by the SQLite API, and providing a simple script template for you to use in your development. It assumes that you have a working Apache and PHP installation.

Editor's Note: The two code listings mentioned in this document are available in a more easily manageable text form by downloading this file.

It is not strictly necessary to have the interactive SQLite program installed on your system; however, for simplicity in creating the initial set of tables for this tutorial, you should download and install this program. Then, create an example table for your SQL queries, by creating an empty text file, executing the binary with the file name as the parameter and entering the following commands (Listing A) at the interactive command prompt.

Once the table has been created, proceed to build a script template using PHP SQLite methods.

There are four simple steps to follow when executing an SQL query with the PHP SQLite extension in Listing B.

Begin by initializing a database handle with a call to the sqlite_open() function. The path and file name of the database (remember, SQLite is file-based, not server-based like MySQL) is passed to the function as an argument.

Create the SQL query string, and execute it with the sqlite_query() function.
The result object of these methods will be different depending on the query type and whether or not it was successful. Successful SELECT queries return a result object; successful INSERT/UPDATE/DELETE queries return a resource identifier; and unsuccessful queries return false. The sqlite_error_string() and sqlite_last_error() methods can be used to catch errors and display appropriate error messages.

For SELECT queries, the result object may be processed further to extract data from it. Used in a loop, the sqlite_fetch_array() function retrieves each record as a PHP array. You access individual fields of the record by calling the appropriate key of the array.

End the session with a call to the sqlite_close() function.

Hopefully, this script template will save you some time when you next sit down to write SQLite connection/interaction routines in PHP. Happy coding!

http://builder.com.com/5100-6371_14-6066383.html?part=rss&subj=bldr
 

 
Item(s) 6 to 10 of 57.   Go to page: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting

Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005