Monday, July 23, 2012

Set index.php as default page

I was programming a Web app and I noticed that always insert address of Web app, I had to type "www.mywebapp.com/index.php" to show up a web page. If I just type the next address "www.mywebapp.com", the Web server (Apache) list the directories and some files.

I would rather type "www.mywebapp.com" and same time the Web server show up the index.php. But how can I do that? Just follow the next steps:

  1. Open notepad and write the next statement "DirectoryIndex index.php"
  2. Save file as
    • Write name of file as ".htaccess"
    • Change type of file from " Text file (*.txt)" to "All files (*.*)"

Note: You must save the file on the same folder where is "index.php". If you don't save on the same folder, it will not work correctly.

Conclusion: The DirectoryIndex command allows specify a default page to display.

Monday, July 9, 2012

How to import a MySQL dump file into a Database

To import MySQL dump file into a database you must have a MySQL command line shell.

Step #1: Make sure your database is already created. If database is not created yet, then you have to create one database. For example:

->mysql -h localhost -u root -p;
(enter your password)
->CREATE SCHEMA IF NOT EXISTS 'database_name';
->exit;

Note

You can replace "localhost" by an IP address (e.g. 192.168.1.88).

Step #2: Type the next command to import MySQL dump file into a database.

->mysql -h localhost -u root -p database_name < file.sql
(enter your password)

Voilá! Now you have a database with all data filled.

Friday, June 15, 2012

How to install drivers for the Arduino Uno with Windows7

To use Arduino Uno and upload programs to Arduino Uno with sucess, you need three steps:
  1. Plug in Arduino Uno to PC via USB;
  2. Download Arduino IDE;
  3. Install Arduino Uno Driver;

Please see the next video how to install drivers correctly.

Did you like this video? Please give me your feedback...

Friday, June 1, 2012

Decoding sign Euro in HTML & PHP


Probably you had a problem to show a Euro sign on a web page like on the next figure.


Code source:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title></title>
    </head>
    <body>
        <?php
        $str = "Price: 99,99€";
        echo htmlentities($str)."<br />";
        echo html_entity_decode($str)."<br />";
        ?>
    </body>
</html>


It happens because web page can have a charset setted UTF-8  (<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">or a ISO-8859-1 (<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">). 
To show correctly sign Euro, you must set charset ISO-8859-15 (<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15">) as you can see next picture.


Besides you set charset ISO-8859-15, you must use PHP function html_entity_decode(string) but must not use PHP function htmlentities(string).




Monday, May 28, 2012

How to create Graphs using HTML5

To develop a small web application, I would like to use the data of students' grades and translate it into a graph bar.  But how can I do that?

With HTML5, using tag <canvas>, it is possible create a graph bar without third part software. The first thing to do is write some functions in JavaScript. Before you copy code, you have to understand the coordinates on Canvas. The next figure show how coordinates are designed. It is not the same of what we learned on Math classes.


JavaScript code:

<script type="text/javascript">


        function drawLine(context,startX,startY,endX,endY,color){
            context.beginPath();
            context.moveTo(startX,startY);
            context.lineTo(endX,endY);
            context.closePath();
            context.strokeStyle=color;
            context.stroke();
        }


        function drawRectangle(context,startX,startY,width,height,fill){
            context.beginPath();
            context.rect(startX,startY,width,height);
            context.closePath();
            context.stroke();
            if(fill) context.fill();
        }


        function clearCanvas(context){
            // Store the current transformation matrix
            context.save();
            // Use the identity matrix while clearing the canvas
            context.setTransform(1, 0, 0, 1, 0, 0);
            context.clearRect(0, 0, myCanvas.width, myCanvas.height);
            // Restore the transform
            context.restore();
        }


        function drawBarChart(context,data,startX,startY,endX,
endY,chartHeight,barWidth,markDataIncrementsIn,scale){
            //draw x-axis
            drawLine(context,startX,startY,endX,startY,"#000");
            //draw y-axis
            drawLine(context,startX,startY,startX,endY,"#000");


            var maxValue=0;
            for(var i=0;i<data.length;i++){
                //extract data
                var values = data[i].split(",");
                //column name
                var name=values[0];
                //column value
                var height=parseFloat(values[1])*scale;
                //find maximum value
                if(parseInt(height)>parseInt(maxValue)) maxValue=height;
                //write data to chart
                //fill rectangle with yellow color
                context.fillStyle="#ff0";
                drawRectangle(context,startX+(i*barWidth*2),(startY-height),barWidth,height,true);
                //add column title to x-axis
                context.textAlign="left";
                //text color is black
                context.fillStyle="#000";
                context.fillText(name,startX+(i*barWidth*2),(chartHeight-25),barWidth*2);
            }


            //add some data markers to y-axis
            var numMarkers = Math.ceil(maxValue/parseInt(markDataIncrementsIn));
            context.textAlign="right";
            context.fillStyle="#000";
            var markerValue=0;
            for(var i=0;i<=numMarkers;i++){
                context.fillText((markerValue/parseInt(scale)),(startX-5),(startY-markerValue),50);
                if(i==0){
                    drawLine(context,startX,(startY-markerValue),endX,(startY-markerValue),"#000");
                }else{
                    drawLine(context,startX,(startY-markerValue),endX,(startY-markerValue),"#00f");
                }
                markerValue+=parseInt(markDataIncrementsIn);
            }
        }


        function graph(scale){
            var s=0;
            var changed=false;
            document.getElementById("range").innerHTML=scale;
            if(scale==undefined){
                s=1;
                changed=false;
            }else{
                s=scale;
                changed=true;
            }
             
            var graphCanvas = document.getElementById("myCanvas");
            //ensure that element is available within DOM
            if(graphCanvas && graphCanvas.getContext){
                //open a 2d context within canvas
                var context = graphCanvas.getContext("2d");
                //bar chart data
                var data = new Array(5);
                data[0]="Bad,2";
                data[1]="Mediocre,0.45";
                data[2]="Sufficient,5.5";
                data[3]="Good,7.2";
                data[4]="Excelent,3.9";
                //draw bar chart
                if(!changed){
                    drawBarChart(context,data,35,560,750,50,myCanvas.height,30,s,s);
                }else{
                    //clear canvas to redrawing
                    clearCanvas(context);
                    //draw bar chart
                    drawBarChart(context,data,35,560,750,50,myCanvas.height,30,s,s);
                    
                }
            }
            return true;
        }
    </script>

The second thing to do is write some HTML tags.

<body onload="graph(1);">
        <div id="title">Grades</div>
        <table>
            <tr>
                <td><canvas id="myCanvas" width="800" height="600"></canvas><td>
            <td>
                <table>
                    <tr>
                        <td>Scale : <span id="range">1</span></td>
                    </tr>
                    <tr>
                        <td><input type="range" min="1" max="501" value="1" onchange="return graph(this.value)"/></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>


The third thing to do is write CSS.

<style type="text/css">
            #title{
                text-align: center;
            }
</style>

Now put together all three parts on a single page or you can create a file to JavaScript and another to CSS and then link them on HTML page. How you can do that? Copy the next code and you can change name files.

<head>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <script type="text/javascript" src="functions.js"></script>
</head>



Attention
All this code is working on Google Chrome. Also works on Safari and Opera. If you will use Internet Explorer, you will have problems. For example, you can not use slide bar and you have to type a value instead.