Sunday, February 28, 2010

Difference between section and foreach loop in Smarty

The main difference between SECTION and FOREACH is that

1.For SECTION you can start from a specific value, and can also set a step for the iteration, whereas for FOREACH you have to loop over all values.

2.foreach is used for associative arrays

Thursday, February 25, 2010

Array Functions : How to insert an element or remove an element from an array

In this process you can insert an element or remove an element from an array


$numbers = array(1,2,3,4,5,6);
print_r($numbers);
echo "

";

// shifts first element out of an array
// and returns it.
$a = array_shift($numbers);
echo "a:" . $a ."
";
print_r($numbers);
echo "

";

// prepends an element to an array,
// returns the element count.
$b = array_unshift($numbers, 'first');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

echo "
";

// pops last element out of an array
// and returns it.
$a = array_pop($numbers);
echo "a: " . $a ."
";
print_r($numbers);
echo "

";

// pushes an element onto the end of an array,
// returns the element count.
$b = array_push($numbers, 'last');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

Friday, February 5, 2010

Import mysql data to excel

// Connect database.
mysql_connect("localhost","","");
mysql_select_db("import");

// Get data records from table.
$result=mysql_query("select * from name_list order by id asc");

// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();

/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'
*/

xlsWriteLabel(0,0,"List of car company.");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"No.");
xlsWriteLabel(2,1,"Company");

$xlsRow = 3;

// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){

xlsWriteNumber($xlsRow,0,$row['id']);
xlsWriteLabel($xlsRow,1,$row['name']);

$xlsRow++;
}
xlsEOF();
exit();

Calculate Visitor Counter

Follow the step
1. Create a file name "visitcount.dat"
2. Upload the file in webserver
3. make file permission CHMOD *777*
Now Insert the PHP code below


$counterfile = "visitcount.dat";
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");
$count = (int) fread($fp, 20);
fclose($fp);
$count++;
echo "Total hit counter: $count";
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off