7
Apr/10
0

Win PHP Challenge 2010 – Beginning

I-am registered now in Win PHP Challenge for 2010. I’am trying to make my best on PHP programming in Windows environment. PHP + mSql.

I need to decide between 2 projects I have in mind:

1. phpMyCat – this will be an online solution for store of CD/DVD/Computer content: folder and filenames, with/without description

2. tinyURL – long URL redirect  with short URL – built on Windows platform, with advanced options, like “Accounts”, “Share” , etc.

I will decide until 9 April.

17
Nov/09
0

New mobile site, made on my new mobileCMS:

m.siteconcept.ro

7
Sep/09
0

I’ve just finished an small project for one company: it’s about “Registru de casa”, some paperwork that all romanian companies needs to mantain on daily basis. I’ve used PHP, Ajax-JQuery with MySQL database. Fast and clean. See here: www.tantar.info/registrucasa .

Here, i-ve build an new PHP Database class to help me:

1. First, how to use


$cDB=new tDatabase;
$cDB->dbConnect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD,DB_DATABASE);

// now, to retrieve all tables from database:

$array_of_tables=$cDB->dbFetchTables();

echo("<pre>");print_r($array_of_tables);echo("</pre>");
<?php

/**
 * @author Cristian Tantar <www.tantar.info> <contact@tantar.info>
 * @copyright 2009
 */

class tDatabase extends tDebug {

  var $connection=false;

  function tDatabase() {
  	// do nothing

  }

  function dbConnect($host, $name, $pass, $db){
  // conect to database
  $connection = @mysql_connect("$host","$name", "$pass");
  if(!$connection){
  	$this->PrintDebug(mysql_errno().":".mysql_error()."<BR> LINE ".__LINE__.", FILE: ".__FILE__,1);
  	die();
  }
  mysql_select_db("$db", $connection);

  }//ends the connection function

 function dbClose(){
   // close database connection
  mysql_close($this->connection);

  }//ends the close function

  function dbFetchRow($query){
  // fetch one row with data from query
  $rows = mysql_fetch_row($this->dbQuery($query));
  return $rows;
  }

  function dbFetchArray($query){
  // fetch array with data from query
  $array = mysql_fetch_array($this->dbQuery($query));
  return $array;
  }

function dbFetchArrayResult($result){
  // fetch array from resultset ( this result can be: $result=$db->query($sql) , or other)
  $array = mysql_fetch_array($result);
  return $array;
  }

  function dbFetchField($query,$field){
  // return only specified field value, or n/a if empty
  $row=$this->dbFetchArray($query);
  if($row[$field]){
  	$field = $row[$field];
  }else{
$field="n/a";
  }
  return $field;
  }

function dbFetchFieldResult($result,$field){
  // return specified field, but from result, instead of sql query
  $row=$this->dbFetchArrayResult($result);
  if($row[$field]){
  	$field = $row[$field];
  }else{
$field="n/a";
  }
  return $field;
  }

  function dbFetchNum($query){
  	/* Get number of rows in query */
  $num = mysql_num_rows($this->dbQuery($query));
  return $num;
  }

  function dbFetchNumResult($result){
  	/* Get number of rows in result */
  $num = mysql_num_rows($result);
  return $num;
  }

function dbFetchTables(){
// return array of tables in database
  	$result=mysql_list_tables(DB_DATABASE) or die (mysql_error());
  	$tables=array();
  	while($tbl=mysql_fetch_array($result)){
  		$tables[]=$tbl[0];
  	}
  	return $tables;
  }

  function dbLastId(){
// return last id generated from previous operation
  	return mysql_insert_id();
  }

  function dbFetchResultsQuery($sql){
  // return array of results from query: $array[0]=row  0, $array[1]=row 1
  // you can retreive data: $array[0][$field]
    $resultArr=array();
$res=$this->dbQuery($sql);
while($data=$this->dbFetchArrayResult($res)){
$resultArr[]=$data;
}
return $resultArr;
  }
function dbQuery($sql){
// return resultset of sql query
  $resultSet=mysql_ query($sql) or die(mysql_error());
return $result;
}

 }//ends the class

?>

<?php

/**
 * simple debug class found on internet. not my property.
 */

class tDebug
{
 var $logfile;
 var $debug;

/*==============================================================================
Constructor
Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
==============================================================================*/
 function hDebug($debug_yesno=0,$logfile_path="")
 {
 $this->debug=$debug_yesno;
 $this->printdebug=1;

 //Report only user generated error messages
 error_reporting(0);

 if($this->debug>=1)
 {
 $this->logfile=$logfile_path;
 set_error_handler(array($this,"ErrorHandler"));
 }
 else
 {
 restore_error_handler();
 }
 }

/*==============================================================================
Following function logs an error message
==============================================================================*/
 function ErrorHandler($errno,$errstr,$errfile,$errline)
 {
 $errtime=date("H:i:s - d/m/y");
 $error_message="An error occured on ".$errtime."\n";
 $error_message .="Details are as follows\n";
 $error_message .="Error Number : $errno\n";
 $error_message .=$errstr."\nOccured in ".$errfile."\n";
 $error_message .="On Line $errline \n\n";
 $error_message .=str_repeat("-",50);
 $error_message .="\n";
 $fp=fopen($this->logfile,"a+");
 fwrite($fp,$error_message);
 fclose($fp);
 }

/*==============================================================================
Following function prints debug messages on the screen
==============================================================================*/
 function PrintDebug($message,$yesno=0)
 {
 if($yesno>0)
 {
 $message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
 print $message;
 }
 }
}

?>
Filed under: scripts
4
Aug/09
7

Verry simple PHP CMS

I have some clients who wants simple sites, only to show basic info about them. So, i-ve implemented an simple system to be faster on page building. This are the steps:

1. create basic structure of the site (3 dirs)

+content
+images
+includes

2. we have 4 files to write:

a. index.php


<?php

/**
 * @author  Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */
require("config.php");

$parameters=array();

if(MAIN_DIR!=""){
 $parameters=explode("/",str_replace(MAIN_DIR."/","",$_SERVER['REQUEST_URI']));
}else{
 $parameters=explode("/",$_SERVER['REQUEST_URI']);
 if($parameters[0]==""){$temp=array_shift($parameters);}
}

 if($parameters[0]==""){$parameters[0]="engine";$parameters[1]="";}
 if(!isset($parameters[1])){$parameters[1]="";}

 if(file_exists($parameters[0].".php")){
 require($parameters[0].".php");
 }else{
 require("engine.php");
 }

?>

b. config.php

<?php

/**
 * @author  Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */

define('APP_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);

 if(isset($_SERVER['SCRIPT_NAME']))
 {
 $main_dir = rtrim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
 }
 else
 {
 die('[config.php] MAIN_DIR error, set manual');
 }

 define('MAIN_DIR', $main_dir);

?>

Here, in config.php, if need, we can include the connection to MySQL or other configs.

c. engine.php

<?php

/**
 * @author Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */

function callback($buffer)
{
 Global $tags;
 foreach($tags as $key=>$value){
 $buffer=str_replace($key, $value, $buffer);
 }

 return ($buffer);
}

 ob_start("callback");

 require("includes/header.php");
 $includefile="content/default.php";
 if(isset($parameters[0])){
 if(file_exists("content/".$parameters[0].".php")){
 $includefile="content/".$parameters[0].".php";
 }
 }

 require($includefile);

 require("includes/footer.php");

 ob_end_flush();
 ?>

and finaly
d. .htaccess
This is an important file, so the site can work

# AddType x-mapp-php5 .php
# AddHandler x-mapp-php5 .php
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
ErrorDocument 404 /page-not-found/
<files ~ "\.tpl$">
order deny,allow
allow from none
deny from all
</files>

3. In [CONTENT] directory, we will put the content pages, offcourse. We need just 1 to begin: default.php
Here is it:

<?php

/**
 * @author Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */

$tags=array();
$tags['{title}']="This is the title of the site";
$tags['{description}']="Here is description of the site";
$tags['{keywords}']="and off course, the keywords";
?>
<div id="content">
<h1>Home</h1>
</div>

Some words on this:

  • You can create tags for each page of the site -> useful for SEO
  • Replace what is between <div id=”content”> … </div> with your code, HTML or PHP for the page.

4. In [INCLUDES] directory, we will put the header.php and footer.php.

   a. header.php
<?php

/**
 * @author Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
 <meta name="author" content="cristian.tantar@gmail.com" />
 <meta http-equiv="Description" content="{description}" />
 <meta http-equiv="Keywords" content="{keywords}" />
 <meta http-equiv="Revisit-after" content="2 Days" />

 <link href="<?=MAIN_DIR;?>/style.css" rel="stylesheet" type="text/css"/>
 <script type="text/javascript" src="<?=MAIN_DIR;?>/lightbox.js"></script>

 <style>
 </style>

 <title>{title}</title>
</head>

<body>
<div id="divMain">
 <div id="logo"><a href="<?=MAIN_DIR;?>"><img src="images/logo.jpg" border="0"/></a></div>

This 2 lines:

<link href="<?=MAIN_DIR;?>/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="<?=MAIN_DIR;?>/lightbox.js"></script>

I use styles from style.css and Lightbox script for my images to popup nice.
(of course, other files are included in main dir, all files required by Lightbox script to run)
b. footer.php

<?php

/**
 * @author Cristian Tantar <http://www.tantar.info> <cristian.tantar@gmail.com>
 * @copyright 2009
 */

?>
<div id="footer" >
Copyright info  here
</div>
</div> <-- close mainDiv-->
</body>
</html>

This is main configuration.
I need only to take care of design, slice it, save images in [IMAGES] folder, and make the header.php,footer.php and content.php pages.
For other content pages, like Services and Contact, you can create "services.php" and "contact.php".
We will rich to them, by http://siteurl/services or http://siteurl/contact .

26
Jul/09
0

Improve OSCommerce also_consider.php

After making some mods on oscommerce file also_consider.php, i-ve find out that the results are not so well displayed, i mean, products are taken from the same categoy, or having almost the same price, but this is not enaugh. So, i-ve recreate the SQL script, so the result is displayed by relevance.

it’s an easy mod:

file to change: includes/modules/also_consider.php

find:

if (mysql_num_rows($prod_query) > 0) { 

above this, change what is between {

$sql=”select p.products_id, p.products_image, p.products_price, p.products_model, pd.products_name, p.products_tax_class_id from ” . TABLE_PRODUCTS . ” p, ” . TABLE_PRODUCTS_DESCRIPTION . …

}

with:


{

$cuvant=$product_info['products_name'];
$split_stemmed=explode(" ",trim($cuvant));
$split_stemmed=array_unique($split_stemmed);
$temp=array();
foreach($split_stemmed as $cuvant){
if(strlen(trim($cuvant))>2){
$cuvant=trim(ereg_replace("[^A-Za-z0-9]", "", $cuvant ));
if(strlen($cuvant)==3){$cuvant.="*";}
$temp[]=$cuvant;
}
}
$split_stemmed=$temp;

$string="";
$f=0;
while(list($key,$val)=each($split_stemmed)){
if($f>1){
$string.=$val.',';
}else{
$string.=''.$val.',';
}
$f++;
}

$string=substr($string,0,strlen($string)-1);

$sql = "SELECT
p.products_id, p.products_image, p.products_price, p.products_model, pd.products_name, p.products_tax_class_id,
( (90 * (MATCH(pd.products_name) AGAINST ('".$string."' ))) + (10 * (MATCH(pd.products_description) AGAINST ('".$string."' ))) ) AS relevance

from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " pc
WHERE
( (0.9 * (MATCH(pd.products_name) AGAINST ('".$string."' IN BOOLEAN MODE))) + (0.1 * (MATCH(pd.products_description) AGAINST ('".$string."' IN BOOLEAN MODE))) )
and   p.products_status = '1' and p.products_id = pc.products_id and p.products_id = pd.products_id and p.products_id != '" . (int)$HTTP_GET_VARS['products_id'] . "'  and pd.language_id = '" . (int)$languages_id . "'
GROUP BY p.products_id ORDER BY relevance DESC LIMIT " . (MAX_ALSO_CONSIDER);
$prod_query = tep_db_query($sql);

}