1

Create Twitter Counter Badge using CodeIgniter

Posted by Raihan Iqbal on Dec 2, 2009 in CodeIgniter, PHP |

I know this is one of those “re-inventing the wheel’” kind of tutorials but I had nothing better to do during my Eid vacation :-P

Things you will need

1. Download and install the excellent CodeIgniter Curl Library

2. You will need an image which will contain your follower count.

The Code

In your Controller class add the following methods

function MyTwitterCounterBadge($username)
{
    $this->load->library('image_lib');
    $config['source_image'] = './images/YOUR_BUTTON_IMAGE.gif';
    $config['wm_text'] = $this->_getTwitterCounter($username);
    $config['wm_type'] = 'text';
    $config['wm_font_path'] = './system/fonts/arlrdbld.ttf';
    $config['wm_font_size'] = '12';
    $config['wm_font_color'] = 'ffffff';
    $config['wm_vrt_alignment'] = 'top';
    $config['wm_hor_alignment'] = 'center';
    $config['wm_hor_offset'] = '10';
    $config['wm_vrt_offset'] = '10';
    $config['dynamic_output'] = 'TRUE'; // DO NOT CHANGE

    $this->image_lib->initialize($config);

    $this->image_lib->watermark();
}

function _getTwitterCounter($username)
{
    $count = 0;
    $this->load->library('curl');

    $json = $this->curl->simple_get("http://twitter.com/users/show/$username.json");
    if( $json )
    {
        $obj = json_decode($json);
        $count = $obj -> {'followers_count'};
    }

    return $count;
}

 

Change the values of wm_font_path, wm_font_size, wm_font_color, wm_vrt_alignment, wm_hor_alignment, wm_hor_offset and wm_vrt_offset to fit your needs. Please refer to the CodeIgniter user guide to know more about these settings.

Now all you have to do is include an image tag in your view page that points to the controller function above

<img src="<?=BASE_URL?>CONTROLLER_NAME/MyTwitterCounterBadge/TWITTER_USER_NAME">

 

And that’s all there is to it. Now you have your own personalized twitter counter badge!

Happy Programming!

Tags: , , , , ,

 
3

Using MsSqlSpatial with Google Maps

Posted by Raihan Iqbal on Aug 26, 2009 in Development, Map, T-SQL, Tools |

One of the many .NET open-source projects that don’t get the attention they deserve is MsSqlSpatial. This project provides spatial extensions to SQL Server 2005 – meaning you can perform various spatial calculations right in your T-SQL queries! Cool eh? But, this project lacks documentation which is a problem for us developers. I’m sure they are working their ass off to make one but in the mean time we have to play around with the code to understand its inner workings.

Since Google Maps is very popular among developers, and my personal favorite, I thought why not give out some tips.

Spatial Reference Identifiers (SRIDs)

Most of their functions, require this as a parameter. If your GIS application uses Google Maps then you should use 4326.

Code Examples

Convert a Point on the map from a Well-Known-Text(WKT) format to a Well-Known-Binary Format:

SELECT ST.PointFromText('POINT(-87.6673250 41.9489650)', 4326)

The Point should be of the format POINT( longitude latitude ).

Polygon to text:

SELECT [Bloggernate].[ST].[PolygonFromText] (
   'POLYGON((-87.71956443786621 41.96727630029047, 
             -87.64369010925293 41.96765920367816, 
             -87.6309871673584 41.920672548686824, 
             -87.72522926330566 41.91939525416699, 
             -87.71956443786621 41.96727630029047))'
   ,4326)

The polygon must be a closed one otherwise you’ll receive an exception.

Check if a polygon contains a specified point:

SELECT [ST].[Contains] (YOUR_WKB_POLYGON, YOUR_WKB_POINT)

I hope this provides a good starting point for those daring to step into the unknown ;)

Happy Programming!

Tags:

 
0

Using Jquery Grid with ASP.NET MVC

Posted by Raihan Iqbal on Jul 28, 2009 in .net, asp.net |

This excellent article from Phil Haack shows you how you can use the excellent JGrid plugin with ASP.NET MVC.

Thanks Phil!

http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx

 
2

ASP.NET Maintenance Mode Module

Posted by Raihan Iqbal on Jun 22, 2009 in .net, Tools, asp.net |

AM3Web developers at times need to put their web applications in an offline mode so as to prevent visitors from accessing the site while they are updating it. The classic way to do this would be to upload an app_offline.html file to the root of the web application. The limitation of this technique is that even the developer cannot visit the site! A developer might need to ensure if everything is working fine and at the same time prevent other users from accessing the web site.

So I created an HTTP module that helps overcome this issue. Just place a few lines in your web.config file and you are ready to go!

 

The following are the main features of this module:

1. Turn on/off the maintenance mode using a setting in the web.config file.

2. Specify the path to the landing page that will be shown to visitors.

3. Specify path to the login page if available.

4. Specify the allowed user roles.

5. Specify the allowed users.

Since the module is in its primary stage, please report any issues that you may find.

Resources

 
2

FireFox+Firebug vs IE8 Developer Toolbar

Posted by Raihan Iqbal on Jun 21, 2009 in Development, Tools |

The first time I used Firebug (back then there was no IE8 Dev toolbar) I fell in love with it and as a Web Developer, life without it is unimaginable. This excellent article by Chris Brandsma tells you why its so. Check it out here.

 
1

How to add a tooltip to an ASP.NET DropdownList item

Posted by Raihan Iqbal on Jun 16, 2009 in Uncategorized |

If you want to set a common tooltip for all the items in a DropDownList then you can just set the Title attribute for each item like so:

foreach(ListItem item in ddlScorecards.Items)
    item.Attributes.Add("Title", "YOUR TOOLTIP TEXT");

But if you want to set unique tooltips for each item then you have wrap the text value of the items in a DIV.

foreach(ListItem item in ddlScorecards.Items)
  item.Text = String.Format("<div title='{0}'>{1}</div>", "YOUR_TOOLTIP", item.Text);

Happy Programming!

 
0

Programmatically extract body of a web page

Posted by Raihan Iqbal on Jun 16, 2009 in .net, asp.net, c# |

The technique used in this article can be used to display the contents of an HTML page inside your ASP.NET pages instead of using IFRAMES.

System.Net.WebRequest req = System.Net.WebRequest.Create(Request.QueryString["url"]);
System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse();
System.IO.Stream respStream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(respStream);
string responseFromServer = sr.ReadToEnd();
System.Text.RegularExpressions.Regex bodyRegex = new System.Text.RegularExpressions.Regex(@"(]*>[\u0000-\uFFFF]+?)");
System.Text.RegularExpressions.Match bodyMatch = bodyRegex.Match(responseFromServer);
Literal1.Text = bodyMatch.Result("$0");
sr.Close();
respStream.Close();
resp.Close();

Thanks to ClayCo at the ASP.NET Forums for his help. Here is the link to the original thread http://forums.asp.net/t/1023144.aspx

 
0

Perform Batch Inserts/Updates in SQL Server 2005 using XML

Posted by Raihan Iqbal on Apr 18, 2009 in Development, T-SQL |

My current project requires me to download huge amounts of data from a remote server in the form of XML and save them to our local database. Lets see if we can do this using just one simple T-SQL stored procedure. Lets first create our table. For the purpose of this demonstration I will use a simple schema with the following signature

   1:  CREATE TABLE [dbo].[Employee](
   2:      [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
   3:      [Name] [nvarchar](50) NOT NULL,
   4:      [Gender] [varchar](1) NOT NULL,
   5:      [DateOfBirth] [datetime] NOT NULL,
   6:   CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
   7:  (
   8:      [EmployeeID] ASC
   9:  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  10:  ) ON [PRIMARY]

Next, we need to create the stored procedure that will do the batch insert for us.

CREATE PROCEDURE [dbo].[spr_Employee_ProcessXmlData](@employees xml)
AS
BEGIN
    DECLARE @TempTable TABLE
    (
        [Name] [nvarchar](50),
        [Gender] [varchar](1),
        [DateOfBirth] [datetime]
    );

    -- Populate the Temporary table from the XML
    INSERT INTO @TempTable (Name, Gender, DateOfBirth)
        SELECT
                ParamValues.Data.value('Name[1]','nvarchar(50)'),
                ParamValues.Data.value('Gender[1]','varchar(1)'),
                ParamValues.Data.value('DateOfBirth[1]','datetime')
        FROM
            @employees.nodes('NewDataSet/Employee') as ParamValues(Data);

    INSERT INTO Employee(Name, Gender, DateOfBirth)
        SELECT T.* FROM @TempTable T WHERE T.Name NOT IN (SELECT Name FROM Employee)
END

I won’t go into details about the XML programming – this great article are among several others that provide a good start for those unfamiliar with this feature of T-SQL. The stored procedure first creates a temporary in-memory table which will hold the data from the XML. Each node is read from the XML and mapped to the appropriate column of the temporary table. The temporary table is merely used to prevent duplicate data from being inserted into our actual table. If you are certain that your XML will never contain redundant data, then you can skip this step and directly insert the data to the Employee table.

We are now ready to perform batch inserts! The following SQL script demonstrates how.

EXEC    [dbo].[spr_Employee_ProcessXmlData]
        @employees = '<NewDataSet>
                        <Employee>
                            <Name>Raihan Iqbal</Name>
                            <Gender>M</Gender>
                            <DateOfBirth>11-05-1983</DateOfBirth>
                        </Employee>
                        <Employee>
                            <Name>John Doe</Name>
                            <Gender>M</Gender>
                            <DateOfBirth>10-05-1972</DateOfBirth>
                        </Employee>
                    </NewDataSet>'

It is needless to say that we can generate the XML and execute the stored procedure using any .NET language.

Happy programming!

 
0

Look Before You Leap – Episode 1

Posted by Raihan Iqbal on Mar 25, 2009 in Islam |

As-salamu-alaikum brothers/sisters

Many of us Alhamdulillah are muslims by birth and thus we "tend" to learn Islam through our peers. But before we do something under the tag of Islam we never ask ourselves the following questions:

  1. What????
  2. Did our prophet PBUH do it? Did any of the prophets do it?!?!
  3. If others are doing it? Why are they doing it?

I’ve decided that I’m going to discuss with my fellow Muslims about one such topic in each episode and I would appreciate if I get some feedback.

One such interesting practice is Arba’een (Arabic: اربعين‎, means "forty") or Chehlum or Chaliswa or Chollisha
I asked myself the above three questions and found the following:

What??
————–
Lets see what the Encyclopedia has to say. This is a quotation from Wikipedia:

Arba’een (Arabic: اربعين‎, means "forty"), or Chehlum, as it is known by Urdu-speaking Muslims, is a Shi’a Muslim religious observation that occurs 40 days after the Day of Ashura, the commemoration of the martyrdom of Husayn bin Ali, the grandson of the Prophet Muhammad which falls on the 20th day of the month of Safar.

To read more follow this link – http://en.wikipedia.org/wiki/Chehlum

Did our prophet PBUH do it? Did any of the prophets do it?!?!
—————————————————————————————–
I couldn’t find anything from Qur’an/Hadith that supports this and this is what the scholars have to say:
http://www.islamweb.net/ver2/Fatwa/ShowFatwa.php?Option=FatwaId&lang=E&Id=83604
http://www.islam-qa.com/en/ref/5871/40%20days

If others are doing it? Why are they doing it?
————————————————————

Culture, tradition, ignorance and the list goes on. What do you think? Please feel free to leave your comments and suggestions below. I hope we can create a platform where we can discuss such “ignored” yet serious topics. As Muslims we cannot afford to turn a blind eye to this otherwise we will be held accounted for. Once again don’t take everything in this post for granted. Do your own research, ask the scholars around you and when you finally find the truth, share it with us and your family members.

Allah knows best

 
0

Generating Tree-like UI structures

Posted by Raihan Iqbal on Feb 25, 2009 in .net, Development, c# |

In many of my projects I have had to generate non-binary trees using information from the database and then display them in a “tree-like” structure as shown below.

blog

My algorithm is very simple and it makes use of Dan Vanderboom’s excellent Generic Tree Collection but frankly I’m not satisfied with certain aspects of my algorithm. The main issue – the data can never be retrieved in the same order as shown above from the database.

Code

Now that we have a collection ready, we can generate the tree structure

Code

Finally, I add the items to my drop-down control by calling 

GenerateDropDownList(rootCategory, 0);

Let’s have a look at the function

blog4

I know I could have used LINQ but many of my projects are running in .NET 2.0 so I had to go old-school! I’m looking forward to your valuable comments as to how YOU would do this and why your solution is more efficient than mine.

Happy Programming!

kick it on DotNetKicks.com

Raihan Iqbal is proudly powered by WordPress and Siteslike