<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-337370530728832813</id><updated>2011-11-27T17:32:06.494-08:00</updated><title type='text'>All about PHP/MySQL</title><subtitle type='html'>In this blog I will tend to put my mostly used PHP functions for the benefit of beginners.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-4783332870797059584</id><published>2009-02-07T00:43:00.000-08:00</published><updated>2009-02-07T00:57:22.863-08:00</updated><title type='text'>Remove Common Words from Search String</title><content type='html'>Before sending the user given search string to MySql, its a good idea to remove common words used in it so that it may not disturb any relevant results you have in your DB. Their are far more professional approaches out there for this task but here I am mentioning a very simple one.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;function RemoveCommonWords($sText){&lt;br /&gt;$CommonWords = array (&lt;br /&gt;'at',&lt;br /&gt;'the',&lt;br /&gt;'and',&lt;br /&gt;'of',&lt;br /&gt;'in'&lt;br /&gt;);&lt;br /&gt; for ($x = 0; $x &lt; count($CommonWords); $x++) {&lt;br /&gt;  $sText = str_replace(' ' . $CommonWords[$x] . ' ', ' ', $sText);&lt;br /&gt; }&lt;br /&gt;return $sText;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$search_string = "sports shops in market";&lt;br /&gt;&lt;br /&gt;$search_string = RemoveCommonWords($search_string);&lt;br /&gt;&lt;br /&gt;Build your own list in the function against the variable $CommonWords.&lt;br /&gt;&lt;br /&gt;Good Luck :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-4783332870797059584?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/4783332870797059584/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=4783332870797059584' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4783332870797059584'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4783332870797059584'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/remove-common-words-from-search-string.html' title='Remove Common Words from Search String'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-3701569432406421415</id><published>2009-02-07T00:01:00.001-08:00</published><updated>2009-02-07T03:23:28.478-08:00</updated><title type='text'>PHP function for Basic Image Gallery</title><content type='html'>This PHP function is a very basic one, yet useful for a single page image gallery within a small website. It reads all the images from given folder and creates a simple looking gallery with given sized thumbnails.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;function Create_Gallery($dir, $width, $height){&lt;br /&gt;&lt;br /&gt; $folder=dir($dir);&lt;br /&gt;&lt;br /&gt; $gallery_html = '&amp;lt;div&amp;gt;     ';&lt;br /&gt;&lt;br /&gt;  while($folderEntry=$folder-&gt;read())&lt;br /&gt;  {&lt;br /&gt;      if ($folderEntry != "." &amp;&amp; $folderEntry != ".."){&lt;br /&gt;    $gallery_html .= '   &amp;lt;a href="'.$dir.'/'.$folderEntry.'"&amp;gt;  &amp;lt;img style="border: 6px double rgb(84, 85, 101);" src="'.$dir.'/'.$folderEntry.'" alt="The thumb" width="'.$width.'" height="'.$height.'"/&amp;gt;&amp;lt;/a&amp;gt;';&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt; $gallery_html .= '     &amp;lt;/div&amp;gt; ';&lt;br /&gt; $folder-&gt;close();&lt;br /&gt;&lt;br /&gt;echo $gallery_html;&lt;br /&gt;&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Create_Gallery("gallery_images", "200", "140");&lt;br /&gt;&lt;br /&gt;Where 'gallery_images' is the folder containing the images. 2nd and 3rd arguments of the function are the desired width and height of thumbnails.&lt;br /&gt;&lt;br /&gt;Good Luck :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-3701569432406421415?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/3701569432406421415/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=3701569432406421415' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/3701569432406421415'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/3701569432406421415'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/php-function-for-basic-image-gallery.html' title='PHP function for Basic Image Gallery'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-8508128359176998602</id><published>2009-02-06T00:25:00.001-08:00</published><updated>2009-02-06T00:33:48.996-08:00</updated><title type='text'>Putting watermark on an Image</title><content type='html'>While working on a project, I found this useful class for achieving watermarking task easily. Here is the code.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Class:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;class watermark&lt;br /&gt;    {&lt;br /&gt;        var $img;&lt;br /&gt;        function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) &lt;br /&gt;     {&lt;br /&gt;      $alpha_level /= 100; &lt;br /&gt;      $main_img_obj_w = imagesx( $main_img_obj );&lt;br /&gt;      $main_img_obj_h = imagesy( $main_img_obj );&lt;br /&gt;      $watermark_img_obj_w = imagesx( $watermark_img_obj );&lt;br /&gt;      $watermark_img_obj_h = imagesy( $watermark_img_obj );&lt;br /&gt;      $main_img_obj_min_x=10;&lt;br /&gt;      $main_img_obj_max_x=10;&lt;br /&gt;      $main_img_obj_min_y=10;&lt;br /&gt;      $main_img_obj_max_y=10; &lt;br /&gt;      $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );&lt;br /&gt;       &lt;br /&gt;      for( $y = 0; $y &lt; $main_img_obj_h; $y++ ) &lt;br /&gt;      {&lt;br /&gt;       for( $x = 0; $x &lt; $main_img_obj_w; $x++ ) &lt;br /&gt;       {&lt;br /&gt;        $return_color = NULL;&lt;br /&gt;        $watermark_x = $x - $main_img_obj_min_x;&lt;br /&gt;        $watermark_y = $y - $main_img_obj_min_y;&lt;br /&gt;        $main_rgb = imagecolorsforindex( $main_img_obj, &lt;br /&gt;        imagecolorat( $main_img_obj, $x, $y ) );&lt;br /&gt;        if ($watermark_x &gt;= 0 &amp;&amp; $watermark_x &lt; $watermark_img_obj_w &amp;&amp; $watermark_y &gt;= 0 &amp;&amp; $watermark_y &lt; $watermark_img_obj_h ) &lt;br /&gt;        {&lt;br /&gt;         $watermark_rbg = imagecolorsforindex( $watermark_img_obj,&lt;br /&gt;         imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );&lt;br /&gt;         $watermark_alpha = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );&lt;br /&gt;         $watermark_alpha = $watermark_alpha * $alpha_level;&lt;br /&gt;         $avg_red = $this-&gt;_get_ave_color( $main_rgb['red'],&lt;br /&gt;         $watermark_rbg['red'], $watermark_alpha );&lt;br /&gt;         $avg_green = $this-&gt;_get_ave_color( $main_rgb['green'],&lt;br /&gt;         $watermark_rbg['green'], $watermark_alpha );&lt;br /&gt;         $avg_blue = $this-&gt;_get_ave_color( $main_rgb['blue'],&lt;br /&gt;         $watermark_rbg['blue'], $watermark_alpha );&lt;br /&gt;         $return_color = $this-&gt;_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );&lt;br /&gt;        } &lt;br /&gt;        else &lt;br /&gt;        {&lt;br /&gt;         $return_color = imagecolorat( $main_img_obj, $x, $y );    &lt;br /&gt;        }&lt;br /&gt;        imagesetpixel( $return_img, $x, $y, $return_color );    &lt;br /&gt;       } &lt;br /&gt;      }     &lt;br /&gt;         $this-&gt;img = $return_img;&lt;br /&gt;     }    &lt;br /&gt;     function _get_ave_color( $color_a, $color_b, $alpha_level ) &lt;br /&gt;     {&lt;br /&gt;      return round((($color_a*(1-$alpha_level))+($color_b*$alpha_level)));&lt;br /&gt;     }  &lt;br /&gt;     function _get_image_color($im, $r, $g, $b) &lt;br /&gt;     {&lt;br /&gt;      $c=imagecolorexact($im, $r, $g, $b);&lt;br /&gt;      if ($c!=-1) &lt;br /&gt;      {&lt;br /&gt;       return $c;&lt;br /&gt;      }&lt;br /&gt;      $c=imagecolorallocate($im, $r, $g, $b);&lt;br /&gt;      if ($c!=-1) &lt;br /&gt;      {&lt;br /&gt;       return $c;&lt;br /&gt;      }&lt;br /&gt;      return imagecolorclosest($im, $r, $g, $b);&lt;br /&gt;     } &lt;br /&gt;     function write($target) &lt;br /&gt;     {&lt;br /&gt;      imagejpeg($this-&gt;img, $target, 100);&lt;br /&gt;     }&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$watermark = new watermark();&lt;br /&gt;$main_img_obj = imagecreatefromjpeg("image.jpg"); //Image which you want to put watermark on&lt;br /&gt;$watermark_img_obj = imagecreatefrompng("logo.png"); //Watermark image (PNG file)&lt;br /&gt;$watermark-&gt;create_watermark( $main_img_obj, $watermark_img_obj, 100 );&lt;br /&gt;$watermark-&gt;write("image_with_watermark.jpg"); //Output Image&lt;br /&gt;&lt;br /&gt;It takes image.jpg, puts logo.png on it as watermark and saves it as a new image file named image_with_watermark.jpg. Thats it!&lt;br /&gt;&lt;br /&gt;Happy Coding :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-8508128359176998602?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/8508128359176998602/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=8508128359176998602' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/8508128359176998602'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/8508128359176998602'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/put-watermark-on-image.html' title='Putting watermark on an Image'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-1641945660426448996</id><published>2009-02-05T23:49:00.000-08:00</published><updated>2009-02-07T00:59:19.510-08:00</updated><title type='text'>Calculating Age from Date of Birth</title><content type='html'>In some sections (e.g Profile pages) its nice to display the user's age for the page viewers. Its easier with the following function to calculate age by providing a valid date of birth.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;function GetAge($DOB) {&lt;br /&gt;&lt;br /&gt;list($Year, $Month, $Day) = explode("-",$DOB);&lt;br /&gt;$YearDifference = date("Y") - $Year;&lt;br /&gt;$MonthDifference = date("m") - $Month;&lt;br /&gt;$DayDifference = date("d") - $Day;&lt;br /&gt;&lt;br /&gt;if ($DayDifference &lt; 0 || $MonthDifference &lt; 0) {&lt;br /&gt;$YearDifference--;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return $YearDifference;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$age = GetAge("1981-05-18"); //Year-Month-Day&lt;br /&gt;&lt;br /&gt;echo $age;&lt;br /&gt;&lt;br /&gt;Happy Coding :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-1641945660426448996?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/1641945660426448996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=1641945660426448996' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1641945660426448996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1641945660426448996'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/calculating-age-from-date-of-birth.html' title='Calculating Age from Date of Birth'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-1789589057522543753</id><published>2009-02-05T23:35:00.000-08:00</published><updated>2009-02-09T00:48:25.067-08:00</updated><title type='text'>Fetch a URL (with cURL)</title><content type='html'>cURL is a very useful PHP library which can be used to connect to different types of servers with different types of protocols. Here is a very basic example of its primary usage.&lt;br /&gt;&lt;br /&gt;function fetchPage($url)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;$ch = curl_init($url);&lt;br /&gt;curl_setopt($ch, CURLOPT_HEADER, 0);&lt;br /&gt;curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;$responce = curl_exec($ch);&lt;br /&gt;curl_close ($ch);&lt;br /&gt;      &lt;br /&gt;$data =$responce;&lt;br /&gt;&lt;br /&gt;return $data;&lt;br /&gt;   &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$page_data = fetchpage("www.cnn.com");&lt;br /&gt;&lt;br /&gt;echo $page_data;&lt;br /&gt;&lt;br /&gt;You can do the magic by using &lt;a href="http://www.regular-expressions.info/"&gt;Regular Expressions&lt;/a&gt; on the data stored in $page_data variable.&lt;br /&gt;&lt;br /&gt;See &lt;a href="http://phpbloger.com/blog/2009/02/08/magic-of-simple-html-dom/"&gt;this post&lt;/a&gt; of my kind friend Mr. Siddique Ahmed on crawling guide using Simple Html DOM Class.&lt;br /&gt;&lt;br /&gt;Happy Coding :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-1789589057522543753?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/1789589057522543753/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=1789589057522543753' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1789589057522543753'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1789589057522543753'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/fetch-url-with-curl.html' title='Fetch a URL (with cURL)'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-8532822758958524868</id><published>2009-02-05T23:23:00.001-08:00</published><updated>2009-02-05T23:30:23.523-08:00</updated><title type='text'>Clean An Email Address</title><content type='html'>Use this function to cleanup an email address. It will remove any unwanted character smoothly.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;function clean_email($email = "")&lt;br /&gt;  {&lt;br /&gt;      $email = trim($email);&lt;br /&gt;      $email = str_replace(" ", "", $email);&lt;br /&gt;      &lt;br /&gt;      // Check for more than one @&lt;br /&gt;      if (substr_count($email, '@') &gt; 1)&lt;br /&gt;      {&lt;br /&gt;          return false;&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      $email = preg_replace("#[\;\#\n\r\*\'\"&lt;&gt;&amp;\%\!\(\)\{\}\[\]\?\\/\s]#", "", $email);&lt;br /&gt;      &lt;br /&gt;      if (preg_match("/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", $email))&lt;br /&gt;      {&lt;br /&gt;          return $email;&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;          return false;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$email_address = clean_email("my!email@domain.com");&lt;br /&gt;&lt;br /&gt;It will save "myemail@domain.com" after removing the typo mistake (probably) by user.&lt;br /&gt;&lt;br /&gt;Happy Coding :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-8532822758958524868?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/8532822758958524868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=8532822758958524868' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/8532822758958524868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/8532822758958524868'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/clean-email-address.html' title='Clean An Email Address'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-4497502693954643321</id><published>2009-02-05T22:59:00.000-08:00</published><updated>2009-02-08T23:44:35.964-08:00</updated><title type='text'>Graphical Bar for Percentage Values</title><content type='html'>Following function can be used to display the percentage value (e.g Polling Result) in form of graphical bar.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; function DrawBar($percentage) {&lt;br /&gt; if ($percentage &gt; 0)&lt;br /&gt; {&lt;br /&gt; $per = imagecreate($percentage * 3,20);&lt;br /&gt;    $background = imagecolorallocate($per, 0x00, 0x99, 0xFF);&lt;br /&gt;    $foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);&lt;br /&gt;    $border = imagecolorallocate($per, 0x99, 0x99, 0x99);&lt;br /&gt;&lt;br /&gt;    header("Content-type: image/png");&lt;br /&gt;    imagepng($per);&lt;br /&gt; imagedestroy($per);&lt;br /&gt; }else{&lt;br /&gt; $per = imagecreate(2,20);&lt;br /&gt;    $background = imagecolorallocate($per, 0x00, 0x99, 0xFF);&lt;br /&gt;    $foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);&lt;br /&gt;    $border = imagecolorallocate($per, 0x99, 0x99, 0x99);&lt;br /&gt;&lt;br /&gt;    header("Content-type: image/png");&lt;br /&gt;    imagepng($per);&lt;br /&gt; imagedestroy($per);&lt;br /&gt; }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;DrawBar($_GET['value']);&lt;br /&gt;&lt;br /&gt;$_GET['value'] is the percentage value which we want the function to display in graphical format. You can use this script in &amp;lt;img&amp;gt; tag also. Create a file named as graph.php and save the above code in it. Then in html portion you can call the script in the following way:&lt;br /&gt;&lt;br /&gt;&amp;lt;img src="graph.php?value=75"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;img src="graph.php?value=65"&amp;gt;&lt;br /&gt;&lt;br /&gt;Easy and Useful!&lt;br /&gt;&lt;br /&gt;Happy Coding :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-4497502693954643321?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/4497502693954643321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=4497502693954643321' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4497502693954643321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4497502693954643321'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2009/02/graphical-bar-for-percentage.html' title='Graphical Bar for Percentage Values'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-6804916891204529528</id><published>2008-03-14T02:50:00.001-07:00</published><updated>2008-03-14T03:07:08.783-07:00</updated><title type='text'>Function to resize images in PHP</title><content type='html'>Very easy and useful function to resize images (of png,gif and jpg extensions) in php. I do use it often in my scripts to make the task easier.&lt;br /&gt;&lt;br /&gt;function Resize_File($file, $directory, $max_width = 0, $max_height = 0)&lt;br /&gt;  {&lt;br /&gt;      global $config;&lt;br /&gt;   &lt;br /&gt;   $full_file = $directory.$file;&lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.png$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $img = imagecreatefrompng($full_file);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.(jpg|jpeg)$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $img = imagecreatefromjpeg($full_file);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.gif$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $img = imagecreatefromgif($full_file);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      $FullImage_width = imagesx($img);&lt;br /&gt;      $FullImage_height = imagesy($img);&lt;br /&gt;      &lt;br /&gt;      if (isset($max_width) &amp;amp;&amp;amp; isset($max_height) &amp;amp;&amp;amp; $max_width != 0 &amp;amp;&amp;amp; $max_height != 0)&lt;br /&gt;      {&lt;br /&gt;          $new_width = $max_width;&lt;br /&gt;          $new_height = $max_height;&lt;br /&gt;      }&lt;br /&gt;      elseif (isset($max_width) &amp;amp;&amp;amp; $max_width != 0)&lt;br /&gt;      {&lt;br /&gt;          $new_width = $max_width;&lt;br /&gt;          $new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);&lt;br /&gt;      }&lt;br /&gt;      elseif (isset($max_height) &amp;amp;&amp;amp; $max_height != 0)&lt;br /&gt;      {&lt;br /&gt;          $new_height = $max_height;&lt;br /&gt;          $new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;          $new_height = $FullImage_height;&lt;br /&gt;          $new_width = $FullImage_width;&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      $full_id = imagecreatetruecolor($new_width, $new_height);&lt;br /&gt;      imagecopyresampled($full_id, $img, 0, 0, 0, 0, $new_width, $new_height, $FullImage_width, $FullImage_height);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.(jpg|jpeg)$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $full = imagejpeg($full_id, $full_file, 100);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.png$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $full = imagepng($full_id, $full_file);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      if (eregi(&amp;quot;\.gif$&amp;quot;, $full_file))&lt;br /&gt;      {&lt;br /&gt;          $full = imagegif($full_id, $full_file);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      imagedestroy($full_id);&lt;br /&gt;      unset($max_width);&lt;br /&gt;      unset($max_height);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;Below is a little example of its usage.&lt;br /&gt;&lt;br /&gt;Resize_File("original.jpg", 'images/', 100, 75);&lt;br /&gt;&lt;br /&gt;It will set the size of 'original.jpg', which exists in 'images' folder, to 100x75.&lt;br /&gt;&lt;br /&gt;Happy coding :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-6804916891204529528?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/6804916891204529528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=6804916891204529528' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/6804916891204529528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/6804916891204529528'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2008/03/function-to-resize-image-in-php.html' title='Function to resize images in PHP'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-1508113180378472453</id><published>2008-03-13T08:19:00.000-07:00</published><updated>2008-03-14T02:48:52.889-07:00</updated><title type='text'>Create nested (dynamic) table</title><content type='html'>In a project where i was calling the data from a mysql table, the requirement came to place it in a html table with a specific number of columns in it. Code should break the row after 3 columns (for example) and a new row of table should start after that. I wrote the following function to accomplish this task. I hope you will find it useful for your scripts.&lt;br /&gt;&lt;br /&gt;&amp;lt;?php&lt;br /&gt;function DrawTable($data,$total,$col)&lt;br /&gt;{&lt;br /&gt;$end = '0';&lt;br /&gt;$newrowcount = '0';&lt;br /&gt;?&amp;gt;&lt;br /&gt;&amp;lt;table border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;1&amp;quot; width=&amp;quot;100%&amp;quot; id=&amp;quot;AutoNumber1&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;?php while ($row = mysql_fetch_array($data)){  &lt;br /&gt;if ($newrowcount == $col)&lt;br /&gt;{&lt;br /&gt;$newrowcount = 0;&lt;br /&gt;echo "&amp;lt;tr&amp;gt;";&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;td&amp;gt;&amp;lt;?php echo $row['Name']; ?&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;?php $newrowcount++;&lt;br /&gt;  if ($newrowcount == $col) {echo"&amp;lt;/tr&amp;gt;";} &lt;br /&gt;   &lt;br /&gt;  $end = $end + 1;&lt;br /&gt;  } ?&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;?php &lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;In my example, I have a table named as 'country' having the countries of world. I will display them in html table with a sequence of 3 columns in a row. Lets start the code.&lt;br /&gt;&lt;br /&gt;&amp;lt;?php &lt;br /&gt;include "db.php";&lt;br /&gt;$result = mysql_query("select * from country order by Name") or die(mysql_error());&lt;br /&gt;$total = mysql_num_rows($result);&lt;br /&gt;DrawTable($result,$total,3);&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;br /&gt;As you noticed, I included the db file, executed the query and got its result and the total number of records it has. Then i passed the three arguments to the function. First is the record set, second is the total number of records and the third is the number of columns I want to create in a single row. Then I displayed the country name in the function among the &amp;lt;td&amp;gt; and &amp;lt;/td&amp;gt; tags. You can do your own experiments up there as this is just one example of how it can be. And of course you can easily format the html table according to your specific page layout. Possibilities are endless.&lt;br /&gt;&lt;br /&gt;If you have any question or want explanation then kindly post the comment and I will try to reply in minimum time.&lt;br /&gt;&lt;br /&gt;Thanks&lt;br /&gt;&lt;br /&gt;Happy Coding :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-1508113180378472453?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/1508113180378472453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=1508113180378472453' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1508113180378472453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1508113180378472453'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2008/03/create-nested-table.html' title='Create nested (dynamic) table'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-1748736418686736459</id><published>2008-02-27T09:49:00.001-08:00</published><updated>2009-02-07T00:14:41.253-08:00</updated><title type='text'>Random string generator in PHP</title><content type='html'>In many cases, like generating a new password or setting up the custom session id, i do use the following function often in my PHP scripts for random string generation:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Function:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;function random_string($length='8')&lt;br /&gt;{&lt;br /&gt;$ran_string = "";&lt;br /&gt;$chars = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "A", "b", "B", "c", &lt;br /&gt;&lt;br /&gt;"C", "d", "D", "e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k", "K", "l", &lt;br /&gt;&lt;br /&gt;"L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", &lt;br /&gt;&lt;br /&gt;"U", "v", "V", "w", "W", "x", "X", "y", "Y", "z", "Z");&lt;br /&gt;&lt;br /&gt;$count = count($chars) - 1;&lt;br /&gt;&lt;br /&gt;srand((double)microtime() * 1000000);&lt;br /&gt;&lt;br /&gt;for ($i = 0; $i &lt; $length; $i++)&lt;br /&gt;{&lt;br /&gt;$ran_string .= $chars[rand(0, $count)];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return($ran_string);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Usage:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$new_random_string = random_string();&lt;br /&gt;&lt;br /&gt;Happy coding :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-1748736418686736459?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/1748736418686736459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=1748736418686736459' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1748736418686736459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/1748736418686736459'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2008/02/random-string-generator-in-php.html' title='Random string generator in PHP'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-4730794114329004628</id><published>2008-02-27T09:26:00.000-08:00</published><updated>2008-02-27T09:37:33.488-08:00</updated><title type='text'>function for SQL Injection Protection</title><content type='html'>If you are using the web forms on your website to collect some user based data to put into a database then this function is right there for you to avoid any SQL injection injected by an experienced hacker. Without going into the details of hacking stuff i am putting the function here to enhance the site's security:&lt;br /&gt;&lt;br /&gt; function quote_smart($value)&lt;br /&gt; {&lt;br /&gt;     if (get_magic_quotes_gpc())&lt;br /&gt;     {&lt;br /&gt;         $value = stripslashes($value);&lt;br /&gt;     }&lt;br /&gt;   &lt;br /&gt;     if (!is_numeric($value))&lt;br /&gt;     {&lt;br /&gt;         $value = "" . mysql_real_escape_string($value) . "";&lt;br /&gt;     }&lt;br /&gt;     return $value;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;Simply scan the posted data before inserting it into database table. For example:&lt;br /&gt;&lt;br /&gt;$form_field_value = quote_smart($_POST['form_field_name']);&lt;br /&gt;&lt;br /&gt;This would help a lot am sure.&lt;br /&gt;&lt;br /&gt;Happy coding :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-4730794114329004628?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/4730794114329004628/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=4730794114329004628' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4730794114329004628'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/4730794114329004628'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2008/02/function-for-sql-injection-protection.html' title='function for SQL Injection Protection'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-337370530728832813.post-5690063052174544742</id><published>2008-02-26T08:16:00.000-08:00</published><updated>2008-02-26T21:37:16.811-08:00</updated><title type='text'>Solution of a common error  - Headers already sent!!</title><content type='html'>Since the beginning (not too long), while working on many php scripts i faced the 'Headers already sent' error many times and i was realy got stucked with that. I passed many hours on google and visited lot of forum posts but wasnt able to detect any quick solution. Some were saying to remove the blank spaces before starting of php tag and some where enforcing to do some other checks around the scripting files. Ofcourse the solutions worked for sometime but they were really time taking. Then a friend of mine, Rajneesh from india, suggested me to use a php function ob_start() while i was again stucked with same error in a script and deadline of that project was almost there. Here is the official definition i could get from php.net regarding this really useful function.&lt;br /&gt;&lt;br /&gt;''    This function will turn output buffering on. While output buffering is    active no output is sent from the script (other than headers), instead the    output is stored in an internal buffer.''&lt;br /&gt;&lt;br /&gt;This solved my problem for ever! Here is the method of its implementation.&lt;br /&gt;&lt;br /&gt;Write down the ob_start() in very beginning of the script like:&lt;br /&gt;&lt;br /&gt;&amp;lt;?php&lt;br /&gt;ob_start();&lt;br /&gt;&lt;br /&gt;And add this at the end:&lt;br /&gt;&lt;br /&gt;ob_end_flush();&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;br /&gt;And thats it. You will no more receive the 'Headers already sent' error ever on that script even if your script is sending the headers before any output.&lt;br /&gt;&lt;br /&gt;I hope this solution will help someone else too facing same problem with PHP.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/337370530728832813-5690063052174544742?l=igenic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://igenic.blogspot.com/feeds/5690063052174544742/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=337370530728832813&amp;postID=5690063052174544742' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/5690063052174544742'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/337370530728832813/posts/default/5690063052174544742'/><link rel='alternate' type='text/html' href='http://igenic.blogspot.com/2008/02/how-i-solved-common-error-headers.html' title='Solution of a common error  - Headers already sent!!'/><author><name>Waqas</name><uri>http://www.blogger.com/profile/18072606912144200119</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
