• Home
  • About
  • Job Profile
  • Portfolio
  • Training
Blue Orange Green Pink Purple

Archive for March, 2013

You can use the search form below to go through the content and find a specific post or page:

Mar 21

insert string with single quote(‘) or double quote(“) in mysql

In developing web application, we write insert query for inserting data into database. Hence i use mysql query and PHP functions for inserting string with single quote(‘) or double quote.

let we know two useful PHP function :

1. addslashes – Quote string with slashes. Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).

<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>

2. stripslashes — Un-quote string quoted with addslashes(). Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

<?php
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

Now come to the point…..if we insert string into database with single or double quote like this :

<?php
$str = "Is your name O'reilly?";
$query = "INSERT INTO tbl (description) VALUES ( '$str')";
?>

This will occur error.

but if we use addslashes($str) function like below and then insert into database, then no error will be occurred.

<?php
$str = "Is your name O'reilly?";
$desc_str = addslashes($str);
$query = "INSERT INTO tbl (description) VALUES ( '$desc_str')";
?>

similarly we can use stripslashes($str) to print that table field value like this :

<?php
echo stripslashes($str);
?>

cheers :)

Mar 21

Get users IP by Getenv() PHP Function

Definition: Getenv () is used to get the value of an environmental variable. It is written as Getenv (varname) .

Below are some examples of environmental variables you can use. If you are interested, you can see a full list by running phpinfo ().

Also Known As: Get Environment Variable

Examples:

Get users IP by getenv() function. This is useful, when you want to know the users IP of your site during registration. By this you can know about users that, from where they are signing up or related information.

You can also track users during login that, every time users are logging in and you know from which area or IP they are access your site. So see the simple use of this function:

<?php
//Gets the IP address
$ip = getenv(“REMOTE_ADDR”) ;
Echo “Your IP is ” . $ip;
?>

<?php
//Gets the document root
$root = getenv(“DOCUMENT_ROOT”) ;
Echo $root;
?>

<?php
//Gets the server admin’s email
$ad = getenv(“SERVER_ADMIN”) ;
Echo $ad;
?>

Happy’s coding :)

Definition: Getenv () is used to get the value of an environmental variable. It is written as Getenv (varname) ;

Mar 21

Alter Dynamic Table Row Background Colors Using JavaScript

Many web applications that present tabular data use alternating background colors of table rows to increase the readability of that data. This could be more attractive and nice looking for visitors.

I wanted the table to be dynamic. We can use PHP for doing this, but hence I’m showing it in JavaScript.

Getting Started

Let’s start with an ordinary html table. Whether the table contains head/foot elements doesn’t matter in this case:

<table id="theTable">
<tr><td>0 - some txt</td></tr>
<tr><td>1 - some txt</td></tr>
<tr><td>2 - some txt</td></tr>
<tr><td>3 - some txt</td></tr>
<tr><td>4 - some txt</td></tr>
</table>

we use a style element through which we have defined two classes for background colors:

<style>
.odd{background-color: white;}
.even{background-color: gray;}
</style>

The style is flexible: it could just as well define something else, such as that every second row should display in italics. The complete function looks like this:

function alternate(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
//manipulate rows
if(i % 2 == 0){
rows[i].className = "even";
}else{
rows[i].className = "odd";
}
}
}
}

The above function should be called from the onload event of the body tag:

<html>
…
<body onload="alternate('thetable')">
<table id="thetable">
<tr><td>…</td></tr>
</table>
…

The result could look something like this:

row color img

row color example

Hope this may help the beginners who are developing web applications. :)

Happy’s coding.

Many web applications that present tabular data use alternating background colors of table rows to increase the readability of that data. This could be more attractive and nice looking for visitors.

I wanted the table to be dynamic. We can use PHP for doing this, but hence I’m showing it in JavaScript.

Getting Started
Let’s start with an ordinary html table. Whether the table contains head/foot elements doesn’t matter in this case:

<table id="theTable">
<tr><td>0 - some txt</td></tr>
<tr><td>1 - some txt</td></tr>
<tr><td>2 - some txt</td></tr>
<tr><td>3 - some txt</td></tr>
<tr><td>4 - some txt</td></tr>
</table>

we use a style element through which we have defined two classes for background colors:

<style>
.odd{background-color: white;}
.even{background-color: gray;}
</style>

The style is flexible: it could just as well define something else, such as that every second row should display in italics. The complete function looks like this:

function alternate(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
//manipulate rows
if(i % 2 == 0){
rows[i].className = "even";
}else{
rows[i].className = "odd";
}
}
}
}

The above function should be called from the onload event of the body tag:

<html>
…
<body onload="alternate('thetable')">
<table id="thetable">
<tr><td>…</td></tr>
</table>
…

The result could look something like this:
1457_rowcolor1

Hope this will may help the beginners who are developing web applications. J

Happy’s coding.

Saif The Green

  • View Saif's profile on LinkedIn
  • Recent Posts
    • RESTful API – The HTTPish …
    • SUSE – another user-friendly desktop Linux distributions
    • SEO meta techniques for a wordpress blog
    • Set favicon in cross browser and more…
    • WordPress custom URL rewrites and tips
  • Archives
    • October 2016
    • August 2016
    • August 2014
    • July 2014
    • April 2014
    • June 2013
    • April 2013
    • March 2013
  • Categories
    • CodeIgniter
    • Javascript
    • Linux
    • MySQL
    • opencart
    • php
    • SEO
    • Software Development
    • Web Development
    • Web Services
    • Wordpress
  • Meta
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Archives
    • October 2016
    • August 2016
    • August 2014
    • July 2014
    • April 2014
    • June 2013
    • April 2013
    • March 2013
  • Search







Saif The Green is proudly powered by WordPress
Entries (RSS) and Comments (RSS).