www.

Blog Tags

Lab Eleven Blogs

A short description about your blog
Tags >> PHP

Joomla 1.5 Custom Breadcrumbs

Posted by: gbluma

Tagged in: Programming , PHP , Joomla , Components

Joomla seems to have everything figured out — Breadcrumb support is no exception.

Recently we were writing some custom component for a client and needed to handle the breadcrumbs in a special way that Joomla couldn't manage automatically.

As a simplified example, imagine we redirect a user to a component that isn't attached to a menu. We would expect the breadcrumbs to simply output "Home" and provide a link back to the index of the site.


Joomla 1.5 Database Functions

Posted by: twmeier

Tagged in: Programming , PHP , Joomla

Sometimes it may be hard to know how to best use Joomla's database class. Here are just a few of the most used database functions.

In Joomla 1.5, include the following line of code inside your php:

<?php
     $database  =& JFactory::getDBO();
?>

Use the $loadObject function when you want to retrieve one row from the database. For example:

$database->setQuery("SELECT * FROM jos_comprofiler WHERE id=125");
$userDetails = $database->loadObject();

You can now access the values as follows:

$userDetails->firstname
$userDetails->lastname
.....

If you want to retrieve more than one row from the database, use the loabObjectList() function. For example:

$database->setQuery("SELECT * FROM jos_comprofiler WHERE id<125");
$userDetails = $database->loadObjectList();

You will now have an array of objects. You can access the values as follows:

$userDetails[0]->firstname
$userDetails[0]->lastname
$userDetails[1]->firstname
$userDetails[1]->lastname
.....

If you want to retrieve a single value from the database, use the loadResult() function. For example:

$database->setQuery("SELECT COUNT(*) FROM jos_comprofiler");
$count= $database->loadResult();

You now have a regular php variable. In this case $count.

There are other Joomla database functions, but these three are the most common.


Changing a New Line to a Break

Posted by: twmeier

Tagged in: PHP

There is a great php function takes a variable and changes the  new lines (\n) into breaks (<br/>).

For example, a user submits a form on your website and used a textarea to submit a couple of paragraphs. You then retrieve this data from the database and echo it out in html. You will notice that it is now just one big paragraph.

To fix this problem, you the function: nl2br

Here is how you might use this:

<?php
     $paragraphs = nl2br($paragraphs);
     echo $paragraphs;
?>


XMLWriter in PHP

Posted by: gbluma

Tagged in: XML , Programming , PHP

Every now and then I find myself needing to export a set of objects to XML. I usually find a quick way to write a dumb function to do it, but this is usually not the most elegant solution and rarely does it scale up to handle more complex data structures. The other option is to serialize the data into xml, but usually these methods suffer from a large performance overhead and should only be used if you can tweak the serializer to work only the right pieces of data.

I have since added XMLWriter to my toolset. Which, oddly enough, is built right into PHP (as of version 5.1.2) and based on the widely popular libxml engine. I find it to be very quick, both in performance and in ease of programming. Best of all, the code makes more sense than any of any of my own homemade tools.

Here's an example where demonstrate how to export a list of products to xml:


Parsing an XML string retrieved using AJAX

Posted by: twmeier

Tagged in: XML , Programming , PHP , Javascript , Ajax

While working on an AJAX function this week, I found the need to parse an XML string using JavaScript and PHP. The following JavaScript was used to retrieve an XML string from a PHP file:

function someJavascriptFunction() {
      xmlHttp=GetXmlHttpObject();    
      url = "/components/com_component/ajax/ajax.php?task=someTask";    
      url += "&cc_number="+document.getElementById('cc_number').value;
      xmlHttp.open("GET",url,true);    
      xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4)  {       
           xmlDoc=loadXMLString(xmlHttp.responseText);
           var response = xmlDoc.getElementsByTagName("response")[0].childNodes[0].nodeValue;
           document.getElementById("someID").innerHTML = response;
     }
     xmlHttp.send(null);
}

function GetXmlHttpObject()    {
      var xmlHttp=null;
      try {
           // Firefox, Opera 8.0+, Safari
           xmlHttp=new XMLHttpRequest();
      }
      catch (e) {
           //Internet Explorer
           try {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
           }
           catch (e) {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
           }
      }
      return xmlHttp;
}

As you can see from the code above, the XML field I was interested in was the "response" field. The XML string that was created and passed using xmlHttp.responseText from the PHP file looked similar to this:

<authorizationResponse>
      <message>Some Message</message>
      <response>Some Message</response>
....
</authorizationResponse>

Not only did I need to parse the XML from the  responseText in javascript, but I also need to parse it inside the PHP function that the JavaScript function called. The XML above was retrieved from a payment gateway in the PHP function using CURL and naned $xmlString.

The only field I needed in the PHP file was the "message" field, and to retrieve it, (or any other field), I did the following in the PHP file:

$xml = new SimpleXMLElement($xmlString);
$message = $xml->authorizationResponse->message;

Don't forget you still need to echo out the entire XML string in your PHP file order to be used by your JavaScript function:

echo $xmlString;


Writing a Joomla 1.5 Content Plugin

Posted by: gbluma

Tagged in: Programming , Plugins , PHP , Joomla

This article will show you how to build a quick plugin to replace some text in an article before it get's displayed to the user.

Intro

Lately I've been writing a number of content plugins for Joomla. They're pretty simple, but basically it allows you to transform the content at a number of points in Joomla's process of editing, saving, and displaying of that content. In theory you could use it to do anything but it's common to replace text and/or add special content into the articles.

I used to frown on these types of plugins because it's better to just get the data right in the first place. But sometimes controlling the input just isn't possible or perhaps you need to keep the article in a certain format but display it in another format.


Latest Blogs

Fix Bad Credit With These Helpful Tips
Feb 26th 2013 by: Administrator

Will iPhone Apps Run On iPad?
Jan 29th 2010 by: Trevor M.

How to create a marquee text for an iPhone app
Jan 21st 2010 by: Trevor M.

How to Submit Additional Images in Virtuemart
Aug 7th 2009 by: Trevor M.

Using an Image as a Submit Button in HTML
Jul 31st 2009 by: Trevor M.

Read all blogs