Categories
PHP Wordpress

Worpress: useful codex commands

<?php get_cat_name( $cat_id ) ?> Get the category name if you have the id of it

<?php get_category_link( $category_id ); ?> Get the category permalink

Categories
PHP Wordpress

WordPress: produce new templates that can be used for new pages

Put them on your Themes directory, any php file that is there and includes the following header will be included in the drop-down menu for templates to choose from while creating a page:

<?php/*Template Name: Custom Feed*/

Categories
JavaScript PHP Wordpress

WordPress: Adding the version of jquery that comes with the installation

wp_enqueue_script('jquery');
Categories
PHP

Where is my php.ini?

So you setup a page with phpinfo() on it, and the php.ini path indicated by it does not actually contain a php.ini file? Kind of a weird bug I run across. After searching for all my php.ini files in my linux box (find / -name php.ini) and not being able to find the appropiate one (I keep making changes on them and not see the changes take place after the server restarts) I decided to move my php.ini-dist again to the location specified on the php.ini path of the phpinfo() page. Success !! It fixed the problem. Speculating on this, the problem may have been originated by running the ./configuration option with the prefix and a new path optio

Categories
PHP

Setting the user agent on a php based bot

The source says it all:

http://forums.digitalpoint.com/showthread.php?s=54a41b4d013e9c78bbb65201b35f70c7&t=13408

Categories
PHP

Files not being uploaded on a PHP server

The solution may be something really simple you overlooked: make sure your form contains the enctype=”multipart/form-data” part.

Categories
mysql PHP

Extension not found error when trying to run the DB package for php Pear

This only means you did not configure that specific database to be compiled with php. For example, if you get that error when trying to connect to a mysql database, make sure that when you run ./configure (when installing php) you included the -with–mysql option.

Categories
PHP

Example on how to use php smarty foreach control structure

<p><!–StartFragment –>                       {foreach from=$advertiser_categories item=category}<br />

</p>

<li><input id=”category_id_{$category}” type=”checkbox” name=”category_id[]” value=”{$category}” />{$category}</li>

<p>                       <br />

{/foreach} Keywords: notes, code</p>

Keywords: notes, code

Categories
PHP

PHP: To turn off only warnings and still show errors

Use the following line on your code:

error_reporting(E_ERROR);

You can also turn it off on your php.ini file (not recommended):

error_reporting = E_ERROR

Categories
PHP

PHP: Do not use fopen() to read XML files into xml_parse(

was using the following function to get a xml url from the web, create a handle,  read the content, and then feed it to xml_parse():

if (!($fp = fopen($xml_file, ‘r’))) {              die(’Cannot open XML data file: ‘.$xml_file);

return false;

}

$bytes_to_parse = 1024;

while ($data = fread($fp, $bytes_to_parse)) {            $parse = xml_parse($this->xml, $data, feof($fp));

if (!$parse) {

die(sprintf(”XML error: %s at line %d”,                    xml_error_string(xml_get_error_code($this->xml)),

xml_get_current_line_number($this->xml)));                        xml_parser_free($this->xml);

}        }

The problem in doing this, is that the 1024K bytes that gets read in every loop of the while may cut out content.

For example, I was capturing the following xml file:

http://www.nytimes.com/services/xml/rss/nyt/podcasts/techtalk.xml

And this is what I was getting:

2006-09-14 15:37:15,699 DEBUG> /dump/include/class.xmlparser.php:40 parse  – This is $data:

29:49

http://podcasts.nytimes.com/podcasts/2006/09/13/14techtalk.mp3       This week: Tech news, new products from Apple, LCD versus Plasma, and reader questions.

Wed, 13 Sep 2006 05:25:00 EDT

Tom Holcomb and J.D. Biersdorfer of The New York Times              2006-09-14 14:32:24,424 DEBUG> /dump/include/class.xmlparser.php:38 parse  – This is $data: uration>25:51

This week: Tech news, online file storage and reader questions.

Wed, 06 Sep 2006 04:43:00 EDT

Tom Holcomb and J.D. Biersdorfer of The New York Times

27:41

This week: Tech news, online movie downloads, and reader questions.

Wed, 30 Aug 2006 04:00:00 EDT

2006-09-14 14:32:24,426 DEBUG> /dump/include/class.xmlparser.php:38 parse  – This is $data: closure length=”26584293″ url=”http://podcasts.nytimes.com/podcasts/2006/08/30/31techtalk.mp3″ type=”audio/mpeg”/>

Notice how the xml file was being cut in several parts, and the beggining of some tags was being included in one section, while the closing of the same tag was included on the next.

Switching to this code solved the problem:

$file_handle = file_get_contents($_POST[’rss_url’]);                         $myFile = new XMLParser($file_handle);

$arr = $myFile->ParseChannelFromXML();

function parse($xml_file)    {

$parse = xml_parse($this->xml, $xml_file);            if (!$parse) {

die(sprintf(”XML error: %s at line %d”,                    xml_error_string(xml_get_error_code($this->xml)),

xml_get_current_line_number($this->xml)));                        xml_parser_free($this->xml

);            }

return true;

}

Now the entire XML file is read once, instead of in several smaller parts, and therefore no cut off happens.