Archive for the “PHP” Category

PHP is one of my preferred languages. I’ve been using it for almost 10 years, since version 3, I witnessed the release of 4.0… in some words: I have a bit of experience.
I’ve been given by Packt one of the latest PHP books they published, called “PHP 5 e-Commerce Development“. The title sounds very promising and I expected to get an update on the latest technologies and techniques related to e-Commerce out of it.

184719964X

First, the book is following what it says in the title, creating one e-Commerce site from the ground. From authentication to discount codes, everything is covered by the book. It touches also some APIs from payment gateways and Google. So who wants to have a ride in knowing how to build an eCommerce site with PHP 5 should read the book.

If you want just the positive feedback you should stop here. Read the rest of this entry »

Tags: , , , ,

Comments Comments Off on Book review: PHP 5 e-commerce Development by Michael Peacock (Packt Publishing)

Seems that my thumbnail generation tutorial was somehow appreciated by community and somebody posted an article on Zend Developer Zone describing my tutorial as “Advanced Thumbnail Trickery with PHP“.

First, thank you!
Second, I never had so many visitors coming from the same referred. When I saw my statistics I thought that my internal statistics tool went crazy and I checked my google analytics account. Seems that it’s true.

I promise I will elaborate the topic and make a class with it. I will also try to do the same thing with ImageMagick as a visitor suggested.

Tags: , ,

Comments No Comments »

No, it’s not yet another PHP thumbnail generation tutorial! I recently needed a function to:

  • create square thumbnails
  • don’t crop the initial image, but scale it and fill with white background
  • center the thumbnail in the square
  • call the function in a loop to process an entire folder

I found no suitable example and I decided that instead of digging too much it’s better to create my own function and a class for it.

Let’s start with a photo that we need to create a thumbnail for:

Nice kid, huh?

Nice kid, huh?


… and what we want to achieve:
danut_thumb


First step would be the thumbnail generation snippet:
Read the rest of this entry »

Tags: , , , , ,

Comments 27 Comments »

HTML emails….. hmm, not such a good idea.
I am not such a big fan of HTML emails, because I am assaulted by tens of such emails daily, most of them spam ones. And using Microsoft Outlook as email client, it ask me to download (or not) pictures from the server. This could be a bad idea sometimes, because harmful code could be be hidden in image files today (especially GIF files).

I was hired to make a newsletter module for one website and first I started to search how to make this.
The steps are below.

1. Create the HTML page for newsletter.
2. Take care to have the CSS and JS embedded in the HTML file and not outside the file.
3. Replace the relative location of image files with absolute location. E.g:
if you have:
[code lang=”xml”]
Coming Soon
[/code]
you must replace with:
[code lang=”xml”]
Coming Soon
[/code]
Read the rest of this entry »

Comments No Comments »

Nowadays everybody talks about Ajax. To be truly honest, I thought that is something extremely difficult, but it is not.
It is no more than a programming technique (Asynchronous JavaScript and XML) in order to load external info in DIVs, without reloading the whole page (Gmail users probably knows best what I’m talking).

The reason why it was not widely used was that the XMLHttpRequest object is not supported by all browsers. Internet Explorer has his own object called XMLHTTP (as an ActiveX object).
Since all new browsers have this facility, this technology (I think that calling this “Language” is not fair) exploded.

First of all we need to define some javascripts to create the object and handle the request. For this particular example, I used another javascript function to make the request.

<script language="javascript"  type="text/javascript">

        //cross-browser object creation function
        function createRequestObject() {
                var ret;
                //read the user’s browser
                var browser = navigator.appName;
               
                //Internet Explorer has his own object
                if(browser == "Microsoft Internet Explorer"){
                        ret = new ActiveXObject("Microsoft.XMLHTTP");
                }
                else{
                        ret = new XMLHttpRequest();
                }
                return ret;
        }
       
        //create a request object
        var http = createRequestObject();
       
        //you will call this function
        function sendRequest(action) {
                //here’s the catch – call an external page
                http.open(‘get’, ‘req.php?action=’+action);
                //wait until the request is completed
                http.onreadystatechange = handleResponse;
                //finalize the request
                http.send(null);
        }
       
        //function that handle response state changes
        function handleResponse() {
                //fires only the object change to state 4
                if(http.readyState == 4){
                        //get the text from the external procedure
                        var response = http.responseText;
                        var update = new Array();
       
                        //parse the text
                        if(response.indexOf(‘|’ != -1)) {
                                //response should be ‘divID|value"
                                update = response.split(‘|’);
                                //update the element with the divID with the value
                                document.getElementById(update[0]).innerHTML = update[1];
                        }
                }
        }

</script>

After this we need to call sendRequest function from HTML, just like this:

<a href="javascript:sendRequest(‘currentDateTime’)">update time</a>

As you see, this example get the date from an external script (req.php), and return a string with the id of the DIV that will be updated and the value of the DIV, separated by a pipe:
Below is the entire code from req.php. It’s easy, don’t it?

<?
  //test the action from request
  switch($_REQUEST[‘action’]) {
        case ‘currentDateTime’:
                //return the divID and the value separated by a pipe
                echo "currentDateTime|".date("d/m/Y h:i:s");
        break;
  }
?>

In the original page we need a DIV to be filled with external info

<div id="currentDateTime">
</div>

The whole code from HTML file is:

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Ajax demystified</title>
<style type="text/css">
A {
  color: #336699;
  text-decoration: none;
  font-weight: bold;
}

A:hover {
  color: #FF0000;
  font-weight: bold;
}

BODY {
  font-family: Tahoma, Verdana, Arial, sans-serif;
  font-size: 12px;
}

.mark {
  color: #FF0000;
  font-weight: bold;
}
</style>
<script language="javascript"  type="text/javascript">

        //cross-browser object creation function
        function createRequestObject() {
                var ret;
                //read the user’s browser
                var browser = navigator.appName;
               
                //Internet Explorer has his own object
                if(browser == "Microsoft Internet Explorer"){
                        ret = new ActiveXObject("Microsoft.XMLHTTP");
                }
                else{
                        ret = new XMLHttpRequest();
                }
                return ret;
        }
       
        //create a request object
        var http = createRequestObject();
       
        //you will call this function
        function sendRequest(action) {
                //here’s the catch – call an external page
                http.open(‘get’, ‘req.php?action=’+action);
                //wait until the request is completed
                http.onreadystatechange = handleResponse;
                //finalize the request
                http.send(null);
        }
       
        //function that handle response state changes
        function handleResponse() {
                //fires only the object change to state 4
                if(http.readyState == 4){
                        //get the text from the external procedure
                        var response = http.responseText;
                        var update = new Array();
       
                        //parse the text
                        if(response.indexOf(‘|’ != -1)) {
                                //response should be ‘divID|value"
                                update = response.split(‘|’);
                                //update the element with the divID with the value
                                document.getElementById(update[0]).innerHTML = update[1];
                        }
                }
        }

</script>
</head>

<body onload="javascript:sendRequest(‘currentDateTime’)">
        This page shows how Ajax is working. Is is nothing fabulous here.
       

 Click here for
         <a href="javascript:sendRequest(‘currentDateTime’)">update time</a>
          <div id="currentDateTime" class="mark">
          </div>
       
Nothing else but the date and time is updated.
       

        &copy;2006 <a href="mailto:andrei@webxpert.ro">Andrei DANEASA</a>
</body>
</html>

If you want to see this example working and test it, click here.

*This is not intended to be a complete tutorial of Ajax, but it covers the main functionalities. The rest is only programmer’s imagination.

Andrei
http://www.webxpert.ro

Comments No Comments »

This should be a guide for Oracle data manipulation from PHP.

First of all, the connection:

<?php $ora_conn = ora_logon("username@instance","password"); ?>

the sql string (we’ll use select in this example):

<?php $sql = "select col1, col2, col3 from table1 where 1=1"; ?>

the loop that retrieves the data and publish as a HTML table:

<?php
$ora_cur = ora_do($ora_conn, $sql);
if ($ora_cur)
{
        $numCols = ora_numcols($ora_cur);
        //alternative coloring
        $col1 = "#eeeeee";
        $col2 = "#dddddd";
        $toggle = "#eeeeee";
        do {
               $toggle == $col1? $toggle = $col2: $toggle = $col1;
        ?>
        <tr bgcolor="<? echo $toggle;?>">
          <td class="normal"><? echo ora_getcolumn($ora_cur,0);?></td>
          <td class="normal"><? echo ora_getcolumn($ora_cur,1);?></td>
          <td class="normal"><? echo ora_getcolumn($ora_cur,2);?></td>
          <td class="normal"><? echo ora_getcolumn($ora_cur,3);?></td>
        </tr>
        <?
        } while (ora_fetch($ora_cur));
}
?>


Andrei
http://www.webxpert.ro

Comments No Comments »