Posts Tagged ‘ PHP

Autocomplete in Codeigniter 2.1.0 and Eclipse pdt

I have recently started working with Codeigniter for a few side projects and to help me stay up to date with php. I am using eclipse pdt as my ide. In order to get auto complete to work it is quite simple. (Note, these steps are the exact same for Aptana. Just adding a user library…)

  • Download the 2.1.0 files to an easy to find location as you will be referencing it for multiple projects.
  • Modify /system/core/controller.php and /system/core/model.php with the code changes listed below
  • In your actual Codeigniter project, add a new user library (/preferences) and point it to the 2.1.0 files and folders
  • Now you should have auto complete for all your CI methods!

    Code Changes:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    //Put this code right before: class CI_Model { ...
    /**
      * @var CI_Config
      */
     var $config;
     /**
      * @var CI_DB_active_record
      */
     var $db;
     /**
      * @var CI_Email
      */
     var $email;
     /**
      * @var CI_Form_validation
      */
     var $form_validation;
     /**
      * @var CI_Input
      */
     var $input;
     /**
      * @var CI_Loader
      */
     var $load;
     /**
      * @var CI_Router
      */
     var $router;
     /**
      * @var CI_Session
      */
     var $session;
     /**
      * @var CI_Table
      */
     var $table;
     /**
      * @var CI_Unit_test
      */
     var $unit;
     /**
      * @var CI_URI
      */
     var $uri;
     /**
      * @var CI_Pagination
      */
     var $pagination;

    (The code is from this post here)

    Printing multi line XML while maintaining the formatting in PHP

    A co-worker of mine introduced me to this tip for maintaining the formatting of multi line information in PHP; in this case it is XML. It uses heredoc. The syntax is basically three <<< and then the name of the type of variable...

    Here is a code snippet of it in use:

    <?php
     
    echo <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <someNode>
    	{$massiveAmountOfXML}
    </someNode>
    XML;
     
    ?>

    HTTP Post in PHP without a Form

    For one reason or another I had trouble getting this to work so I thought I would post my code snippet that I used here.

    <?php
    <?php
    ini_set('display_errors', E_ALL);
     
    class PostWithOutAForm {
     
    	var $_returned_message;
    	var $data = array();
     
    	public function __construct() {	}
     
    	public function callPong($msg) {
    		$data['message'] = $msg;
     
    		$post_str = '';
    		foreach($data as $key=>$val) {
    			$post_str .= $key.'='.urlencode($val).'&';
    		}
    		$post_str = substr($post_str, 0, -1);
     
    		// Initialize cURL
    		$ch = curl_init('http://localhost:8888/TargetOfPost.php');
    		curl_setopt($ch, CURLOPT_POST,				count($data));
    		curl_setopt($ch, CURLOPT_POSTFIELDS, 		$post_str);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 	TRUE);
     
    		// Execute...
    		$output = curl_exec($ch);
     
    		 if (!$output) {
    			$output = curl_error($ch);
    		}
    		curl_close($ch);
     
    		// Display the result
    		return $output;
    	}
    }
    ?>

    Create a png with php and as3

    For a project I was on awhile ago, I needed to decorate a character with different items of clothing. When the user was done ‘dressing’ their character, they would press a button and the whole character would shrink down so it would fit as an ornament on a tree. In order to not confuse the issue, I am going to only show the code that enables you to take a movieclip and create a png from it. In order to do this, you will need to include the Flex SDK as a part of your flash build (for the Base64Encoder class) and you need to download as3corelib. To include the Flex SDK, in your Flash preferences add this line: (Mac OSX)

    //VERSION will prob be 3.5, 4 etc...
    /Applications/Adobe Flash Builder 4/sdks/flex_sdk_VERSION/frameworks/projects/framework/src

    Code after the jump…

      CreatePNG (44.4 KiB, 173 hits)

    Read more

    Avoid Twitter API-IP Call Limit

    Twitter limits the amount of calls you can make to its API and from one IP to a few hundred an hour. So that means if you have something on the page that is showing your latest tweets, it will only work for the first few hundred people. We have a really high traffic site and needed a more robust solution.

    What we did is create a php file that loads the XML from the twitter user timeline (example) and saves it on our server. We setup a chron job to call it every minute. The Flash movie loads the XML file that our php file creates for us ensuring that we will never go over the limit but always have the most up to date content from Twitter displayed on our website.

    If you have any questions or want an example of the source, post in the comments. Running Example.

    Resources Used:

  • The LoadXML and LoadURL files from here.
  • The AxisScroller from here.
  • Tweenlite
  • Flex and Flash with Zend

    In order for this tutorial to work, you need to make sure that your environment is setup by following the instructions located here. I have included an fla (cs3) file in the /Flash folder of the Flex project archive. You would not normally do this for a production project but this is just meant to be a simple example and since they both use the same php files it didnt make much sense to split this article up into two separate articles.

    I am currently using Flex Builder 3.0.2 and Flash CS4 but I saved the fla as a cs3 file for those that don’t have it yet.

    Code and instructions after the jump Read more

    Setup WAMP and the Zend Framework for use with Flash and Flex

    There are a few tutorials out there on the net about using Zend with Flash/Flex but most seem to be overly complicated and many don’t work even after following the instructions. (I am hoping that mine will. :) ) The next few articles cover a VERY simple implementation that you can build on for use with future projects and I hope it can save people time when it comes to beginning to explore this new method. This post will deal with setting up your environment to be able to use Zend with Flash/Flex.

    Code and instructions after the jump. Read more

    Secure way to pass a value to a Form field with PHP

    Recently I worked on a project where I had to pass a value from one php page to another. It was very simple and I had it working but didnt think about the security around this functionality as I am still fairly new to php. The biggest issue is to make sure someone cant inject code into the field and mess up your site. Most examples I saw didn’t include this security check, so here you go.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <?php
    	//grab email that is passed in from a page
    	if (isset ($_GET["email"])) {
    	        //htmlentities removes quote characters and html characters from the value passed in
    		$email = htmlentities($_GET["email"], ENT_QUOTES);
    	} else {
    		$email = '';
    	}
    ?>

    And here is the code you use to send the value to the page:

    <form name="myForm" action="submitForm.php?email=+document.getElementById('emailField').value);">
    ...
    <input type="text" name="emailField" id="emailField" value="email goes here">
    <input type="image" src="images/submit.jpg">
    </form>

    Thanks to PlanetKodiak for the help with this.

     

    Switch to our mobile site