Geeks On PHP OOP Update

I want to give everybody a quick update on what is going on with Geeks On PHP and what to expect in the near future. As we talked about on our last episode we are putting together a series of podcasts focused on OOP in PHP. Unfortunately due to time constraints, work overload and other things out of our control we have had to push things back a bit. The series is still going to happen and we are excited to have Mitchell Hislop who is a friend of mine and a very talented developer / social media expert joining us for the series.

As soon as we are able to settle on dates to record I will provide updates via Twitter and our Facebook page letting people know what to expect. Thank you for your patience and we look forward to releasing a great series on OOP!

Follow both Stephen & Myself on Twitter and also subscribe via Tunes.

- Nicholas

  • Share/Bookmark

Read Users' Comments (2)

Geeks On PHP Episode 14

This week Stephen and I talk about user defined functions in PHP and why they matter. This is a precursor to our upcoming series on OOP in PHP.

Functions are the key to reusable code and thus should be used as often as it makes sense. Duplicating code is a big no no and should avoided at all costs, this is where functions come into play. Just last week I was working on a script that pulled back two different data sets (as arrays) and needed to do essentially the same thing to these two sets. Rather than duplicate the code twice I just converted my logic to a function and boom I was off an running with no duplication what-so-ever.

function fnXML_Body($adata)
{
  $sxml_body = NULL;
  $i = 0;
  foreach($adata as $arow)
  {
    foreach($arow as $svar=>$sval)
    {
      switch($svar)
      {
        case "slabel":
          $slabel = $sval;
          break;
        case "ivalue":
          $ivalue = $sval;
          break;
      }
      if($i < 2)
        $sslice = "isSliced=\"1\"";
      else
        $sslice = NULL;
    }
    $sxml_body .= "\n";
    $i++;
  }
  return $sxml_body;
}

Using the function above I can call pass a new data array to it as many times as I need to in a script without duplicating any code. The function accepts the data array, does some parsing and returns the XML that I need to generate a 3D Pie Chart.

The call to this function would be as follows:

  $sxml_body = fnXML_Body($adata);

In the next couple weeks we are going to kick off a series on object oriented programming with PHP which will include all the basics as well as the tools needed to build a small web application from start to finish. We will be featuring a special guest for this series, it’s going to be very exciting and something you definitely don’t want to miss.

Follow both Stephen and Myself on Twitter and also subscribe via Tunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

  • Share/Bookmark

Read Users' Comments (1)

Geeks On PHP Episode 13

This week Stephen and I talk about strings and how they are accessed, manipulated and used in PHP.

We kick off the discussion talking about some differences between echo() and printf(). Below is an example of using both functions to populate a text field on a web form. We will start off with using echo():


.. input type="text" name="foo" id="foo"
[Inside PHP Block]
if(!empty($dbresult['foo']))
echo ' value="'.$dbresult['foo'].'" />';
else
echo ' value="" />';
?>

Here is the same end-result using printf():


... input type="text" name="foo" id="foo"
[Inside PHP Block]
printf(" value=\"%s\" ", !empty($dbresult['foo']) ? $aResults['foo'] : "");
?>

I tried to make the “argument” during the show that printf() allows for a lot more flexibility as well as resulting in slightly more compact code. Notice the usage of PHP’s ternary operator in the printf() example. It allows for a great deal of flexibility and power directly in-line w/ your output statement.

Regular Expressions were also a big topic of discussion this week and as promised here are some examples of commonly used RegEx’s in web application development:


$pattern_date = '#(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d#';
$pattern_email = '#^[a-zA-Z._]+@[a-zA-Z_]+?\.[a-zA-Z]{2,4}$#';
$pattern_email2 = '#^\w+([\.%-]\w+)*@\w+([\.-]\w+)*(\.\w{2,})+$#';
$pattern_phone = '#^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$#';
$pattern_time = '#(1[0-2]|[1-9]):([0-5][0-9])#';
$pattern_url = '#(http|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(/\S*)?$#';
$pattern_alphanum = "#^[a-zA-Z0-9\s]+$#";

To test matching on these patterns you can simply use the preg_match() function:

preg_match($pattern, $string, $matches);
if(!empty($matches))
    print_r($matches);

A couple of my favorite resources for regular expressions are:
The Online Regular Expression Testing Tool
The Regular Expression Library

Regular Expressions provide so much power and versatility when it comes to pattern matching and replacements that they just can’t be ignored. If you have questions about any of the above please let us know.

Follow both Stephen and Myself on Twitter and also subscribe via Tunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

  • Share/Bookmark

Read Users' Comments (1)

Geeks On PHP Episode 12

In this episode Stephen and I talk about database connections, retrieving results and making use of those results. Both MySQL and PostgreSQL are discussed in this episode so whichever open source database you are using we got you covered.

Here is a link to Stephen’s database class for MySQL. He discusses it’s usefulness and some of it’s functions in the show.

Here is some sample code for getting connected to and pulling out results from a PostgreSQL database. The database include file should be separate from your application code so that you can easily change connection parameters in one location without touching your application.

/* Example of PostgreSQL database include file
	named db.inc */

$host = "hostname";
$port = "port";
$dbname = "database";
$dbuser = "username";
$dbpass = "dbpass";

$hdb = pg_connect("host=$host port=$port dbname=$dbname
	user=$dbuser password=$dbpass");

/* How to include and open connection in script */
require_once("db.inc");

/* How to query a PostgreSQL database */
$res = pg_query($hdb, "SELECT name, email FROM table");

/* Close database connection */
pg_close($hdb);

/* Create associative array */
while($ares = pg_fetch_assoc($res))
{
	// do stuff
}

/* Create object */
while($ores = pg_fetch_object($res))
{
	// do stuff
}

Check both Stephen and Myself out on Twitter and also subscribe to us on iTunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

  • Share/Bookmark

Read Users' Comments (2)

Geeks On PHP Episode 11

In this episode we talk about everything from image manipulation, generating charts & graphs, classes and frameworks and even home invasions.

The charting libraries I discuss are Fusion Charts and pChart, both are very good libraries and have great documentation on how to get started.

The following is a code snippet that I wrote which takes in an associative array, parses it out for a data label ($a[0]) and a data value ($a[1]) and generates a Fusion compatible XML file.

function fnGenerateXML($scaption, $sxaxis, $syaxis, $adata)
{
  if(empty($adata))
    die("Empty data set passed to function.");

  $sdata = "\n";

  foreach($adata as $arow)
    $sdata .= " \n";

  $sdata .= "";

  $sfile_xml = "./chart.xml";
  file_put_contents($sfile_xml, $sdata);
  return $sfile_xml;
}

The function is called in the following fashion:

$sfile_xml = fnGenerateXML("Caption", "X Lbl", "Y Lbl", $a);

The Fusion Chart is generated using the following method call:

echo renderChartHTML("./Column3D.swf", "$sfile_xml", "",
      "ID", 650, 650, false);

Here is some sample code I wrote to generate a graph using pChart:

// Dataset definition
$pData = new pData;
$pData->AddPoint($a1, "s1");
$pData->AddPoint($a2, "s2");
$pData->AddPoint($alabels, "lbl");
$pData->AddSerie("s1");
$pData->AddSerie("s2"); 

$pData->SetSerieName("s1lbl", "s1");
$pData->SetSerieName("s2lbl", "s2");
$pData->SetYAxisName("Ylbl");

$pData->SetAbsciseLabelSerie("lbl");

// Init graph
$pGraph = new pChart(800, 340);
$pGraph->setFontProperties("./tahoma.ttf",8);
$pGraph->setGraphArea(50,30,680,200);
$pGraph->drawFilledRoundedRectangle(7,7,693,240,5,240,240,240);
$pGraph->drawRoundedRectangle(5,5,695,240,5,260,230,230);
$pGraph->drawGraphArea(255,255,255,TRUE);
$pGraph->drawScale($pData->GetData(),
   $pData->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);
$pGraph->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$pGraph->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the graph
$pGraph->drawStackedBarGraph($pData->GetData(),
   $pData->GetDataDescription(),TRUE);

// Finish
$pGraph->drawLegend(500,40,$pData->GetDataDescription(),255,255,255);
$pGraph->drawTitle(225,22,"Title",0,0,0);
$pGraph->Stroke();

As you can see pChart requires a bit more setup to get going but both are great libraries for getting the job done. Another free option for charting/graphing that we didn’t discuss on the podcast is the Google Chart API.

Check both Stephen and Myself out on Twitter and also subscribe to us on iTunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

  • Share/Bookmark

Read Users' Comments (1)

Geeks On PHP Episode 10

We are happy to bring you episode 10 of the Geeks On PHP Podcast!  This week we talk about API programming, specific API’s that we have worked with, some great development tips and even alcohol.  This episode has it all.

The Authorize.net API comes up a bit in discussion and I mention the article I wrote a couple of days ago regarding some changes to online debt card processing as it pertains to the Authorize.net API.

We invite you to follow both Stephen and I on Twitter and also subscribe to us on iTunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

  • Share/Bookmark

Read Users' Comments (0)

New MasterCard & Discover Processing Requirements

MasterCard and Discover are modifying their rules concerning processing of debit, prepaid and gift cards. MasterCard’s rule changes went into effect 5/1/2010 and Discover’s rule changes went into effect on 4/16/2010. Merchants using Authorize.Net have until 6/30/2011 to comply with the new standards but since I am compliance hound I wanted to make these changes immediately.

Since I was reading up on all of the documentation and re-working my API code accordingly I figured I would share the changes with everyone in hopes of saving people a bit of time when they sit down to modify their own code.

So what’s new?
– Balance response transactions
– Partial authorization transactions
– Authorization reversals

So what does all that mean?
Transactions made on a gift/debt/etc card can now be split among multiple cards using partial authorization and the splitTenderId value. This value “links” the authorization request to subsequent transactions.

The new API request fields to pay attention to are ['x_allow_partial_Auth'] which is either true or false and ['x_split_tender_id'] which gets set to the original authorization request value.

The new relay response reply fields are as follows:
['x_account_number'] = last 4 digits of card
['x_card_type'] = string value indicating the type of card used
['x_split_tender_id'] = original authorization value
['x_prepaid_requested_amount'] = amount requested in original auto
['x_prepaid_balance_on_card'] = balance on the debit/prepaid card

The obvious things to note here (aside from the tender id) are the card balance and the requested amount. With these two values you can easily determine if the transaction can go through or if a subsequent transaction (w/ the sane tender id) has been requested.

New response codes to take note of are the following:
Reason: 295 Code: 4 — Amount was only partially approved
Reason: 296 Code: 3 — The splitTenderId is invalid.
Reason: 297 Code: 3 — A trans ID and a Split Tender ID can’t both be used in a single transaction request.
Reason: 298 Code: 3 — The splitTenderId has been completed (released or voided) and can not be associated with new transactions.

Here is what a sample relay repines looks like: (Taken from official documentation)

Hopefully this summary helps identify the changes that need to be made and why. All of this information can be found in the official documentation which is available at: http://www.authorize.net/support/pafaqs/

- Nicholas

  • Share/Bookmark

Read Users' Comments (1)

Geeks On PHP – Episode 9

After a long wait, Geeks on PHP – Episode 9 is finally out!  As I mentioned in a previous post we are now on a weekly recording schedule and will be releasing a new episode every Sunday evening unless otherwise specified.

In this exciting episode we discuss lots of information and things to think about *before* you start building your database or writing your application.  We also touch on SQL statements, database schema design and how they relate to your PHP applications.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Also, subscribe to us on iTunes and leave us your comments: [ Launch iTunes ]

Follow us on Twitter :
Nick : http://www.twitter.com/niczak
Stephen : http://www.twitter.com/darthweef

- Nicholas

  • Share/Bookmark

Read Users' Comments (0)

Geeks On PHP Is Alive!

Geeks On PHPHey all, I just want to give everybody an update on the Geeks On PHP podcast.  Stephen and I have arranged our schedules as such that we will be on a regular weekly recording schedule beginning NEXT WEEK.  This is very exciting news for us and for our listeners.  We apologize for the gaps between episodes in the past and the long gap between our last recording and the upcoming one.

Episode 9 will be focused on discussing different ways to utilize data from a database (agnostic discussion) with PHP.  We are set to record on Wednesday 4/28 and the show should be available on iTunes by the weekend.

We really want to thank all of our listeners for the feedback and support we received during our down time.  It means a lot to both of us and we look forward to getting back on track and bringing you the best PHP podcast the internet has to offer.

Sincerely,

- Nicholas Kreidberg

  • Share/Bookmark

Read Users' Comments (1)

Geeks on PHP — Update

geeksOnPHP

Hey all, I know the release schedule for the Geeks on PHP podcast has been erratic and unpredictable lately. We are currently in the process of revamping the format of the show including me taking over as host and the possibility of adding someone else as co-host. Stephen’s responsibilities with the New Brew Thursday podcast have increased exponentially which is the underlying reason behind the changes going on with Geeks on PHP.

I have a couple people in mind for co-hosts but would love to get feedback from our readers/listeners about what you would like to get out of the show. If any of you are interested in doing a guest spot please get in touch with me either by Email or on Twitter.

I will keep everybody updated as things progress but for now I just wanted to make people aware of what is going on and also that the show is not dead.

Thanks for listening and please don’t hesitate to comment, email or otherwise — your involvement is highly encouraged!

Sincerely,

- Nicholas Kreidberg

  • Share/Bookmark

Read Users' Comments (0)

 Page 1 of 3  1  2  3 »