You Are Here Home > PHP

PHP

How Not To Write Code

I’m working on a website and it’s absolutely awful, almost all the design choices are bad so I will compile a list here as I encounter them:

1 – Don’t have fields in your database like “extra1″, “extra2″ or “extra3″ you probably need to refactor and rethink your design…

2 – Don’t have a global object and access and manipulate it from deep inside your code, here for example there is a $tpl variable which – wrongly – is called $class_tpl and it’s global, the authors are calling a pager function to create an array for rendering a pager and after they call they manipulate the $class_tpl a bit by adding paging data to it and you would think that was it, but guess what? In their pager function they again access $class_tpl and manipulate a bit more!!!

3 – Don’t ever, ever, ever, ever query your database from within your templates, never…

4 – Don’t die or exit out of functions, return error messages…

5 – Don’t output out of function either, no “echo $blah;” in a function, they should return the text string rather than printing it…

6 – Don’t ever, ever, ever repeat yourself, if you are too lazy to write a function or rethink that portion of your code, then you suck as a programmer… To be honest, I do suck sometimes, but I try my very best :)

How Not To Write Code
Comments (0)   Filed under: C Programming,General,PHP,Web Development   Posted by: Hamid

Importing GeoWorldMap Database Into MySQL Using PHP

First download the database from:

http://www.geobytes.com/freeservices.htm

Then, create a folder in your server where you can access it via a browser and upload the files in there, then save this script as import.php and upload it into the same folder and then point your browser to this PHP script you just uploaded. Note that you must edit the script and add your own database name/username/password to it before uploading.

<?php
 
   $db_info = array(
      'host' => 'localhost',
      'name' => 'EDIT ME',
      'user' => 'EDIT ME',
      'pass' => 'EDIT ME'
   );
 
   error_reporting(E_ALL);
   ob_end_clean();
 
   function out($string) {
      echo $string;
      flush();
   }
 
   out("<pre>");
 
   function map_fields($table, $row, $index) {
      if ($index == 0)
         return NULL;
      $retval = array();
      switch ($table) {
         case 'country':
            $retval = array(
               'id' => $row[0],
               'name' => $row[1]
            );
            break;
         case 'region':
            $retval = array(
               'id' => $row[0],
               'country_id' => $row[1],
               'name' => $row[2]
            );
            break;
         case 'city':
            $retval = array(
               'id' => $row[0],
               'country_id' => $row[1],
               'region_id' => $row[2],
               'name' => $row[3]
            );
            break;
         default:
            exit("Fatal: Called map_fields with unsupported table: $table\n\n");
      }
      return $retval;
   }
 
   function do_mysql_query($query) {
      if (!mysql_query($query))
         exit("\n\nFatal: do_mysql_query failed with MySQL error: " .mysql_error() ."\n---------------------\nAnd query: $query\n\n");
   }
 
   function create_table($table) {
      out("Creating table: $table...");
      do_mysql_query("
         DROP TABLE IF EXISTS codehead_$table
      ");
      $query = "";
      switch ($table) {
         case 'country':
            $query = "
               CREATE TABLE codehead_country (
                  id INT NOT NULL PRIMARY KEY,
                  name VARCHAR(100),
                  index (name)
               )
            ";
            break;
         case 'region':
            $query = "
               CREATE TABLE codehead_region (
                  id INT NOT NULL PRIMARY KEY,
                  country_id INT NOT NULL,
                  name VARCHAR(100),
                  index (name),
                  index (country_id, name)
               )
            ";
            break;
         case 'city':
            $query = "
               CREATE TABLE codehead_city (
                  id INT NOT NULL PRIMARY KEY,
                  country_id INT NOT NULL,
                  region_id INT NOT NULL,
                  name VARCHAR(100),
                  index (name),
                  index (region_id, name),
                  index (country_id, region_id, name)
               )
            ";
            break;
         default:
            exit("Fatal: Called create_table with unsupported table: $table\n\n");
      }
      do_mysql_query($query);
      out("Done!\n");
   }
 
   function empty_table($table) {
      do_mysql_query("DELETE FROM codehead_$table");
   }
 
   function insert_row_into_table($table, $row) {
      $query = "";
      switch ($table) {
         case 'country':
            $id = intval($row['id']);
            $name = mysql_real_escape_string($row['name']);
            $query = "
               INSERT INTO codehead_country VALUES (
                  $id,
                  '$name'
               )
            ";
            break;
         case 'region':
            $id = intval($row['id']);
            $country_id = intval($row['country_id']);
            $name = mysql_real_escape_string($row['name']);
            $query = "
               INSERT INTO codehead_region VALUES (
                  $id,
                  $country_id,
                  '$name'
               )
            ";
            break;
         case 'city':
            $id = intval($row['id']);
            $country_id = intval($row['country_id']);
            $region_id = intval($row['region_id']);
            $name = mysql_real_escape_string($row['name']);
            $query = "
               INSERT INTO codehead_city VALUES (
                  $id,
                  $country_id,
                  $region_id,
                  '$name'
               )
            ";
            break;
         default:
            exit("Fatal: Called insert_row_into_db with unsupported table: $table\n\n");
      }
      do_mysql_query($query);
   }
 
   $base_dir = dirname(__FILE__);
 
   $tables = array(
      $base_dir .'/Countries.txt' => 'country',
      $base_dir .'/Regions.txt' => 'region',
      $base_dir .'/Cities.txt' => 'city'
   );
 
   $db = mysql_connect($db_info['host'], $db_info['user'], $db_info['pass']);
   if (!$db)
      exit("Fatal: Couldn't connect to MySQL...\n\n");
   if (!mysql_selectdb($db_info['name']))
      exit("Fatal: Couldn't select database...\n\n");
 
   foreach ($tables as $file => $table) {
      if (($fp = fopen($file, 'r')) !== false) {
         create_table($table);
         out("Inserting rows into table: $table");
         $index = 0;
         while (($data = fgetcsv($fp, 10000)) !== false) {
            if ($index == 0) {
               ++$index;
               continue;
            }
            $row = map_fields($table, $data, $index);
            insert_row_into_table($table, $row);
            ++$index;
            out(".");
         }
         fclose($fp);
         out("Done!\n");
      } else
         exit("Fatal: Couldn't open file: $file\n\n");
   }
 
   out("All done!\n\n");
   mysql_close($db);
 
?>
Importing GeoWorldMap Database Into MySQL Using PHP
Comments (1)   Filed under: PHP,Web Development   Posted by: Hamid
Older Posts »