January 09 2014

example of Yii autocomplete

Tagged Under : ,

Yii
Because I didn’t found any example of Yii autocomplete function. particularly showing how to fetched data via a server request.

For the widget function in view:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
  'name' => 'username',
  'value' => $model->username,
  'sourceUrl' => array('user/getusername'),//path of the controller
  'options' => array(
    'minLength' => '1', // min chars to start search
    'select' => '' // when the value selected
  ),
));

Now, we start create the getusername action in the UserController file.

January 01 2014

Tidesdk connect to MySQL Database

Tagged Under :

TideSDK
In here, I am showing you how to use TideSDK to connect MySQL Database with PHP in your applicaion.

Example Data
The example data in this post uses as below.
CREATE TABLE fruits (`id` int, `name` varchar(50));
	
INSERT INTO fruits (`id`, `name`)
VALUES (1, 'Apple'),
       (2, 'Durian'),
       (3, 'Banana'),
       (4, 'Lemon'),
       (5, 'Pear'),
       (6, 'Star fruit'),
       (7, 'Strawberry'),
       (8, 'Orange');

After that we need create a connection string with the Database. Save the code below as conn.php and put it inside the Resources folder.
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('foo', $conn);

December 30 2013

ASP.net add class to App_Code folder

Tagged Under :

asp.net
In ASP MVC4 Appliction I can add a cshtml file in app_code folder like below so that I can use my custom helper method in another cshtml.

App_Code/MyHelpers.cshtml
@helper LabelExtensions(string input) {
    "<pre>@input</pre>"
}

Using the Helper in a Page.
<p>This is some paragraph text.</p>
@MyHelpers.LabelExtensions("My Test Label.")
<p>This is some paragraph text.</p>

Output:
<p>This is some paragraph text.</p>
<pre>My Test Label.</pre>
<p>This is some paragraph text.</p>

But if the helper is classes (cs file) then you are not allow call the helper function like above. Below example show you how to use it in a Page.

December 25 2013

A better way to get item url in WordPress

Tagged Under :

Wordpress
When linking to posts or images from your posts, there is a more versatile means of doing so. Imagine that you change themes or move to another domain. You need to go through entire posts to search and replace every link.

Maybe you will think linking the posts or images like below example:
<a href="../contact/" >Contact Us</a>
But it was bad way.

This is worse as well:
<a href="http://chevronscode.com/contact/" >Contact Us</a>

December 22 2013

MySQL check for duplicates before inserting

Tagged Under : ,

MySQL
Tables or result sets sometimes contain duplicate records. Sometimes, it is allowed but sometimes it is required to stop duplicate records. Sometimes, it is required to identify duplicate records and remove them from the table. This chapter will describe how to prevent duplicate records occurring in a table and how to remove already existing duplicate records.

You are allow use PRIMARY KEY or UNIQUE Index on a table with appropriate fields to stop duplicate records. Let’s create an example table with id and name as Primary Key:
CREATE TABLE `foo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `value` int(11) NOT NULL,
  PRIMARY KEY (`id`, `name`),
);

December 20 2013

MySQL ordering by specific field values

Tagged Under : ,

MySQL
There may be times when a specific order is required in a SQL query which cannot be done using either ASC or DESC or using a special sort field. MySQL has a ORDER BY FIELD function which can be used to do this.

Example Data
The example data in this post uses as below. This is a somewhat simple table but it can be used to illustrate the point in this post quite well.
CREATE TABLE fruits (`id` int, `name` varchar(50));
	
INSERT INTO fruits (`id`, `name`)
VALUES (1, 'Apple'),
       (2, 'Durian'),
       (3, 'Banana'),
       (4, 'Lemon'),
       (5, 'Pear'),
       (6, 'Star fruit'),
       (7, 'Strawberry'),
       (8, 'Orange');

December 15 2013

Jquery highlight table row on click

Tagged Under :

jquery
To highlighter table row when user click the row. The easy way was add a css class on table “tr”.

How to add a css on “tr”? you need used JQuery to perform this action.

Before that, you need a table like below:
<div id="gridview">
  <table border="1" cellpadding="0" cellspacing="0" width="100%">
    <tr>
      <td>Row 1</td>        
    </tr>
    <tr>
      <td>Row 2</td>        
    </tr>
    <tr>
      <td>Row 3</td>            
    </tr>
    <tr>
      <td>Row 4</td>        
    </tr>
  </table>
</div>

And then add the “highlighted” css class as well
.highlighted {
    color: #261F1D
    background-color: #E5C37E;
}

December 09 2013

WordPress easy way to display the number of search results

Tagged Under :

Workpress
In prepping the new site design for my client, I need to show the total number of search results on my search page.

From the wordpress Forum I found 1 of the solution. Like the below example code.
$mySearch =& new WP_Query("s=$s & showposts=-1");
$num = $mySearch->post_count;
echo $num. "results";
But unfortunately, sometimes it still gave me the wrong count.

December 08 2013

ASP.net MVC 4 Razor WebGrid auto increase row number

Tagged Under :

asp.net
Below example showing you how to add the row number in WebGrid.

You just need add the following script inside the grid columns.
grid.Column(header: "No.", 
                format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex)

December 06 2013

ASP.net MVC Razor if else statement display output

Tagged Under :

asp.net
Not all content container blocks start with a tag element tag, though, and there are scenarios where the Razor parser cannot implicitly detect a content block.

Razor addresses this by enabling you to explicitly indicate the beginning of a line of content by using the @: character sequence within a code block. The @: sequence indicates that the line of content that follows should be treated as a content block:
@if (someCondition) {
  @: Some content to display...
}

For more practical example, the below snippet demonstrates how we could output a "Not Stock" message to our product if numbers of product is zero.
@model.product_name
@if (model.unit_stock == 0) {
  @: Not Stock
}

Because I am not wrapping the "Not Stock" message in an HTML tag element, Razor can’t implicitly determine that the content within the @if block is the start of a content block. We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.