Advertise here


Tag cloud


 

You are currently browsing the archives for the Web Development category.

Archive for the ‘Web Development’ Category

Regex link removal script

Posted on Thursday, February 16th, 2012 in Web Development

Need to remove links from your content? This regex patten should do the trick.

First you will need to choose a programming language has regex functionality and returns the properties start position, the length and group values of a string.

Then use a substring method to extract the string that matched the pattern.

Use the replace function to remove the link and use group two from the results of the regex function to put back the link text.

(<a\s*href="http://www\.sitename\.com/[a-zA-Z0-9-\?&;%=/_""\s\.]*">)([a-zA-Z0-9-\?&;%=/_"\s\.]*)(</a>)

Example below is using c#.

if (myMatchs.IsMatch(newContent))
{
      Status = "[Match]";

      try
      {
            int totalMatches = myMatchs.Matches(newContent).Count;
            Match keyMatch;
            string m;

            for (int i = 0; i < totalMatches; i++)
            {
                   keyMatch = myMatchs.Match(newContent);
                   m = newContent.Substring(keyMatch.Index, keyMatch.Length);
                   changes += " , " + m;
                   newContent = newContent.Replace(m, keyMatch.Groups[2].Value);
             }
        }
        catch (Exception ex)
        {
              Status = string.Format("[Fail] - {0}", ex.Message);
        }
}
else
{
        Status = "[No Match]";
}

A good idea would be to output the changes to a text file so you can track the modifications.

If you are replacing links in MySql database, using innodb engine use transactions so you can check the changes before committing them.


Using MVC DropDownListFor Html helper

Posted on Wednesday, January 25th, 2012 in Web Development

You might have been mislead when using DropDownListFor method when you wanted to set the selected item or it is just me. I thought you can use the SelectList function fourth parameter to set the selected value, as it says in the documentation but this does not work because data is binded to the data object.

If the object or property in the model is empty and it used as an int then it will default to zero, so the item with the value zero will be selected.

So naturally all you need to do is set the property in the model to the value you want  and it will select the correct default value in the list.

Example below:

@{
//set the username to be selected
Model.Post.UserId = Model.GetUserId;
}
//Outputs a list of userid
@Html.DropDownListFor(model => model.Post.UserId, new SelectList(Model.Users, "Id", "Name"))

Sounds so simple and straight forward now, but is amazing how something small can distract you from the obvious answer sometimes.


Set Start Index & Max Rows

Posted on Wednesday, October 5th, 2011 in Lambda Expressions

How to set a start index and max rows like in MySQL limit but using Lambda Expressions in c-sharp:

.skip() = start index

.take() = max rows

Example: If you had a list of twenty items and you only wanted to get the last five items.

var ItemList = Got.From.DataSource();
foreach(var Item in ItemList.Skip(15).Take(5)){
       //output result;
}

This saves you from querying your data source to get a set of results that might need to be displayed in different ways.


Convert int array to string array

Posted on Saturday, September 10th, 2011 in Top Tips

Quick and useful way to convert an int array to a string.

int[] ints = new[] { 2, 5, 90, 49}
string[] strings = Ids.Select(m => m.ToString()).ToArray();

What is happening? We are moving through each item in the array and converting it into a string. They will all be in a string format and it is then assigned to the string array. The same is done below except it is from a string to an int array. This can be applied to most other array types.

String array to int array


string[] strings = new[] { "1", "2", "3" };
int[] ints = strings.Select(x =&gt; int.Parse(x)).ToArray();

Make navigation elements hover over flash.

Posted on Monday, August 1st, 2011 in Top Tips

I was working on a drop down menu which unfortunately was dropping behind a flash advert, so most of the navigation items were not visible.

There are essentially two things you need to do to combat this small issue. Firstly the flash object (embed tag) needs to have wmode attribute and set to transparent. A param tag also needs to be added, setting the wmode, as seen in the example below.

<embed wmode="transparent" />
<param name="wmode" value="transparent" />

The menu class then needs the z-index set to a very high value in this case: 9 999 999 to layer above the flash item. The item also needs to have an absolute position for the z-index to take affect. The reason for using z-index is because this controls the layers level in the web page, essentially the depth. Flash objects and web forms naturally have a high z-index and this is why most elements appear below them. The simple script can be used over web forms as well, you don’t need to do anything special to the forms.

Z-index the higher the value the near to the top it will be.

.menuItems{
      position:absolute;
      z-index: 9999999;
}

Use both Apache and IIS servers

Posted on Tuesday, July 12th, 2011 in Top Tips

In some cases you might find yourself having to use Apache and IIS locally to test our your sites. Unfortunately you can’t have both running at the same time as they both use port 80.

When you install the development tools for .Net applications, IIS is installed as well by default. After this or when ever you start up your machine IIS will automatically turn on, which will prevent you from running Apache. Read more


Use MySql with c-sharp .Net

Posted on Tuesday, July 5th, 2011 in Web Development

I’m going to show how to connect, read and update a MySQL database using C-sharp .Net. You can use this for your web applications or other types of C# apps you create.

You can check out how to get your connection string from the app.config for console applications. This also applies for web applications to access the web.config. This will help you better understand the config file and how to access it. But I have made the first method in the code below get the database connection if you already aware of the web.config.

Firstly to access a MySql database you will need to download the MySQL connector dll from MySQL which you reference in your project. Read more


Developer tool Notepad ++

Posted on Friday, June 24th, 2011 in Top Tips

NotePad Plus Plus Logo free Text editor One of the most useful developer tools that I have come across while developing is NotePad ++. It is simple and lightweight so it loads up ultra quick. This is really useful when you just want to edit or create a text based document. The great thing about it is its completely free. So there is a good reason why you should download and play around with it.

Notepad ++ has some excellent features like the find and replace which can use regular expressions as well. It allows you to search with in a folder, all open documents, generate a list of where it found a match and can replace in all open files. There are plenty utilities on the find and replace making its so very useful for fast development. Read more



Web Design Essex | Richard Kotze – Web Technology, Design and Development powered by WordPress | Entries (RSS)