base path and module path

to get theme path use

drupal_get_path('theme', 'theme_name')

for module path use

drupal_get_path('mobule', 'module_name')

to get base url

$GLOBALS['base_path']

Creating Drupal Module – Part 2

now I will cover the mymodule.module part

there is three hooks we use alot in backend those are

insert_hook, update_hook, delete_hook

each hook add node, delete node, and update node

insert_hook

starting clear we can implement hook insert by name modulename_insert

function modulename_insert($node) {
  db_query("INSERT INTO {mytable} (nid, extra)
    VALUES (%d, '%s')", $node->nid, $node->extra);
}


function modulename_update($node) {
//if the node is set for revision insert instead of delete
if ($node->revision) {
 modulename_insert($node);
 }
 else
{
 db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d",
    $node->extra, $node->nid);
}
}
function modulename_delete(&$node) {
  db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
}

Creating Drupal Module Part 1 – Install Package

first of all we should know that the name of installing any function should be modulename.install and we wont need

<?php
function mymodule_schema() {
$schema['mytable1'] = array(
// specification for mytable1
);
$schema['mytable2'] = array(
// specification for mytable2
);
return $schema;
}

function mymodule_install() {
// Create my tables.
drupal_install_schema('mymodule');
drupal_set_message(t('The tables created successfully'));
}

function mymodule_uninstall() {
// Drop my tables.

drupal_uninstall_schema('mymodule');
drupal_set_message(t('The tables were removed'));

}
?>

sample of table schema array

$schema['distribution_center'] = array(
‘fields’ => array(
‘vid’ => array(
‘type’ => ‘int’,
‘not null’ => TRUE,
‘description’=> “Log ID”
),
‘nid’=>array(
‘type’=>’int’,
‘not null’=>FALSE,
),
‘fname’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘lname’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘address’=>    array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘country’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘city’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘phone’=>array(
‘type’=>’text’,
‘not null’=>FALSE,
),
‘mobile’=>array(
‘type’=>’text’,
‘not null’=>FALSE,
),
‘email’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
’site’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘center_attr’=>    array(
‘type’=>’text’,
‘not null’=>FALSE,
),
),
‘primary key’ => array(‘vid’)
);

Logging in Drupal

$msg = ‘No content from %url.’;
$vars = array( ‘%url’ => $url );
watchdog(‘module_name’, $msg, $vars, WATCHDOG_WARNING);

There are eight different constants that can be passed to this function:
WATCHDOG_EMERG: The system is now in an unusable state.
WATCHDOG_ALERT: Something must be done immediately.
WATCHDOG_CRITICAL: The application is in a critical state.
WATCHDOG_ERROR: An error occurred.
WATCHDOG_WARNING: Something unexpected (and negative) happened, but
didn’t cause any serious problems.
WATCHDOG_NOTICE: Something significant (but not bad) happened.
WATCHDOG_INFO: Information can be logged.
WATCHDOG_DEBUG: Debugging information can be logged.

How to add paging in Drupal

this article is taken from http://www.sononix.com/node/270 just placed here for reminder purposes

//This is numbers per page
$num_per_page = 5;

//actual query

$query = “SELECT n.nid, n.created FROM {node} n WHERE n.type = ‘halloffame’ AND n.status = 1 ORDER BY n.created DESC”;

//the count query should be similar to the query above
$count_query = “SELECT COUNT(*) AS row_count FROM {node} n WHERE n.type = ‘halloffame’ AND n.status = 1 ORDER BY n.created DESC”;

//pager_query function
$result = pager_query($query, $num_per_page, 0, $count_query, $user_load->uid);

//dont forget

while ($node = db_fetch_object($result)) {
$output .= node_view(node_load(array(‘nid’ => $node->nid)), 1);
}

$output .= theme(‘pager’, NULL, 5, 0);

print $output;

?>

Starting with SQLITE WITH PHP

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>";
}

using curl as post

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

access associative array in_array

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!

get unique value from XML array

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));
}
}

div open close

a script that help open three divs only at times, a very good I even used it in joomla news in front page by modifying tempaltes/mytemplate/html/com_content/frontend

<script type=”text/javascript”><!–
function OpenDiv(id) {
var opened_divs = 0;
for (i=1;i<=5;i++) { // so you can add more than 2
var divname = ‘div’+i;
var divstyle = document.getElementById(divname).style;
if(divstyle.display == ‘block’) opened_divs++;
}
var state = document.getElementById(id).style;
if(state.display == ‘block’) {
state.display = ‘none’;
return;
}
else if (state.display == ‘none’){
if(opened_divs < 3) {
state.display = ‘block’;
return;
}
else {
for (j=3;j<=5;j++) {
var new_div = ‘div’+j;
if(document.getElementById(new_div).style.display == ‘block’ && new_div != id) {
document.getElementById(new_div).style.display = ‘none’
document.getElementById(id).style.display = ‘block’
return;
}
}
}
}
}
// –></script>
<div id=”alldivs”><!– all will probably confuse IE = see document.all –>
<a class=”gray” onclick=”OpenDiv(‘div1′);”>DIV 1</a>
<div id=”div1″ style=”display: block;”>Div 1</div>
<a class=”gray” onclick=”OpenDiv(‘div2′);”>DIV 2</a>
<div id=”div2″ style=”display: block;”>Div 2</div>
<a class=”gray” onclick=”OpenDiv(‘div3′);”>DIV 3</a>
<div id=”div3″ style=”display: block;”>Div 3</div>
<a class=”gray” onclick=”OpenDiv(‘div4′);”>DIV 4</a>
<div id=”div4″ style=”display: none;”>Div 4</div>
<a class=”gray” onclick=”OpenDiv(‘div5′);”>DIV 5</a>
<div id=”div5″ style=”display: none;”>Div 5</div>
</div>

Return top

INFORMATION

Change this sentence and title from admin Theme option page.