Pages

Monday, April 22, 2013

navbar search


  1. <form class="navbar-search pull-left">  
  2.   <input type="text" class="search-query" placeholder="Search">  
  3. </form>  

Tuesday, April 2, 2013

php format date


$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));


<? echo date(" \n l jS F Y", strtotime($data['availability'])) ?>

http://php.net/manual/en/datetime.format.php

Monday, April 1, 2013

ec2 machine


curl -XPUT 'http://ec2-184-73-28-56.compute-1.amazonaws.com:9200/blog/post/1' -d '
{
    "user": "dilbert",
    "postDate": "2011-12-15",
    "body": "Search is hard. Search should be easy." ,
    "title": "On search"
}'

scp -i PrashantKey.pem FinaleCrawler-full.jar ubuntu@ec2-184-73-28-56.compute-1.amazonaws.com:/data

http://ec2-184-73-28-56.compute-1.amazonaws.com:9200/document/post/_search

EC2 Ubuntu Instance - UNPROTECTED PRIVATE KEY FILE


UNPROTECTED PRIVATE KEY FILE!  
permissions 0644 for 'xxxxx.pem' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: xxxxx.pem
Permission denied (publickey).

Private keys must be readable only by the owner ..
Do chmod 400 xxxxx.pem on the machine from which you're connecting

scp (secure copy) to ec2 instance without password


scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz ec2-user@mec2-50-17-16-67.compute-1.amazonaws.com:~/.

Saturday, March 16, 2013

You should always issue a redirect for successful POST requests


The reason: if a user hits “Refresh” on a page that was loaded via POST, that request will be repeated. This can often lead to undesired behavior, such as a duplicate record being added to the database – or, in our example, the e-mail being sent twice. If the user is redirected to another page after the POST, then there’s no chance of repeating the request.
You should always issue a redirect for successful POST requests. It’s a Web development best practice.

blank=True null=True

So that’s a long way of saying this: if you want to allow blank values in a date field (e.g., DateFieldTimeField,DateTimeField) or numeric field (e.g., IntegerFieldDecimalFieldFloatField), you’ll need to use bothnull=True and blank=True.

Friday, March 15, 2013

Adding your models to the Admin Interface


from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

Indent error def __unicode__(self):


Your exact indentation you've pasted wouldn't throw that error, but in your real code, you must have thedef __unicode__ line at the exact indent depth as the other lines in your model.
Make sure you are using spaces and not tabs for all of your indents, as tabs can sometimes make the indent level seem the same as the others.

No module named books

Thanks for the help everyone. I was using 1.4.x, not the 1.0 the book is based on. After fooling around with the commands for a bit, I was able to figure out that the problem was in the INSTALLED_APPS section - I included 'mysite.books', but the new way to do that is just 'books'.

Instead of mysite.books in the INSTALLED_APPS write books

sqlite3.OperationalError: unable to open database file

I'm a beginner, too, and had a similar problem. The .db file will created for you when you run python manage.py syncdb. From your terminal window, just be sure that you're located in the same folder as the manage.py file in your directory structure. From this location, I was able to successfully create the necessary tables. The settings.py file will have already been created when you initially set the project up.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydata.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

Database will be created once you do python manage.py syncdb

Finally to check if everything is fine

from django.db import connection
cursor = connection.cursor()

If nothing happens, then your database is configured properly. Otherwise, check the error message for clues about what's wrong.

Monday, March 4, 2013

ls -lh
finding out the sizes of different files in a folder

wc -l
finding out the number of lines in a file

head -10
top 10 lines of a file




Wednesday, February 27, 2013

Sharing files in Linux!

python -mSimpleHTTPServer xxxx (xxxx- port number) to start HTTP Server at current working directory. Quite handy for sharing files/directories.

Friday, February 15, 2013

Pass a PHP array to a JavaScript function


Use JSON.
In the following example $php_variable can be any PHP variable.
<script type="text/javascript">
    var obj = <?php echo json_encode($php_variable); ?>;
</script>
In your code, you could use like the following:
drawChart(600/50, <?php echo json_encode($day); ?>, ...)
In cases where you need to parse out an object from JSON-string (like in an AJAX request), the safe way is to use JSON.parse(..) like the below:
var s = "<JSON-String>";
var obj = JSON.parse(s);

Thursday, February 14, 2013

$('#urlresults .concept-name').each(function(){$(this).html()});

$('#urlresults .concept-name').each(function (){if ($(this).html() === 'tarot readings') {$(this).html("test")}});

$('#urlresults .concept-name').each(function (){if ($(this).html() === 'test') {$(this).next(".block").html("block")}});

$('#urlresults .concept-name').each(function (){if ($(this).html() === 'bikini photos') {$next = $(this).next(".block"); $next.html($next.attr('data-tld'))}});

$('#urlresults .concept-name').each(function (){if ($(this).html() === 'bikini photos') {$next = $(this).next(".block"); $next.html(generateUnblockConceptLink($next.attr('data-tld'), $next.attr('data-basis'), $next.attr('data-admin-id'), $next.attr('data-url')))}});


Saturday, February 9, 2013

Comma Rupees php


<?php
function my_money_format($number)
{
    if(strstr($number,"-"))
    {
        $number = str_replace("-","",$number);
        $negative = "-";
    }

    $split_number = @explode(".",$number);

    $rupee = $split_number[0];
    $paise = @$split_number[1];

    if(@strlen($rupee)>3)
    {
        $hundreds = substr($rupee,strlen($rupee)-3);
        $thousands_in_reverse = strrev(substr($rupee,0,strlen($rupee)-3));
        for($i=0; $i<(strlen($thousands_in_reverse)); $i=$i+2)
        {
            $thousands .= $thousands_in_reverse[$i].$thousands_in_reverse[$i+1].",";
        }
        $thousands = strrev(trim($thousands,","));
        $formatted_rupee = $thousands.",".$hundreds;

    }
    else
    {
        $formatted_rupee = $rupee;
    }

    if((int)$paise>0)
    {
        $formatted_paise = ".".substr($paise,0,2);
    }

    return $negative.$formatted_rupee.$formatted_paise;

}
?>

http://php.net/manual/en/function.number-format.php

Change value of a select list using onclick in href


<form>
      <select id="action">
         <option value="a">Add</option>
         <option value="b">Delete</option>
      </select>
 </form>

<a href="#" onClick="document.getElementById('action').value='a'">Add</a> 
<a href="#" onClick="document.getElementById('action').value='b'">Delete</a> 

Tuesday, February 5, 2013



<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
function fetchKeywords(url,count,impressions){

$.ajax({

'url' : 'getData2.php',
'type' : 'GET',
'data' : {
'count':count,
'impressions':impressions,
'url':url
},

'success' : function(data) {


var div = document.getElementById('urlresults');

div.innerHTML = div.innerHTML + data;
}

});
 
 
   
  }</script>

Ajax Tutorial to fetch data from other URL


  • url - The target of the request.
  • type - The type of HTTP request either: "GET" (Default) or "POST".
    Difference between GET and POST.
  • async - Set asyncronous to TRUE if you want it to load in background and this will allow you to run mutiple AJAX requests at the same time. If you set to FALSE the request will run and then wait for the result before preceeding with the rest of you code.
  • data - Specify data you wish to pass with the AJAX call in "{param1: 'value1'}" format.
  • dataType - Specify the type of data that is returned: "xml/html/script/json/jsonp".
  • success - The function that is fired when the AJAX call has completed successfully.
  • error - The function that is fired when the AJAX call encountered errors.
  • jsonp - Allows you to specify a callback function when making cross domain AJAX calls.
  • timeout - You can also set a timeout on the AJAX call in milliseconds.
//Listen when a button, with a class of "myButton", is clicked
//You can use any jQuery/JavaScript event that you'd like to trigger the call
$('.myButton').click(function() {
//Send the AJAX call to the server
  $.ajax({
  //The URL to process the request
    'url' : 'page.php',
  //The type of request, also known as the "method" in HTML forms
  //Can be 'GET' or 'POST'
    'type' : 'GET',
  //Any post-data/get-data parameters
  //This is optional
    'data' : {
      'paramater1' : 'value'
      'parameter2' : 'another value'
    },
  //The response from the server
    'success' : function(data) {
    //You can use any jQuery/JavaScript here!!!
      if (data == "success") {
        alert('request sent!');
      }
    }
  });
});

Saturday, February 2, 2013

Making xampp to run properly on my windows machine


short_open_tag = On
error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT

Friday, February 1, 2013

php short tags problem

http://stackoverflow.com/questions/435705/why-is-no-longer-working-and-instead-only-php-works

Thursday, January 31, 2013

Formatting XML in php


For xml formatted with attributes...
data.xml:
<building_data>
<building address="some address" lat="28.902914" lng="-71.007235" />
<building address="some address" lat="48.892342" lng="-75.0423423" />
<building address="some address" lat="58.929753" lng="-79.1236987" />
</building_data>
php code:
$reader = new XMLReader();

if (!$reader->open("data.xml")) {
    die("Failed to open 'data.xml'");
}

while($reader->read()) {
  if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'building') {
    $address = $reader->getAttribute('address');
    $lattitude = $reader->getAttribute('lat');
    $longitude = $reader->getAttribute('lng');
}

$reader->close();