Programming

Generating Serial Page Numbers in PHP [Pager]

Tagged:  

Hello friends,

In this tutorial we explain how to generate page numbers serially and store them in a file called pages.html

You can further use this tutorial to implement this technique in your CMS [if you are building one :D]

In this tutorial we count the no. of articles in the database and divide it by 5 which implies that we display 5 articles per page. (we take intval() of the result).

This code will generate page nos as

Page 1

Page 2

....so on

Enjoy!

The code:

 

<?php

$db = mysqli_connect("localhost","user","password","database");

if ( mysqli_errno($db) ) {

die(mysqli_error($db));

}

$count = mysqli_query($db,"SELECT COUNT(*) FROM content");

if ( mysqli_errno($db) ) {

die(mysqli_error($db));

}

$r = mysqli_fetch_row($count);

$pg_no = intval($r[0]/5);

$htmlfile = fopen("pages.html", "w") or die("Error occurred while opening file");

for ( $i = 0; $i != $pg_no; $i++ ) {

$str = "<a href=home.html?p=".($i+1).">Page ".($i+1)."<br>";

fwrite($htmlfile, $str);

}

fclose($htmlfile);

print "\n";

?>

Adding your META tags on to a drupal page

Tagged:  

Most blogging systems OR CMSes don't have the option to add the META KEYWORDS and META DESCRIPTION tags which are the key to search engine success to some extent.

Yes, I know you can add them in theme files... but if you change theme ? You have to re-add them. So we need a persistent solution which doesn't change while changing themes.

Below is a simple procedure where you can add your META tags to a drupal (taking 5.x here) system (without editing any of the theme files!).

Open common.inc in your favourite editor. common.inc is found in <drupal-install-path>/includes

Scroll down to Line Number 116 (or search for drupal_get_html_head).

After the first function line, you will see a line like this -

 
$output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";

You then append these lines RIGHT AFTER the above line (else its not going to work).


$output .= '<meta name="keywords" content="keywrd1,keywrd2,..." />'."\n";

$output .= '<meta name="description" content="A small description here" />'."\n";

Save the file. Make a backup of the older file and place this new common.inc overriding the old one.

The above method has been tested and was found to work. So, if you have any problems just comment down here!

Have a nice day!

Remove your site titles from content pages in Drupal

Tagged:  

In Drupal (considering v5.x here), the content page titles i.e. the text which is generated and put between <TITLE> HTML tag comes like this -

Content-Name | Site Name

Right ?

This doesn't look nice and it is not possible to remove this by editing a theme's file.

So here's a simple code change about how to remove this title internally so that whenever it is generated, it gets generated as

Content-Name

We're considering the default template engine - PHPTemplate which comes with Drupal.

You need to edit the file called phptemplate.engine file in /themes/engines/phptemplate directory.

Open the file your favourite editor.

Jump to Line 185 (As of Drupal 5.x)

$head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));

Change the above line so that it looks like this -

$head_title = array(strip_tags(drupal_get_title()));

Save the file.

And copy this new file to /themes/engines/phptemplate/phptemplate.engine while backing up the older file.

And voila! you are done!

PHP: Receiving Function Arguments without declaring any parameters in prototype!

Tagged:  

As you know, we usually declare functions in PHP like this -

function foo($arg1, $arg2) { 

// code

}

This leads to PHP-error when one of the argument is missing. To deal with this, we have an alternate method. We shall NOT declare ANY arguments in the prototype. Then we'll use func_get_args() to get all the arguments passed to function in an array. Further we can manipulate the array as we want.

function foo() {

$args = func_get_args();

}

When you will call foo(1,2,3) then $args[0] = 1 ; $args[1] = 2 and so on.

This way you can prevent PHP-Errors and have custom errors.

To handler PHP-Errors I suppose there does exist an function to set a custom handler for PHP-Errors. This is just an alternate method ;)

Extracting Data from a file using cut and grep

Ever thought of having a database as a single TEXT file ? You could do that, but how do you extract data from it ?

This tutorial deals with extracting data from a SINGLE file very much similar to a RDBMS SELECT with WHERE query.

Let's create a file with the following lines -

A;A1;A2;A3;A4;A5
B;B1;B2;B3;B4;B5
C;C1;C2;C3;C4;C5
D;D1;D2;D3;D4;D5
E;E1;E2;E3;E4;E5
F;F1;F2;F3;F4;F5

As you see above, data has been separated by semicolons (;).

Suppose you want to extract the 4th field from the file for the record which starts with C.

To do the above, use this -

$ grep ^C file | cut -f4 -d\;

Please note that this will give you the value which says C3. This is because the first entry itself is a field.

In the above command, grep ^C means show all lines starting with the letter C.

And the next part cut -f4 -d\; means ONLY show the fourth field and since we have separated our fields by a semicolon, the -d\; option is used. Here you need to put the backslash before the semicolon because semicolon is a SHELL character. You could also use colon (:) in the file and the cut command to avoid this problem.

Please comment if you have questions/suggestions.

Creating a MySQL quoter

Tagged:  

Well, many times we have to quote field names and table names in SELECT, INSERT, and other MySQL queries via PHP using backticks `  . This is a big trouble inserting backticks in each SQL query especially when you are using the traditional MySQL extension instead of MySQLi which has a prepare() method. Well, I have created a simple PHP function (rather a snippet :D) in which you just have to specify the field names to be quoted (it can also be table names). So, here's my snippet -

(read at the end what it does)

function quote($f = NULL,$quote="`") {
    
    if($f == NULL):
        
        return false;
            
    endif;

    $f explode(',',$f); 
        
    foreach($f as $k => $v) {
        
        $sql_fields .= $quote.$v.$quote.","; 
            
    }
        
    $sql_fields strrev($sql_fields); 
        
    $sql_fields_exp explode(',',$sql_fields); 
        
    end(array_reverse($sql_fields_exp)); 
        
    array_shift(&$sql_fields_exp); 
        
    $sql_fields array_reverse($sql_fields_exp); 
        
    $while_init=0; 

    $while_count=count($sql_fields); 

    while($while_init != $while_count) {
          $sql_fields[$while_init] = strrev(&$sql_fields[$while_init]);
        
        $while_init++;

    }

  $sql_fields implode(',',$sql_fields);
    
    return $sql_fields;
    }

Description of what it does -

  • By default if no input is given to the function, it will set $f to NULL and $quote to a backtick.
  • Next, it checks if $f is NULL or not.
  • Now, the input $f is split into an array by removing the commas between the input string.
  • Each field into the split array $f, is appended as a string to $sql_fields with a comma on the end which results in an unwanted comma at the end of the string.
  • The string $sql_fields is reversed.
  • And again the fields are exploded by removing a comma preserving the backtick on each field name.
  • Now we reverse the array as obtained in the previous step and set the array's internal pointer to the end.
  • Now, the last element is empty eariler was a comma is deleted from the array.
  • We create a new array which contains the reversed array obtained in the previous step.
  • As you know, explode() splits a string into numerical array (or an array with numeric indices).
  • So, we have a while loop which brings back the order of fields (it matters a lot in inserts).
  • Now, imploding the new string with a comma gets us the final result!

I have tested this code with single value and multiple value -- it works!

If you have any suggestions, questions, please comment else spread the link to everyone!

Syndicate content
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by the Poster. Rest Copyright © iTech7.com

For License and Copyrights see http://www.itech7.com/Content-Copyrights