Internet Methodologies Journal And News

Let's explore internet with imran

How can I retrieve values from one database server and store them in other database server using PHP?

We can always fetch from one database and rewrite to another. Here is a very simple solution.


  $db1 = mysql_connect("hostname","username","password") // Connect to Server 1
  mysql_select_db("db1″, $db1);
  $res1 = mysql_query("query",$db1);

  $db2 = mysql_connect("hostname","username","password"); // Connect to Server 2
  mysql_select_db("db2″, $db2);
  $res2 = mysql_query("query",$db2);

At this point you can only fetch records from you previous ResultSet, i.e $res1 – But you cannot execute new query in $db1, even if you
supply the link as because the link was overwritten by the new db, So at this point the following script will fail

$res3 = mysql_query("query",$db1); //this will failSo how to solve that?

Please have a look at.


	$db1 = mysql_connect("hostname","username","password");
	mysql_select_db("db1″, $db1);
	$res1 = mysql_query("query",$db1);

	$db2 = mysql_connect("hostname","username","password", true)
	mysql_select_db("db2", $db2);
	$res2 = mysql_query("query",$db2);

So mysql_connect has another optional boolean parameter which indicates whether a link will be created or not.

As we connect to the $db2 with this optional parameter set to ‘true’, so both link will remain live.

Now the following query will execute successfully.

	$res3 = mysql_query("query",$db1);
  • Share/Bookmark


Word Limiter function that preserves the original whitespace

<?php

echo word_limiter('this is my text for t',3); // display 3 words

function word_limiter($str, $limit = 10, $end_char = '&#8230;') {

    if (trim($str) == '')
        return $str;

    preg_match('/\s*(?:\S*\s*){'. (int) $limit .'}/', $str, $matches);

    if (strlen($matches[0]) == strlen($str))
        $end_char = '';

    return rtrim($matches[0]) . $end_char;
}

?>
  • Share/Bookmark


Explain the difference between FLOAT, DOUBLE and REAL

FLOATs store floating point numbers with 8 place accuracy and take up 4 bytes.

DOUBLEs store floating point numbers with 16 place accuracy and take up 8 bytes.

REAL is a synonym of FLOAT for now.

  • Share/Bookmark


How to get file extension using PHP ?

Example 1:

function getFileExtension($filename)
{
	$fileinfo= pathinfo($filename);
	return $fileinfo[extension];
}

Example 2:

$ext = end(explode('.', $file));
$ext = substr(strrchr($file, '.'), 1);

$ext = substr($file, strrpos($file, .) + 1);

$ext = preg_replace(/^.*\.([^.]+)$/D, $1, $file);

$exts = split("[/\\.]", $file);

$n = count($exts)-1;
$ext = $exts[$n];
  • Share/Bookmark


Explain about the naming Conventions in cakePHP – cakePHP Interview Question

  • Table names are plural and lowercased.
  • Model names are singular and CamelCased: ModelName.
  • Model filenames are singular and underscored: model_name.php.
  • Controller names are plural and CamelCased with *Controller* appended: ControllerNamesController.
  • Controller filenames are plural and underscored with *controller* appended: controller_names_controller.php
  • Associations should use the ModelName, and the order should match the order of the foreignKeys: var $belongsTo = ‘User’;
  • Foreign keys should always be: table_name_in_singular_form_id: user_id (foreign key) -> users (table)
  • Many-to-many join tables should be named: alphabetically_first_table_plural_alphabetically_second_table_plural: tags_users , columns in many-to-many join tables should be named like other foreign keys: tag_id and user_id , columns named “created” and “modified” will automatically be populated correctly
  • Share/Bookmark


A cute little function for truncating text to a given word limit:

<?php
echo limit_text('this is my text for t', 2); //display 2 words

    function limit_text($text, $limit) {
      if (strlen($text) > $limit) {
          $words = str_word_count($text, 2);
          $pos = array_keys($words);
          $text = substr($text, 0, $pos[$limit]) . '...';
      }
      return $text;
    }
?>
  • Share/Bookmark


Blueprint – A very good CSS framework you should know

Blueprint is a CSS framework, which aims to cut down on your development time. It gives you a solid foundation to build your project on top of, with an easy-to-use grid, sensible typography, useful plugins, and even a stylesheet for printing.

What does Blueprint have to offer?

  • A CSS reset that eliminates the discrepancies across browsers.
  • A solid grid that can support the most complex of layouts.
  • Typography based on expert principles that predate the web.
  • Form styles for great looking user interfaces.
  • Print styles for making any webpage ready for paper.
  • Plugins for buttons, tabs and sprites.
  • Tools, editors, and templates for every step in your workflow.

Source: http://www.blueprintcss.org/

  • Share/Bookmark


How to append dots after particular number of words in PHP?

<?php
echo wordLimiter('this is my text for t',2);

function wordLimiter($text,$limit=20){
    $explode = explode(' ',$text);
    $string  = '';

    $dots = '...';
    if(count($explode) <= $limit){
        $dots = '';
    }
    for($i=0;$i<$limit;$i++){
        $string .= $explode[$i]." ";
    }

    return $string.$dots;
}
?>
  • Share/Bookmark


Count HTML Tags in PHP through strlen() function

<?php

	$text = "<p>This is 'a' my first paragraph.</p><!-- Comments --> sample text text";

	echo "String Lenght of HTML chars:".strlen_noHtml($text); // 48 chars

function strlen_noHtml($string){
    $crs = 0;
    $charlen = 0;
    $len = strlen($string);
    while($crs < $len)
    {
        $offset = $crs;
        $crs = strpos($string, "<", $offset);
        if($crs === false)
        {
           $crs = $len;
           $charlen += $crs - $offset;
        }
        else
        {
            $charlen += $crs - $offset;
            $crs = strpos($string, ">", $crs)+1;
        }
    }
    return $charlen;
}
?>
  • Share/Bookmark


Count a string with including HTML Tags through strlen()


$text = "<p>This is 'a' my first paragraph.</p><!-- Comments --> sample text text";

echo "String Lenght of HTML chars:".strlen_html($text); // 24 chars

function strlen_Html($string){
    $crs = 0;
    $charlen = 0;
    $len = strlen($string);
    while($crs < $len)
    {
        $scrs = strpos($string, "<", $crs);
        if($scrs === false)
        {
           $crs = $len;
        }
        else
        {
            $crs = strpos($string, ">", $scrs)+1;
            if($crs === false)
                $crs = $len;
            $charlen += $crs - $scrs;
        }
    }
    return $charlen;
}
?>
  • Share/Bookmark