Avoid app from crashing during the loop – C#

I was new on C#.Net when i tried to create an app for scraping websites. At first i am using a single thread to handle loop logic which causes the app to crash several times if it involves a huge amount of data and leave all activity on foreground.

C# has background worker which can help avoid system crashes and more resource wise.

check this simple code , this shows how background workers being used

Laravel 404

From your web development using Laravel you may experience this issue as you setup a fresh web application run by apache and using virtual host for your http configurations. Defined routes doesn’t work but you think you set it right. On your virtual host configuration (.conf file) you put

AllowOverride All

but still you are getting a 404 error

the solution is, be sure to enable mod_rewrite

 

sudo a2enmod rewrite

then restart your apache

sudo service apache2 restart

This should fix the issue

Note: This is on linux implemetation

I experienced this issue when i tried to setup AsgardCMS (a laravel CMS) on a fresh/new server, took me a long time to discover the issue that’s why i am posting this note just encase.

 

Using Google Sheet as Database

One way to show the data to your client with read only(depends on you) access accessible through web/mobile without creating new interface. Google Sheet is one of the solutions you can pick. Google sheet provides security features, history etc.. which make stuffs easier.

Using GSHEET as a DB is probably not a good idea. This is just an experiment (You might lose your hair)

On this article I will teach you how to Integrate Google Sheet as your database with PHP engine. LET’S GET STARTED!

NOTE
We need Google Developer Console Account for this

  1. On your google developer console account create a new project , name it whatever you want 😉 .
  2. Once it is successfully created. Click on credentials , then create credentials. Select service account key  cc
  3. Then you will be redirected to this page(see image below).
    cre
  4.  On service account select New service then fill out Service account name(input whatever service name you want). Choose P12 as Key type
  5. Then hit Create!
  6. After it is created .p12 file should be automatically downloaded(example file: whateveryouwant-2.p12 – dont lose this file we will gonna use this for Google Service API)  and pop out look like this(see image below). Note on the private key “notasecret” we will be using this one later. (BTW You can hit close)pop
  7. After closing the modal you will be Service Accounts page.sap Please note on the service account ID from the service account we created. It’s an email address format right? This we will use this email address later.
  8. Next step is to create your own GOOGLE SHEET (now here we go setting up our DB).
  9. Done creating your own GOOGLE SHEET?
  10.  Hold up! dont close the sheet. Click on share.
  11.  Now share it to the service account we created on step #7. (Input the service account ID in this case whateveryouwant@whateveryouwant.iam.gserviceaccount.com)INTEGRATING the connection with PHP using google service API will be in  my next article 😉

ZMQ on Xampp

I have an app run by laravel and experienced this error when i run composer install command from CLI.

Problem 1
 - Installation request for react/zmq v0.3.0 -> satisfiable by react/zmq[v0.3.0].
 - react/zmq v0.3.0 requires ext-zmq * -> the requested PHP extension zmq is missing from your system.
To enable extensions, verify that they are enabled in those .ini files:
 - C:\xampp\php\php.ini
 You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Here are some awesome ways to deal with that issue.
1.  Download all dll here http://pecl.php.net/package/zmq to your matching OS.
Make sure you got the right one (x86 or x64) and that it is either thread safe or non-      thread safe. How would you know? Time to play DLL roulette.
2. Copy libzmq.dll to your ~/xampp/php/
3. Copy php_zmq.dll to your php extension directory (eg. ~/xampp/php/ext/)
4. Then add this to your php.ini extension=php_zmq.dll

 

Javascript search item from an array of objects

Using jquery grep to search an object from an array with a value of a specific key.

For example searching like mysql 'like'
var restaurants [{id:1, restname: 'Cafe Namo'},{id:2,restname:'TefeCool'},{id:3,restname:'Ivans Resto'}];

var query_string = 'fe';


$.grep(restaurants, function (e) {
 var search = new RegExp(query_string, "i");
 return search.test(e.restname);
});

Return value is
[{id:1, restname: 'Cafe Namo'},{id:2,restname:'TefeCool'}]

Google Maps Address Interaction(postcode) using

I am using jquery, html5 for this example.

HTML

Load the google maps api link

https://maps.googleapis.com/maps/api/js?sensor=true

This will be the container of the google maps Note: Set height for your google map container.

JS

var map;

var geocoder;

//This function intialize the google maps settings

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
        zoom: 16, center: latlng, disableDoubleClickZoom: true
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

//This function sets the address for the google map
function codeAddress(address) {
    geocoder.geocode({'address': address}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        } 
    });
}

//now its time to load the google maps

$(document).ready(function(){

     initialize();
     codeAddress('600'); //set default postcode

});


Enable Cross Domain AJax File Upload using jquery file upload plugin

WHAT TO DO FIRST?
1. Enable Cross Domain Ajax Request

2.  Download Blueimp file upload. (I will be using this jquery file upload plugin for my ajax upload).

START

1. First thing first is to initialize your file upload on the client side.

//html
<input id="fileupload" type="file" name="file">


//javascript
jQuery("#fileupload").fileupload({
    url: //url for upload handler,
    datatype: 'json',
    xhrFields: {
        withCredentials: true
    },
    done: function (e, data) {
       //code for done uploading callback
    }
})

As you can see above i set withCredentials to true which will help me for xdomain request

And for the server side (i use php for this one)

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    return YOURRESTRESPONSE('OK');
}

if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) { // handle upload heare}

You should be wondering why there is a blocking for REQUEST METHOD “OPTION” because it will validate if the request is authentic which will depend on the response header YOURRESTRESPONSE is a custom function i created for modified header response. The functin contains modifying

Access-Control-Allow-Credentials your_other_origin
Access-Control-Allow-Origin  'true'
Access-Control-Allow-Methods  'POST PUT DELETE GET OPTIONS'

Above Properties are stated on Enable Cross Domain Ajax Request

If YOURRESTRESPONSE is success. Blueimp ajax upload will automatically send another request for file upload.
And after that your upload file should be working.