Jul 13, 2010

Uploading Photos on server with PHP

I have tried to upload pictures on my site. For that I have created one small class in PHP. I keep the name of the picture after that is uploaded. And, also, the extension. But, I transform the extension from uppercase (eg. .JPG will be .jpg).


Bellow you'll find my class. In this class I have used on small function (getextension) which I have find on the internet.

class Galerie {

function aa(){
echo 'aa';
}

function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
} //function getExtension()



function foto_upload(){
//incarcare poza pentru galeria foto
$locatie = "galerie";

$categorie = $_POST['categorie'];
$descriere = $_POST['descriere'];

//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['poza']['name']);
//get the extension of the file in a lower case format
$extension = strtolower($this->getExtension($filename));
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
$mesaj = $mesaj."
Fisierul imagine trebuie sa fie .jpg, sau .gif, sau .png
";
} else {
$numefisier = explode(".".$extension, $filename);
$numeimagine = $numefisier[0];
$newname = "../uploads/".$locatie."/".$numeimagine.".".$extension;

$copied = copy($_FILES['poza']['tmp_name'], $newname);
$fisier = $numeimagine . "." . $extension;
//echo $copied;

if(!$copied){
$mesaj = $mesaj."

Fisierul ".$fisier." nu a fost urcat pe server!";
//$imagine = "";
} else {
//salvez numele fisierului in baza de date

//mai intai calculez maximul de pozitii
if($pozitia == 0) {
$sPoz = "SELECT MAX(pozitia) AS M FROM imagini_galerie";
$rPoz = mysql_fetch_array(mysql_query($sPoz));
$pozitia = 1 + $rPoz['M']; //noua pozitie
//echo $sPoz."
".mysql_error();
}
$sql_fisier = "INSERT INTO imagini_galerie(photo, categorie, descriere, pozitia)
VALUES('".$fisier."', '" . $categorie . "', '" . $descriere . "', $pozitia)";
$rez_fisier = mysql_query($sql_fisier);

if($rez_fisier) {
echo "
Imaginea a fost incarcata!
";
//header('Location:?p=galerie');

//echo "

 

 

 

 

 

 

";
} else {
echo $sql_fisier;
echo "

 

";
echo "
Eroare la incarcarea imaginii!
";
}
} //if $copied
}

}//function foto_upload()





function foto_del($id){
//sterge imaginea din galerie
$id=(integer)$id;
$sql = "SELECT * FROM imagini_galerie WHERE id=$id";
$rnd = mysql_fetch_array(mysql_query($sql));
unlink('../uploads/galerie/'.$rnd['photo']);

$sqlSterg = "DELETE FROM imagini_galerie WHERE id=$id";
mysql_query($sqlSterg);
} //end foto_del()





} //end class Galerie


Jul 9, 2010

Analog Clock with JavaFX

Last mounth just I have commenced learning JavaFX.

I have entered on the site www.javafx.com and I have started with some tutorials. And after couple of hours I started with Analog Clock. And I have modified the code and I have saved as desktop application. And now rules like that:


My code look like this:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


/**
* @author Stefan Vilceloiu
*/

//public class Main {

import java.util.Calendar;
import java.util.Date;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Transform;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

// varies according to scene size

var width:Number = bind scene.width;
var height:Number = bind scene.height;

// list of months
var months:String[] = ["IAN", "FEB", "MAR", "APR", "MAI", "IUN",
"IUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var radius:Number = bind width/3; // Decides the main size of the clock
var centerX:Number = bind scene.width/2; // Shifting the X center
var centerY:Number = bind scene.height/2; // Shifting the Y center
var hours:Number;
var minutes:Number;
var seconds:Number;
var date:String;
var month:String;
var year:String;
var combination: String;

var time = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames: [
KeyFrame {
time: 1s
action: function() {
actionOnTick();
}
}
]
}

time.play();

//instance of calendar
function createCalendar() {
var date = new Date();
def calendar = Calendar.getInstance();
calendar.setTime(new Date()); // fix for mobile
calendar
}
// action taken on one tick
function actionOnTick () {
var calendar = createCalendar();
seconds = calendar.get(Calendar.SECOND);
minutes = calendar.get(Calendar.MINUTE);
hours = calendar.get(Calendar.HOUR);
date = String.valueOf(calendar.get(Calendar.DATE));
if(date.length() != 2) {
date = "0{date}";
}
month = months[(calendar.get(Calendar.MONTH))];
year = String.valueOf((calendar.get(Calendar.YEAR)));
combination = "{date}-{month}-{year}";
}
//scene which can be dynamic
var scene:Scene = Scene {
width: 233
height: 260
fill: Color.TRANSPARENT
content: [
Group {
translateX: bind centerX
translateY: bind centerY
content: bind [
Circle {
radius: radius + 20
fill: RadialGradient {
centerX: 0
centerY: 0
radius: radius + 20
proportional: false
stops: [

Stop {
offset: 0.9
color: Color.SILVER
},
Stop {
offset: 1.0
color: Color.BLACK
}
]
}

},
Circle {
radius: radius + 10
stroke: Color.BLACK
fill: RadialGradient {
centerX: 0
centerY: 0
radius: 90
proportional: false
stops: [
Stop {
offset: 0.0
color: Color.WHITE
},
Stop {
offset: 1.0
color: Color.ANTIQUEWHITE
}
]
}

},
Rectangle {
x: -35,
y: 2 * radius / 3 - 15
width: 71
height: 20
fill: Color.GRAY
opacity:0.4
strokeWidth: 2
stroke: Color.BLACK
arcHeight:10
arcWidth:10
},
Text {
font: Font {
size: 11
name: "Arial"
}
x: -31 ,
y: 2 * radius / 3
content: bind combination
},
Text {
font: Font {
size: 11
name: "Tahoma"
}
x: -24 ,
y: -1.21 * radius / 3
opacity:0.85
fill: Color.RED
content: "Vilceloiu"
},
for (i in [3, 6, 9, 12]) // Setting the main digits 3,6,9,12
Text {
transforms:bind [
Transform.translate(-5, 5)
]
x: radius * (( i + 0 ) mod 2 * ( 2 - i / 3))
y: radius * (( i + 1 ) mod 2 * ( 3 - i / 3))
content: "{i}"
font: Font {
size: 12
name: "Tahoma"
}
},
// making dots on rest of the place
for (i in [1..12])
if (i mod 3 != 0 ) then Circle {
transforms:Rotate {
angle: 30 * i
}
centerX: radius
radius: 4
fill: Color.BLACK
} else [ ],

// circle at the core center
Circle {
radius: 5
fill: Color.BLACK
},
// one more circle inside the above circle
Circle {
radius: 3
fill: Color.GRAY
},
// second arm
Line {
transforms: Rotate {
angle: bind seconds * 6
}
endY: -radius - 3
strokeWidth: 2
stroke: Color.RED

},
// hour arm
Path {
transforms: Rotate {
angle: bind (hours + minutes / 60) * 30 - 90
}
fill: Color.BLACK
opacity: 0.8
elements: [
MoveTo { x: 4, y: 4 },
ArcTo {
x: 4
y: -4
radiusX: 1
radiusY: 1
},
LineTo { x: radius - 23 y: 0 },
]
},
// minute arm
Path {
transforms: Rotate {
angle: bind minutes * 6 - 90
}
fill: Color.BLACK
elements: [
MoveTo { x: 4, y: 4 },
ArcTo {
x: 4
y: -4
radiusX: 1
radiusY: 1
},
LineTo{ x: radius y: 0 },
]
}
]
}
]
};

Stage {
style: StageStyle.TRANSPARENT
title: bind combination
scene: bind scene
width: 240
height: 320
}
//}



And now you can see my name on the clock and the current date in the bar (bottom)

Jul 6, 2010

MySQL tutorial - Managing database

Creating Database

To create a database in MySQL, you use the CREATE DATABASE statement as follows:
CREATE DATABASE [IF NOT EXISTS] database_name;
CREATE DATABASE statement will create the database with the given name you specified. IF NOT EXISTS is an option part of the statement, this part prevents you from error if there is a database with the given name exists on the database server. In our tutorial, for example, to create classicmodels database, you just only apply CREATE DATABASE statement above as follows:
CREATE DATABASE classicmodels;
After executing the statement, the MySQL will returns you a message to indicate whether the execution are successful or not.

Showing Databases

SHOW DATABASE statement will show all databases in your database server. You can use SHOW DATABASE statement  to check the database you've created or to see all the databases' name on the database server before you create a new database.
SHOW DATABASES;
On my database server, the output is :
+--------------------+
| Database           |
+--------------------+
| information_schema |
| classicmodels      |
| mysql              |
+--------------------+
8 rows in set (0.00 sec)

Selecting Database

To select a database which you plan to work with, you use USE statement
USE database_name;
You can select our sample database by using the USE statement as follows:
USE classicmodels;
From now you can query the tables' data and do whatever you want inside the selected database.

Removing Database

Removing database means you delete the database. All the data and related objects inside the database are permanently deleted and cannot be undone. So it is very important to execute this query with cares. To remove the database you can use DROP DATABASE statement as follows :
DROP DATABASE [IF EXISTS] database_name;
Like CREATE DATABASE statement, IF EXIST part is an optional part to prevents you from removing database which is not existed. In order to practice with DROP DATABASEstatement, you can create a temporary database, show the database on the database server, and drop it step by step as follows :
CREATE DATABASE IF NOT EXISTS temp_database;
SHOW DATABASES;
DROP DATABASE IF EXISTS temp_database;