Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, April 23, 2010

mysql a way to do - copy, insert, select,

Mysql PHP Insert multiple data in 1(one) query

<?

$sql = ' INSERT INTO tablename ( fieldA , fieldB ) VALUES ( dataA1 , dataB1 ), ( dataA2 , dataB2)';
$result = mysql_query($sql);

?>

Insert/copy data from other to another table

<?

$sql = ' INSERT INTO tableA SELECT * FROM tableB ';
$result = mysql_query($sql);

?>

CodeIgniter using controller for beginner

You must know that are

-config : Configuration files
-helper : helper for application, contain function that you create
-libraries : Collection of your code.
-errors : Contains template for error display
-hooks : Controlling files load.
-model : Collection of your code
-controller : Controller, control client request to others(view,library... etc)
-view : Template for showing information to user.



simple create test page

1. if your class name is thegreatestpost, you should save your controller file as thegreatestpost.php

<?php
class thegreatestpost extends Controller {

function index()
{
echo 'Hello the greatest post!';
}
}
?>


to view it, www.your-site.com/index.php/thegreatestpost/


if you want controller thegreatestpost is default controller, just edit $route['default_controller'] in config\routes.php as :
$route['default_controller'] = 'thegreatestpost';
and url to view index is www.your-site.com/index.php/

display result will be:
Hello the greatest post!

Add another function,

<?php
class thegreatestpost extends Controller {

function index()
{
echo 'Hello the greatest post!';
}
function greatest()
{
echo 'Hello postmen';
}

}
?>


to view greatest www.your-site.com/index.php/thegreatestpost/greatest/
result view:
Hello postmen

to passing value url to your function simply like this:

function greatest($value1 = "") : url= www.your-site.com/index.php/thegreatestpost/greatest/123

meaning $value1 = 123

function greatest($value1 = "", $value2 = "") :url= www.your-site.com/index.php/thegreatestpost/greatest/123/abc

meaning $value1 = 123, $value2 = abc


more reference visit : http://codeigniter.com/

Monday, March 1, 2010

Send SMS function using PHP

What are usually need to make a sms;



This is a simple function to send sms using php script.


<?php

function PostToHostSMS($host, $port, $username, $password, $data_to_send)
{
$dc = 0;
$bo ="--------453534534534536";

$fp = fsockopen($host, $port, $errno, $errstr);
if (!$fp) {
echo "errno: $errno \n";
echo "errstr: $errstr\n";
return $result;
}

fputs
($fp, "POST / HTTP/1.1\r\n");
if ($username != "") {
$auth = $username . ":" . $password;
echo "auth: $auth\n";
$auth = base64_encode($auth);
echo "auth: $auth\n";
fwrite
($fp, "Authorization: Basic " . $auth . "\r\n");
}
fputs
($fp, "User-Agent: NowSMS PHP Script\r\n");
fputs
($fp, "Accept: */*\r\n");
fputs
($fp, "Content-type: multipart/form-data; boundary=$bo\r\n");

foreach($data_to_send as $key=>$val) {
$ds =sprintf("%s\r\nContent-Disposition: form-data;
name=\"%s\"\r\n%s\r\n"
,$bo,$key,$val);
$dc += strlen($ds);

}
$dc += strlen($bo)+3;
fputs
($fp, "Content-length: $dc\r\n");
fputs
($fp, "\r\n");
fputs
($fp, "This is a MIME message\r\n\r\n");

foreach($data_to_send as $key=>$val) {
$ds =sprintf("%s\r\nContent-Disposition: form-data;
name=\"%s\"\r\n%s\r\n"
,$bo,$key,$val);
fputs
($fp, $ds );
}
$ds = $bo."--\r\n" ;
fputs
($fp, $ds);

$res = "";

while(!feof($fp)) {
$res .= fread($fp,1);
}
fclose
($fp);


return $res;
}

?>


Create xml rss feed with PHP

First, write a script like below;

<?php header('Content-type: text/xml'); ?>

<?php $phpver = phpversion(); ?>

<rss version="2.0">
<channel>
<title>SapuraTIE</title>
<description>Sapuratie</description>
<language>en-US</language>
<link>http://thegreatestpost.blogspot.com/</link>
<copyright>Your copyright</copyright>
<generator>
XML/RSS feed generator by thegreatestpost(PHP version
<?php echo $phpver; ?>)
</generator>

<managingEditor>Site Editor</managingEditor>
<webMaster>Webmaster Email</webMaster>



<?php
require_once('db_connect.php'); //your connection to database

$query = "SELECT id,content_title,content_data,content_date,
content_page FROM sapurastie_article
WHERE content_section <> 10 AND
content_language =
$lang ORDER BY content_date DESC LIMIT 0,15"; //your query
$execute = mysql_query($query) or die(mysql_error());

while($result = mysql_fetch_array($execute)) {
?>
//Start create item
<item>
<title> <?=htmlentities(strip_tags($result['content_title'])); ?></title>
<description>
<?=htmlentities(strip_tags(substr($result['content_data'],0,500)."...",'ENT_QUOTES'));
?>

</description>

<pubDate>
<?php
echo date('r', strtotime($result['content_date'])); ?>
</pubDate>
<guid>http://thegreatestpost.blogspot.com</guid>
</item>
<?php } ?>

</channel>
</rss>

then, the output will be like;



Created by: thegreatestpost.



Wednesday, February 10, 2010

Object Oriented PHP and Basic example OOP

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.
- wikipedia.org

let's go....

Define class
------------
Syntax Class classname { }


Class TheGreatestPost
{
      public $word;
      public function print()
      {
       echo $this->word;
       }
}



To print output
---------------

$post = new TheGreatestPost(); //instantiate the object
$post->word = 'hello The Greatest Post';
$post->print();


::output:: “hello The Greatest Post”.


Using    _construct()
-----------------
special method that is used in the first instantiation of an object that allows you to construct the object when it is invoked, as it is triggered whenever a new object is created.



Class TheGreatestPost
{
     public $word;
     public function __construct($word)
    {
        $this->word = $word;
    }

   public function print()
   {
       echo $this->word;
    }
}

$post = new TheGreatestPost('hello The Greatest Post');
$post->print();


::output:: “hello The Greatest Post”




Class Inheritance
-----------------
Through this, an extended class inherits methods and members of its parent class defined in the extend declaration.




class TheGreatestPost
{
    public function print()
    {
      echo $this->word;
     }
}

class TheGreatestPost2 extends TheGreatestPost
{
     protected $word = 'hello The Greatest Post';
}

$post = new TheGreatestPost2();
$post->print();



::output:: hello The Greatest Post

 
Simple code created by thegreatestpost.


Need more tutorial?? visit: http://www.killerphp.com/tutorials/object-oriented-php/ ( include video tutorial)
thanks to killerphp.com for video tutorial.

Thursday, January 21, 2010

Convert number to Roman numerals in PHP

This script is the way how to convert number (1,2,3,4) to roman number ( i,ii,iii,iv).
Basic roman number:

<?
function numberToRoman($num,$type){

if($type == 1){ //upper character number
// Make sure that we only use the integer portion of the value

$n = intval($num);
$result = '';

// Declare a lookup array that we will use to traverse the number:
$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);

foreach ($lookup as $roman => $value){
// Determine the number of matches
$matches = intval($n / $value);

// Store that many characters
$result .= str_repeat($roman, $matches);

// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
elseif($type == 2){ //low character number

// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';

// Declare a lookup array that we will use to traverse the number:

$lookup = array('m' => 1000, 'cm' => 900, 'd' => 500, 'cd' => 400,
'c' => 100, 'xc' => 90, 'l' => 50, 'xl' => 40,
'x' => 10, 'ix' => 9, 'v' => 5, 'iv' => 4, 'i' => 1);

foreach ($lookup as $roman => $value){
// Determine the number of matches
$matches = intval($n / $value);

// Store that many characters
$result .= str_repeat($roman, $matches);

// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
}
// and your condition to call them (use function )
for($i=1;$i<1000;$i++){
echo numberToRoman($i,1)."<br>";

}
?>


modify from - go4expert.com

Wednesday, January 20, 2010

PHP list Directory and File


Script below to list directory and file using function opendir(); This is a simple code to define directory name, file name, object type and also file extension.


<?php

$dirname = "radio"; // set directory name
$dir = opendir($dirname); // open directory

while(false != ($file = readdir($dir))){

if(($file != ".") and ($file != "..")){ // prevent list hidden directory
echo filetype($dirname."/".$file); //print object type ( dir. or file )
echo "--ext. name:".pathinfo($file, PATHINFO_EXTENSION)."--"; // print file extention
echo("<a href='".$dirname."/$file'>$file</a> <br />"); // print file and the url

}

}

?>


You can customize to be photo, video, music and also others file gallery.

Folder or directory as the category and file in the directory as the item of the category.

For video you can download the plug-in form FLV JW player( posted in article before ) .

Example video listing.


Example file listing


Need a simple script? download here .

have fun!!!


- Created By Thegreatestpost.blogspot.com

Tuesday, January 19, 2010

PHP and jquery file upload with progress bar

A very nice code to add into your application, easy to understand and simple to use.
Just configure 1 php script and 1 html script.
Provide tutorial by www.webdeveloperplus.com.

This is a screen shot of my test on it.




Visit http://demo.webdeveloperplus.com/jquery-swfupload/ to download and test it, http://webdeveloperplus.com/jquery/multiple-file-upload-with-progress-bar-using-jquery/ to heaving tutorial. http://demo.webdeveloperplus.com/jquery-swfupload/ for the demonstration

Thanks to : http://webdeveloperplus.com

Monday, January 18, 2010

A way to prevent SQL injection in PHP

Add this function into yr code.

function cleanInputData($data)
{

$cleaned = trim($data);
$cleaned = mysql_real_escape_string($cleaned);
return $cleaned;

}

Monday, September 28, 2009

FLV video Streaming with PHP

The simple meaning of video streaming is you no need to download video to play but play the video via web, youtube.com as example.

I found a source how to make a video streaming with PHP ( flv format)
Click Here ( credit to FlashComGuru )

visit : www.flashcomguru.com

Tuesday, July 28, 2009

Export data to Excel in PHP

I will show you how to export data html to excel using php with oop implementation.
1. Create file viewhtml.php and excel.class.php(class file)

2. Write code in viewhtml.php :
*******************************

<?

ob_start(); //output buffering is active no output is sent from the script

include 'excel.class.php';

$data = "<table><tr><td>The Greatest Post</td><td>dot blogspot</td></tr></table>"; // the data you want to print

echo $data;

if(isset($_GET['export'])){

for ($i=1;$i<=5;$i++){
echo $data;
}

$create = new Excel; // create obj.

$create->CreateExcel($data,$_GET[export]); //call func. creat

}

?>

<a href="viewhtml.php?export=sini_letak_nama_file">Export Excel</a>

3. Write code in excel.class.php

**************************************

<?php

class Excel {

function CreateExcel($datatoprint,$filename){
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $datatoprint;
}

}

?>

**********************************************

4. Run page viewhtml.php then click link to test.
Have try....!!