Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

Archive for the 'php' Category

« Previous Entries
Next Entries »

PHP Generated SQL

Wednesday, May 21st, 2008

If you find yourself laboriously building SQL queries by typing each field = 'value' statement... think again.

Imagine this:

PLAIN TEXT
PHP:
  1. $sql_query = mysql_query("select * from table");
  2.  
  3. $insert_elsewhere_sql = "INSERT INTO other_table SET ";
  4.  
  5. while($s = mysql_fetch_assoc($sql_query)){
  6.     foreach($s as $k=>$v){
  7.     $insert_elsewhere_sql .= "$k = '$v', ";
  8. }
  9.  
  10. $insert_elsewhere_sql = substr($insert_elsewhere_sql, 0, -2);

This will generate a valid SQL insert statement with all of your field, value pairs set up.

Time saver or what?

Posted in mysql, php | No Comments »

Purashop : SEO Services

Wednesday, May 14th, 2008

I am right in the middle of a big SEO overhaul for purashop who have been good clients of mine for a while now.

My initial project for them was a fast checkout system. Getting rid of the standard osCommerce checkout procedure and rolling it all up into a single page which is loads faster, easier to use and much more customer friendly.

They have recently asked me to help them out with their search engine optimisation. The first thing I have done is install my bespoke SEO URLs system. This includes product, category and manufacturer SEO URLs. It even includes SEO page urls, no .html?page= for these guys, it's a full static looking URLs system and runs with even less server load than the original oscommerce.

This is a major contrast with the Edmonds Commerce approach to SEO URLs compared with other systems out there. I have seen people who are using systems which dynamically preg_replace every single URL after it has already been generated, adding a huge amount of server load.

The extra icing on the cake though is the SEO URLs search system combined with a search logging system and a popular searches page. This page is excellent as it is not only a nice resource for customers, but is incredibly search engine friendly. As the content is dictated by the searches performed on the site, the page constantly keeps itself up to date with the latest trends and will constantly optimise their site for the most relevant and active topics of the moment.

I am continuing to tweak and improve their SEO package and have high hopes for a marked improvement in traffic in the not too distant future.

Posted in php | No Comments »

Flash and PHP Charts and Graphs

Wednesday, May 14th, 2008

It is entirely possible and easy enough to create all kinds of charts and graphs using only PHP along with the GD library.

These days PHP is generally compiled with the GD library enabled so it is highly likely that you already have access to it. It is a powerful system, unfortunately often only used for creating thumbnail images.

For building management information systems with real time generated graphs there are some excellent classes available which will help you display information in a graphical and therefore easier to understand manner.

Checkout these great free graph classes:

Class: Google Graph
Class: Bar Graph generator
Class: Graph Drawing Class 2

However, sometime the graphics produced by GD simply are not flash enough for your application.

When you want something really flashy on your web site then you really can't go wrong using flash.

There are a few flash graph packages you can purchase online for not too much cash. However if you prefer to keep it all open source then I highly recommend checking out this open source flash graph class.

Open Flash Chart project

Further Reading:

noteplog.com
bivald.com
blogs.sun.com
www.webappers.com
blogulate.com
www.linewbie.com
hotware.wordpress.com
wordpress.designpraxis.at
www.stat.columbia.edu

Posted in flash, gd, graphs, php | 3 Comments »

Plesk Web Fusion PHP Limits

Friday, April 25th, 2008

While recently migrating a client from a dedicated server which was proving a little too expensive over to one of the great value Linux Virtual Private Servers offered by Web Fusion, I found myself frustrated with the 2mb upload limit apparent in the control panel side of things.

So I went and edited the php.ini to allow for a more generous upload limit. Restarted the server but to no effect.

After a closer inspection I realised that the Plesk control panel actually had its own separate php.ini file. Edited this file and it works.

Another Problem Solved :-)

Posted in hosting, php, plesk | No Comments »

MySQL Dump Partial Restore - Split MySQL Dump into Tables

Wednesday, April 9th, 2008

MySQL dumps are often used as a quick and easy way of backing up an entire MySQL database in one go. However, they are only really designed to restore whole databases as well. So what if you only want to restore certain tables?

I have written this nice little PHP script which will take your dump file and split it into lots of little sql files - one for each table. You can then restore which ever tables you want.

Simply create a php file in the same directory as your dump file. Name it something easy to remember. Then go to it in your web browser, input the name of the dump file and click go.

As this is a fairly massive task (depending on the size of your dump file) you may find it works better on your local test server with some generous php_ini settings for max_execution_time etc.

PLAIN TEXT
PHP:
  1. <?php
  2. /*MySQL Dump Split to Tables
  3. * script supplied by Edmonds Commerce
  4. * www.edmondscommerce.co.uk
  5. * if you like it, please link back with a do-follow
  6. */
  7. echo '<h1>MySQL Dump Split to Tables</h1>';
  8. set_time_limit(600);
  9. if(!isset($_GET['dump_file'])){
  10.     echo '<form><b>Dump File Path:</b> <input name="dump_file"> <input type="submit" value="go"></form>';
  11. }else{
  12.     $handle = @fopen($_GET['dump_file'], "r");
  13.     if ($handle) {
  14.         while (!feof($handle)) {
  15.             $line = fgets($handle);
  16.             if(strstr($line, 'CREATE TABLE')){
  17.                 if(isset($out)){
  18.                     fclose($out);
  19.                     unset($out);
  20.                 }
  21.                 preg_match('%CREATE TABLE `(.+?)`%', $line, $matches);
  22.                 $table_name = $matches[1];
  23.                 $out = fopen($table_name . '.sql', 'w');
  24.                 fwrite($out, $line . "\n");
  25.             }else{
  26.                 if(isset($out)){
  27.                     fwrite($out, $line . "\n");
  28.                 }
  29.             }   
  30.         }
  31.         fclose($out);
  32.         fclose($handle);
  33.     }   
  34. }
  35. echo '<h1>Done</h1>'
  36. ?>

Posted in mysql, php | 3 Comments »

Advanced PHP Debug Function

Thursday, March 20th, 2008

I have taken the debug function about as far as I can think of. This function will now take a variable and display it all in a nice easy to read format. It will also give the name of the variable being examined. It works with html, using htmlentites. It preserves the tab layout formatting for arrays making it easy to understand:

Heres How to Use It:

PLAIN TEXT
PHP:
  1. //For example, displaying $_POST information
  2. dbug($_POST);
  3.  
  4. //If using inside a function
  5. function something($blah){
  6. dbug($blah, get_defined_vars());
  7. }

Note that if called within the scope of a function (for example) you must give get_defined_vars().

And here are the neccessary functions

PLAIN TEXT
PHP:
  1. //DEbug Functions
  2.  
  3. //convert tabs to spaces
  4. function tab2space($text){
  5.     $text = str_replace('  ', '&nbsp;&nbsp;', $text);
  6.     $text = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $text);
  7.     return $text;
  8. }
  9.  
  10. //return name of variable passed by reference
  11. function vname(&$var, $scope=false){
  12.     if($scope) $vals = $scope;
  13.     else      $vals = $GLOBALS;
  14.     $old = $var;
  15.     $new = 'THISONE';
  16.     $var = $new;
  17.     $vname = false;
  18.     foreach($vals as $key => $val) {
  19.         if($val !=='var'){
  20.             if($val === $new) $vname = $key;
  21.         }
  22.     }
  23.     $var = $old;
  24.     return $vname;
  25.   }
  26.  
  27.  
  28. function dbug(&$item, $scope=false){
  29.     $vname = vname(&$item,$scope);
  30.     echo '<hr><h3>Debug Info: $' . $vname  . '</h3>' . tab2space(nl2br(htmlentities(var_export($item, true)))) . '<hr>';   
  31. }

Posted in php | No Comments »

PHP Save Images Using cURL

Tuesday, March 18th, 2008

Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images by calling imagecreatefromjpeg($img) where $img is a url for an external image does not work.

This is a shame as this technique is pretty easy..

PLAIN TEXT
PHP:
  1. $remote_img = 'http://www.somwhere.com/images/image.jpg';
  2. $img = imagecreatefromjpeg($remote_img);
  3. $path = 'images/';
  4. imagejpeg($img, $path);

However I was tearing my hair out trying to get a product feed integration system to work on a host that has disabled the allow_url_fopen mechanism which meant the above wouldnt work.

Thankfully cURL was still working. So here is the function I made for grabbing and saving an image using cURL instead:

PLAIN TEXT
PHP:
  1. //Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
  2. function save_image($img,$fullpath){
  3.     $ch = curl_init ($img);
  4.     curl_setopt($ch, CURLOPT_HEADER, 0);
  5.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6.     curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  7.     $rawdata=curl_exec($ch);
  8.     curl_close ($ch);
  9.     if(file_exists($fullpath)){
  10.         unlink($fullpath);
  11.     }
  12.     $fp = fopen($fullpath,'x');
  13.     fwrite($fp, $rawdata);
  14.     fclose($fp);
  15. }

Another Problem Solved :-)

Posted in curl, php | No Comments »

Debugging PHP Scripts

Monday, March 17th, 2008

When coding PHP, or more frequently when trying to customise or fix someone elses code, it can sometimes be tricky to figure out exactly what is going wrong.

In this kind of situation, it is often neccessary to go through the flow of the script and view the contents of the various strings and arrays at that stage of the script.

I have written this small function to help in this process:

PLAIN TEXT
PHP:
  1. function dbug($item){
  2.     echo '<hr><h3>Debug Info</h3><pre>' . (var_export($item, true)) . '</pre><hr>'; 
  3. }

This script will take any item and print out detailed information for you in a nicely formatted and easy to read way.

For an example when you might decide to use this functionality, you could check the contents of your $_POST

PLAIN TEXT
PHP:
  1. <?php
  2. /*very top of script */
  3. dbug($_POST);

Posted in debugging, php | No Comments »

MySQL Add Column if Not Exists - PHP Function

Thursday, March 13th, 2008

This is a nice little function which I struggled to find elsewhere on the web.

As discussed elsewhere this is not the kind of thing that should be included in a public facing script, but for administration tools etc its pretty handy:

PLAIN TEXT
PHP:
  1. function add_column_if_not_exist($db, $column, $column_attr = "VARCHAR( 255 ) NULL" ){
  2.     $exists = false;
  3.     $columns = mysql_query("show columns from $db");
  4.     while($c = mysql_fetch_assoc($columns)){
  5.         if($c['Field'] == $column){
  6.             $exists = true
  7.             break;
  8.         }
  9.     }      
  10.     if(!$exists){
  11.         mysql_query("ALTER TABLE `$db` ADD `$column`  $column_attr");
  12.     }
  13. }

This function grabs the column information for the table, then it loops through the info looking for the column. If it finds the column then it acknowledges that it exists and ceases the loop.

After this stage, the function checks to see if exists is true or not, and if it is not true then it adds the column with the attributes defined when calling the function.

The default attributes are for a VARCHAR field up to 255 characters in length and NULL enabled.

Posted in mysql, php | 2 Comments »

PHP and Zip Files

Thursday, March 13th, 2008

There aren't many things you can't do with PHP. One of the things you can do is deal with zip files.

PHP has a built in class for dealing with zip files which allows you to create them, unpack them, add and delete things from them and generally use them within your scripts.

Recently on a spidering / product feed integration job that I have been working on I needed to grab a load of image zip files and unpack them all into a folder called 'images/' (surprisingly enough)

Here is how I did it:

PLAIN TEXT
PHP:
  1. function unpack_zips($directory, $destination = 'images/'){
  2.     $files = dir_list($directory);
  3.     //print_r($files);
  4.     $zips = array();
  5.     foreach($files as $k=>$file){
  6.         if(stristr($file, '.zip')){
  7.             $zips[] = $directory . $file;
  8.         }
  9.     }
  10.     print_r($zips);
  11.     $zip = new ZipArchive;
  12.     foreach($zips as $z){
  13.         if ($zip->open($z) === TRUE) {
  14.             $zip->extractTo($directory . $destination);
  15.             $zip->close();
  16.             //echo "<h3>$z OK</h3>";
  17.         } else {
  18.             echo"<h3 style=\"color: red;\">$z Failed</h3>";
  19.             bottom();
  20.         }   
  21.     }
  22. }
  23.  
  24. function dir_list($directory){
  25.     echo $directory;
  26.     if ($handle = opendir($directory)) {
  27.         while (false !== ($file = readdir($handle))) {
  28.             if($file != '.' && $file != '..'){
  29.                 $return[] = $file;
  30.             }
  31.         }
  32.         closedir($handle);
  33.         return $return;
  34.     }else{   
  35.         return false;
  36.     }
  37. }

There are two functions. The first function is the one that deals with the Zip files. The second function is called by the first function and simply lists all files within a specified directory.

These two functions combined allowed me to find all zip files in a particular folder and then unpack them all into a destination folder.

Related Resources

http://www.phpclasses.org/browse/package/945.html

http://www.phpit.net/article/creating-zip-tar-archives-dynamically-php/

Posted in php, zip | No Comments »

« Previous Entries
Next Entries »
  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • MySQL Add Column if Not Exists - PHP Function
    • Favourite Firefox Extensions
    • Purashop : SEO Services
    • Freelance osCommerce UK
    • Meta Title Tag and SEO
    • osCommerce Output Queries Debug : Store Speed Optimisation
    • Faster File Browsing with Tabs for Ubuntu
    • osCommerce Password Reset Using phpMyAdmin
    • Web Developer Toolbar for Firefox
    • Ultimate osCommerce Checkout - Fast and Friendly

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design