Breathing Spring

Archive for the ‘php’ Category

Starting with SQLITE WITH PHP

No Comments »

January 26th, 2010 Posted 2:57 pm

First of all you need to make sure that the extension of sqlite is working through the settings in php.ini

Assuming its working, I will make it brief first create a file names create_database.php
And add the following to it


$db= new SQLiteDatabase("db.sqlite");
$db->query("
BEGIN;
CREATE TABLE users (id INTEGER UNSIGNED PRIMARY KEY,
name CHAR(255),
email CHAR(255));
INSERT INTO users (id,name,email) VALUES (NULL,'User1','user1@domain.com');
insert into users (id,name,email) values (NULL, 'user2','user2@lamis.com');
insert into users (id,name,email) values (NULL, 'user3','user3@lamis.com');
COMMIT;
");

This will create a new SQLiteDatabase and a table called users

now create another file call it test_result.php
and add the following to it

$db= new SQLiteDatabase("db.sqlite");
$result = $db->query("Select * from users");
echo "<br><br>++++++ POINTER WAY +++++++<br><br>";
while($result->valid())
{
print_r($result->current());
echo "<br>";
$result->next();
}
echo "<br><br>++++++ ASSOCIATIVE WITH FETCH+++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetch(SQLITE_ASSOC))
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ NUM WITH FETCH+++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetch(SQLITE_NUM))
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ FETCH ALL ++++++<br><br>";
$result = $db->query("Select * from users");
$rows= $result->fetchAll();
foreach($rows as $row)
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ FETCH Object ++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetchObject())
{
print_r($row);
echo "<br> you can call any like this \$row->id<br><br><br>";
}

Posted in php

using curl as post

No Comments »

January 25th, 2010 Posted 1:51 pm

First of all know this code work for something with no captcha

here how it goes

$url = "http://someurl.com";
$ch = curl_init();

The url is to specify the link for the post


// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 3);
// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,"name=".$name."&username=".$username.."&email=".$email);
//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result= curl_exec ($ch);
curl_close ($ch);

We can also add some settings which can be useful , for instance to ignore redirection in the url we can add

curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);

Here is a link of the set of the things we can add

http://ca3.php.net/manual/en/function.curl-setopt.php

Posted in php

access associative array in_array

No Comments »

December 1st, 2009 Posted 9:09 am

so let me explain this
usually we have arrays like this

$my_array= new Array ("US","UK", "FR")
and to search for a value we do
if( in_array("FR", $my_array) )

the problem is when we have an associative array that has several values something like this

$my_array= new Array ("US"=>array(40,50) ,"UK"=>array(46,60) , "FR"=>array(70,50) );

now searching for FR wont be possible with the previous way

so what we do is simple easy add!

if(in_array("FR" ,array_keys($my_array)))

enjoy!

Posted in php

get unique value from XML array

No Comments »

April 1st, 2009 Posted 9:08 am

this should work best as far as I know and tried..I used the serialize because oddly it couldnt compare strings

hope it maybe help someone there that needs it

$new=array();

$exclude = array("");

foreach ($myItems as $item)
{
if (!in_array(serialize($item->fileName) ,$exclude))
{
$new[] = $item;
array_push($exclude, serialize($item->fileName));
}
}

Posted in php

Wrong And Rights

No Comments »

March 15th, 2009 Posted 7:37 pm

Switch statements:
// Always assume that the case got not catched

// Wrong

switch ($mode)
{
	case 'mode1':
		// I am doing something here
		break;
	case 'mode2':
		// I am doing something completely different here
		break;
}

// Good

switch ($mode)
{
	case 'mode1':
		// I am doing something here
	break;

	case 'mode2':
		// I am doing something completely different here
	break;

	default:
		// Always assume that the case got not catched
	break;
}

Operations in loop definition:

// On every iteration the sizeof function is called

for ($i = 0; $i < sizeof($post_data); $i++)
{
	do_something();
}

// You are able to assign the (not changing) result within the loop itself

for ($i = 0, $size = sizeof($post_data); $i < $size; $i++)
{
	do_something();
}

Posted in php

subtract current date with past date in PHP

No Comments »

August 30th, 2008 Posted 1:01 pm

<?php
function subtract_dates($begin_date, $end_date)
{
return round(($end_date – $begin_date) / 86400);
}
$beginDate= mktime(0, 0, 0, 8, 30, 2008);
$currentDate= mktime();
echo subtract_dates($beginDate,$currentDate);

?>

Posted in php

Short Echo If Else Statment

No Comments »

August 3rd, 2008 Posted 8:11 am

there are two ways of doing short echo statment

<?php echo condition? “right” : “left”;?>

I beleive the above way is the best because it has standards, I heard from a friend that <?php is very important. I know he is a geek about making things super perfect but anyways , I go with it.

There is a shorter version which I dont prefer

<?= echo condition? “right” : “left”;?>

though both work but the first one is more right to me and to my computer geek friend ;)

Posted in php

fetching one result from mysql

No Comments »

June 29th, 2008 Posted 8:21 am

usually we use mysql queries to retrieve largeĀ  data contents.

But sometimes all we need is a number (Max, Count, or maybe just to retrieve unique feild). so we wont need to create an array.

to do so we can try the following code

$query=”select max(‘num’) from table”;

$result=mysql_query(query);

//now the magic line

$MaxNum=mysql_result($result,0,0)

there must be a reason for the 0, 0 but actually I dont know. if you know and can help me then I will be grateful.

Posted in php