<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>隨機產生亂數</title>
<Script type ="text/javascript">
var a=0,b=0,c=0;
var minNum = 0;
var maxNum = 6;
a = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
if(b==a)
{b=b+1;}
if(c==a)
{c=c+2;}
if(c==b)
{c=c+1;}
document.write("亂數a:",+a,"亂數b:",+b,"亂數c:",+c);
</Script>
</head>
<body>
<h2>亂數不重複</h2>
</body>
</html>
2016年6月3日 星期五
2016年6月2日 星期四
亂數不重複
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>隨機產生亂數</title>
<Script type ="text/javascript">
var a=0,b=0,c=0;
var minNum = 0;
var maxNum = 9;
a = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
while(b==a)
{b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
while(c==a )
{c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
while(c==b )
{c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
document.write("亂數a:",+a,"亂數b:",+b,"亂數c:",+c);
</Script>
</head>
<body>
<h2>亂數不重複</h2>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>隨機產生亂數</title>
<Script type ="text/javascript">
var a=0,b=0,c=0;
var minNum = 0;
var maxNum = 9;
a = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
while(b==a)
{b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
while(c==a )
{c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
while(c==b )
{c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;}
document.write("亂數a:",+a,"亂數b:",+b,"亂數c:",+c);
</Script>
</head>
<body>
<h2>亂數不重複</h2>
</body>
</html>
亂數可重複
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>隨機產生亂數</title>
<Script type ="text/javascript">
var a=0,b=0,c=0;
var minNum = 0;
var maxNum = 9;
a = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
document.write("亂數a:",+a,"亂數b:",+b,"亂數c:",+c);
</Script>
</head>
<body>
<h2>document.write用法</h2>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>隨機產生亂數</title>
<Script type ="text/javascript">
var a=0,b=0,c=0;
var minNum = 0;
var maxNum = 9;
a = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
b = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
c = Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
document.write("亂數a:",+a,"亂數b:",+b,"亂數c:",+c);
</Script>
</head>
<body>
<h2>document.write用法</h2>
</body>
</html>
2016年5月13日 星期五
單選+圓餅圖
<!DOCTYPE html ><html >
<head>
<title>
Form
</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var a;
var b;
var c;
var total;
function check1() {
a=0;
b=0;
c=0;
total=0;
var radio1=document.getElementsByName("v1");
var radio2=document.getElementsByName("v2");
var radio3=document.getElementsByName("v3");
var radio4=document.getElementsByName("v4");
var radio5=document.getElementsByName("v5");
var radio6=document.getElementsByName("v6");
var radio7=document.getElementsByName("v7");
var radio8=document.getElementsByName("v8");
var radio9=document.getElementsByName("v9");
var radio10=document.getElementsByName("v10");
var flag=new Array();
for(var i=0;i<radio1.length;i++){
if(radio1.item(i).checked==true) {
flag[1]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio2.length;i++){
if(radio2.item(i).checked==true) {
flag[2]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio3.length;i++){
if(radio3.item(i).checked==true) {
flag[3]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio4.length;i++){
if(radio4.item(i).checked==true) {
flag[4]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio9.length;i++){
if(radio9.item(i).checked==true) {
flag[9]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio5.length;i++){
if(radio5.item(i).checked==true) {
flag[5]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio10.length;i++){
if(radio10.item(i).checked==true) {
flag[10]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio6.length;i++){
if(radio6.item(i).checked==true) {
flag[6]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio7.length;i++){
if(radio7.item(i).checked==true) {
flag[7]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio8.length;i++){
if(radio8.item(i).checked==true) {
flag[8]=i+1;
total=total+1;
break;
}
}
if(total!=10){alert("請選十個選項,少選或多選都剁了你")}
if(total==1){alert("少選9個")}
if(total==2){alert("少選8個")}
if(total==3){alert("少選7個")}
if(total==4){alert("少選6個")}
if(total==5){alert("少選5個")}
if(total==6){alert("少選4個")}
if(total==7){alert("少選3個")}
if(total==8){alert("少選2個")}
if(total==9){alert("少選1個")}
for(var i=1;i<11;i++)
{
if(flag[i]==1)
{a=a+1;}
}
for(var i=1;i<11;i++)
{
if(flag[i]==2)
{b=b+1;}
}
for(var i=1;i<11;i++)
{
if(flag[i]==3)
{c=c+1;}
}
drawChart()
}
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['one', a],
['two', b],
['three', c]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<form action="post.php" method="post" name="add" enctype="multipart/form-data" onSubmit="return check();" >
<h1>1、ssss?</h1>
<input name="v1" type="radio" value="1">1
<input name="v1" type="radio" value="2">2
<input name="v1" type="radio" value="3">3
<input type="button" value="submit" onclick="check1()"/>
<h1>2、ssss?</h1>
<input name="v2" type="radio" value="1">1
<input name="v2" type="radio" value="2">2
<input name="v2" type="radio" value="3">3
<h1>3、ssss?</h1>
<input name="v3" type="radio" value="1">1
<input name="v3" type="radio" value="2">2
<input name="v3" type="radio" value="3">3
<h1>4、ssss?</h1>
<input name="v4" type="radio" value="1">1
<input name="v4" type="radio" value="2">2
<input name="v4" type="radio" value="3">3
<h1>5、ssss?</h1>
<input name="v5" type="radio" value="1">1
<input name="v5" type="radio" value="2">2
<input name="v5" type="radio" value="3">3
<h1>6、ssss?</h1>
<input name="v6" type="radio" value="1">1
<input name="v6" type="radio" value="2">2
<input name="v6" type="radio" value="3">3
<h1>7、ssss?</h1>
<input name="v7" type="radio" value="1">1
<input name="v7" type="radio" value="2">2
<input name="v7" type="radio" value="3">3
<h1>8、ssss?</h1>
<input name="v8" type="radio" value="1">1
<input name="v8" type="radio" value="2">2
<input name="v8" type="radio" value="3">3
<h1>9、ssss?</h1>
<input name="v9" type="radio" value="1">1
<input name="v9" type="radio" value="2">2
<input name="v9" type="radio" value="3">3
<h1>10、ssss?</h1>
<input name="v10" type="radio" value="1">1
<input name="v10" type="radio" value="2">2
<input name="v10" type="radio" value="3">3
</form>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
<head>
<title>
Form
</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var a;
var b;
var c;
var total;
function check1() {
a=0;
b=0;
c=0;
total=0;
var radio1=document.getElementsByName("v1");
var radio2=document.getElementsByName("v2");
var radio3=document.getElementsByName("v3");
var radio4=document.getElementsByName("v4");
var radio5=document.getElementsByName("v5");
var radio6=document.getElementsByName("v6");
var radio7=document.getElementsByName("v7");
var radio8=document.getElementsByName("v8");
var radio9=document.getElementsByName("v9");
var radio10=document.getElementsByName("v10");
var flag=new Array();
for(var i=0;i<radio1.length;i++){
if(radio1.item(i).checked==true) {
flag[1]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio2.length;i++){
if(radio2.item(i).checked==true) {
flag[2]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio3.length;i++){
if(radio3.item(i).checked==true) {
flag[3]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio4.length;i++){
if(radio4.item(i).checked==true) {
flag[4]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio9.length;i++){
if(radio9.item(i).checked==true) {
flag[9]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio5.length;i++){
if(radio5.item(i).checked==true) {
flag[5]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio10.length;i++){
if(radio10.item(i).checked==true) {
flag[10]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio6.length;i++){
if(radio6.item(i).checked==true) {
flag[6]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio7.length;i++){
if(radio7.item(i).checked==true) {
flag[7]=i+1;
total=total+1;
break;
}
}
for(var i=0;i<radio8.length;i++){
if(radio8.item(i).checked==true) {
flag[8]=i+1;
total=total+1;
break;
}
}
if(total!=10){alert("請選十個選項,少選或多選都剁了你")}
if(total==1){alert("少選9個")}
if(total==2){alert("少選8個")}
if(total==3){alert("少選7個")}
if(total==4){alert("少選6個")}
if(total==5){alert("少選5個")}
if(total==6){alert("少選4個")}
if(total==7){alert("少選3個")}
if(total==8){alert("少選2個")}
if(total==9){alert("少選1個")}
for(var i=1;i<11;i++)
{
if(flag[i]==1)
{a=a+1;}
}
for(var i=1;i<11;i++)
{
if(flag[i]==2)
{b=b+1;}
}
for(var i=1;i<11;i++)
{
if(flag[i]==3)
{c=c+1;}
}
drawChart()
}
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['one', a],
['two', b],
['three', c]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<form action="post.php" method="post" name="add" enctype="multipart/form-data" onSubmit="return check();" >
<h1>1、ssss?</h1>
<input name="v1" type="radio" value="1">1
<input name="v1" type="radio" value="2">2
<input name="v1" type="radio" value="3">3
<input type="button" value="submit" onclick="check1()"/>
<h1>2、ssss?</h1>
<input name="v2" type="radio" value="1">1
<input name="v2" type="radio" value="2">2
<input name="v2" type="radio" value="3">3
<h1>3、ssss?</h1>
<input name="v3" type="radio" value="1">1
<input name="v3" type="radio" value="2">2
<input name="v3" type="radio" value="3">3
<h1>4、ssss?</h1>
<input name="v4" type="radio" value="1">1
<input name="v4" type="radio" value="2">2
<input name="v4" type="radio" value="3">3
<h1>5、ssss?</h1>
<input name="v5" type="radio" value="1">1
<input name="v5" type="radio" value="2">2
<input name="v5" type="radio" value="3">3
<h1>6、ssss?</h1>
<input name="v6" type="radio" value="1">1
<input name="v6" type="radio" value="2">2
<input name="v6" type="radio" value="3">3
<h1>7、ssss?</h1>
<input name="v7" type="radio" value="1">1
<input name="v7" type="radio" value="2">2
<input name="v7" type="radio" value="3">3
<h1>8、ssss?</h1>
<input name="v8" type="radio" value="1">1
<input name="v8" type="radio" value="2">2
<input name="v8" type="radio" value="3">3
<h1>9、ssss?</h1>
<input name="v9" type="radio" value="1">1
<input name="v9" type="radio" value="2">2
<input name="v9" type="radio" value="3">3
<h1>10、ssss?</h1>
<input name="v10" type="radio" value="1">1
<input name="v10" type="radio" value="2">2
<input name="v10" type="radio" value="3">3
</form>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
2016年5月6日 星期五
選項加起來
<!DOCTYPE html ><html >
<head>
<title>
Form
</title>
<script>
function check1() {
var radio1=document.getElementsByName("v1");
var radio2=document.getElementsByName("v2");
var radio3=document.getElementsByName("v3");
var radio4=document.getElementsByName("v4");
var radio5=document.getElementsByName("v5");
var radio6=document.getElementsByName("v6");
var radio7=document.getElementsByName("v7");
var radio8=document.getElementsByName("v8");
var radio9=document.getElementsByName("v9");
var radio10=document.getElementsByName("v10");
for(var i=0;i<radio1.length;i++){
if(radio1.item(i).checked==true) {
flag1=i+1;
break;
}
}
for(var i=0;i<radio2.length;i++){
if(radio2.item(i).checked==true) {
flag2=i+1;
break;
}
}
for(var i=0;i<radio3.length;i++){
if(radio3.item(i).checked==true) {
flag3=i+1;
break;
}
}
for(var i=0;i<radio4.length;i++){
if(radio4.item(i).checked==true) {
flag4=i+1;
break;
}
}
for(var i=0;i<radio9.length;i++){
if(radio9.item(i).checked==true) {
flag9=i+1;
break;
}
}
for(var i=0;i<radio5.length;i++){
if(radio5.item(i).checked==true) {
flag5=i+1;
break;
}
}
for(var i=0;i<radio10.length;i++){
if(radio10.item(i).checked==true) {
flag10=i+1;
break;
}
}
for(var i=0;i<radio6.length;i++){
if(radio6.item(i).checked==true) {
flag6=i+1;
break;
}
}
for(var i=0;i<radio7.length;i++){
if(radio7.item(i).checked==true) {
flag7=i+1;
break;
}
}
for(var i=0;i<radio8.length;i++){
if(radio8.item(i).checked==true) {
flag8=i+1;
break;
}
}
document.write(flag1,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10);
document.write("<br>")
document.write(flag1+flag2+flag3+flag4+flag5+flag6+flag7+flag8+flag9+flag10);
}
</script>
</head>
<body>
<form action="post.php" method="post" name="add" enctype="multipart/form-data" onSubmit="return check();" >
<h1>1、ssss?</h1>
<input name="v1" type="radio" value="1">1
<input name="v1" type="radio" value="2">2
<input name="v1" type="radio" value="3">3
<input type="button" value="submit" onclick="check1()"/>
<h1>2、ssss?</h1>
<input name="v2" type="radio" value="1">1
<input name="v2" type="radio" value="2">2
<input name="v2" type="radio" value="3">3
<h1>3、ssss?</h1>
<input name="v3" type="radio" value="1">1
<input name="v3" type="radio" value="2">2
<input name="v3" type="radio" value="3">3
<h1>4、ssss?</h1>
<input name="v4" type="radio" value="1">1
<input name="v4" type="radio" value="2">2
<input name="v4" type="radio" value="3">3
<h1>5、ssss?</h1>
<input name="v5" type="radio" value="1">1
<input name="v5" type="radio" value="2">2
<input name="v5" type="radio" value="3">3
<h1>6、ssss?</h1>
<input name="v6" type="radio" value="1">1
<input name="v6" type="radio" value="2">2
<input name="v6" type="radio" value="3">3
<h1>7、ssss?</h1>
<input name="v7" type="radio" value="1">1
<input name="v7" type="radio" value="2">2
<input name="v7" type="radio" value="3">3
<h1>8、ssss?</h1>
<input name="v8" type="radio" value="1">1
<input name="v8" type="radio" value="2">2
<input name="v8" type="radio" value="3">3
<h1>9、ssss?</h1>
<input name="v9" type="radio" value="1">1
<input name="v9" type="radio" value="2">2
<input name="v9" type="radio" value="3">3
<h1>10、ssss?</h1>
<input name="v10" type="radio" value="1">1
<input name="v10" type="radio" value="2">2
<input name="v10" type="radio" value="3">3
</form>
</body>
</html>
<head>
<title>
Form
</title>
<script>
function check1() {
var radio1=document.getElementsByName("v1");
var radio2=document.getElementsByName("v2");
var radio3=document.getElementsByName("v3");
var radio4=document.getElementsByName("v4");
var radio5=document.getElementsByName("v5");
var radio6=document.getElementsByName("v6");
var radio7=document.getElementsByName("v7");
var radio8=document.getElementsByName("v8");
var radio9=document.getElementsByName("v9");
var radio10=document.getElementsByName("v10");
for(var i=0;i<radio1.length;i++){
if(radio1.item(i).checked==true) {
flag1=i+1;
break;
}
}
for(var i=0;i<radio2.length;i++){
if(radio2.item(i).checked==true) {
flag2=i+1;
break;
}
}
for(var i=0;i<radio3.length;i++){
if(radio3.item(i).checked==true) {
flag3=i+1;
break;
}
}
for(var i=0;i<radio4.length;i++){
if(radio4.item(i).checked==true) {
flag4=i+1;
break;
}
}
for(var i=0;i<radio9.length;i++){
if(radio9.item(i).checked==true) {
flag9=i+1;
break;
}
}
for(var i=0;i<radio5.length;i++){
if(radio5.item(i).checked==true) {
flag5=i+1;
break;
}
}
for(var i=0;i<radio10.length;i++){
if(radio10.item(i).checked==true) {
flag10=i+1;
break;
}
}
for(var i=0;i<radio6.length;i++){
if(radio6.item(i).checked==true) {
flag6=i+1;
break;
}
}
for(var i=0;i<radio7.length;i++){
if(radio7.item(i).checked==true) {
flag7=i+1;
break;
}
}
for(var i=0;i<radio8.length;i++){
if(radio8.item(i).checked==true) {
flag8=i+1;
break;
}
}
document.write(flag1,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10);
document.write("<br>")
document.write(flag1+flag2+flag3+flag4+flag5+flag6+flag7+flag8+flag9+flag10);
}
</script>
</head>
<body>
<form action="post.php" method="post" name="add" enctype="multipart/form-data" onSubmit="return check();" >
<h1>1、ssss?</h1>
<input name="v1" type="radio" value="1">1
<input name="v1" type="radio" value="2">2
<input name="v1" type="radio" value="3">3
<input type="button" value="submit" onclick="check1()"/>
<h1>2、ssss?</h1>
<input name="v2" type="radio" value="1">1
<input name="v2" type="radio" value="2">2
<input name="v2" type="radio" value="3">3
<h1>3、ssss?</h1>
<input name="v3" type="radio" value="1">1
<input name="v3" type="radio" value="2">2
<input name="v3" type="radio" value="3">3
<h1>4、ssss?</h1>
<input name="v4" type="radio" value="1">1
<input name="v4" type="radio" value="2">2
<input name="v4" type="radio" value="3">3
<h1>5、ssss?</h1>
<input name="v5" type="radio" value="1">1
<input name="v5" type="radio" value="2">2
<input name="v5" type="radio" value="3">3
<h1>6、ssss?</h1>
<input name="v6" type="radio" value="1">1
<input name="v6" type="radio" value="2">2
<input name="v6" type="radio" value="3">3
<h1>7、ssss?</h1>
<input name="v7" type="radio" value="1">1
<input name="v7" type="radio" value="2">2
<input name="v7" type="radio" value="3">3
<h1>8、ssss?</h1>
<input name="v8" type="radio" value="1">1
<input name="v8" type="radio" value="2">2
<input name="v8" type="radio" value="3">3
<h1>9、ssss?</h1>
<input name="v9" type="radio" value="1">1
<input name="v9" type="radio" value="2">2
<input name="v9" type="radio" value="3">3
<h1>10、ssss?</h1>
<input name="v10" type="radio" value="1">1
<input name="v10" type="radio" value="2">2
<input name="v10" type="radio" value="3">3
</form>
</body>
</html>
2016年4月29日 星期五
網頁選項
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>第一支javaScript</title>
</head>
<body>
<h2>document.write用法</h2>
<Script type ="text/javascript">
</Script>
<form action="#" method="post" class="demoForm" id="demoForm">
<fieldset>
<legend>Demo: Get Value of Selected</legend>
<p>Select a shipping method:</p>
<p>
<label><input type="radio" name="ship" value="a" checked />(a) Standard Ground</label>
<label><input type="radio" name="ship" value="b" /> (b)Second Day</label>
<label><input type="radio" name="ship" value="c" /> (c)Overnight</label>
<label><input type="radio" name="ship" value="d" /> (d)Pick up</label>
</p>
<p><button type="button" name="getVal">Get Value of Selected</button></p>
</fieldset>
</form>
<script type="text/javascript">
// to remove from global namespace
(function() {
function getRadioVal(form, name) {
var val;
var radios = form.elements[name];
for (var i=0, len=radios.length; i<len; i++) {
if ( radios[i].checked ) {
val = radios[i].value;
break;
}
}
return val;
}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>第一支javaScript</title>
</head>
<body>
<h2>document.write用法</h2>
<Script type ="text/javascript">
</Script>
<form action="#" method="post" class="demoForm" id="demoForm">
<fieldset>
<legend>Demo: Get Value of Selected</legend>
<p>Select a shipping method:</p>
<p>
<label><input type="radio" name="ship" value="a" checked />(a) Standard Ground</label>
<label><input type="radio" name="ship" value="b" /> (b)Second Day</label>
<label><input type="radio" name="ship" value="c" /> (c)Overnight</label>
<label><input type="radio" name="ship" value="d" /> (d)Pick up</label>
</p>
<p><button type="button" name="getVal">Get Value of Selected</button></p>
</fieldset>
</form>
<script type="text/javascript">
// to remove from global namespace
(function() {
function getRadioVal(form, name) {
var val;
var radios = form.elements[name];
for (var i=0, len=radios.length; i<len; i++) {
if ( radios[i].checked ) {
val = radios[i].value;
break;
}
}
return val;
}
2016年4月18日 星期一
2016年4月17日 星期日
第四次上課
建立login.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `user`";
$result = $conn->query($sql);
if ($result != null) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. $row["passwd"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `user`";
$result = $conn->query($sql);
if ($result != null) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. $row["passwd"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
第四次第五次
Eclipse 程式
Eclipse連結SQL
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcmysql {
private Connection con = null; //Database objects
//連接object
private Statement stat = null;
//執行,傳入之sql為完整字串
private ResultSet rs = null;
//結果集
private PreparedStatement pst = null;
//執行,傳入之sql為預儲之字申,需要傳入變數之位置
//先利用?來做標示
private String dropdbSQL = "DROP TABLE User ";
private String createdbSQL = "CREATE TABLE User (" +
" id INTEGER " +
" , name VARCHAR(20) " +
" , passwd VARCHAR(20))";
private String insertdbSQL = "insert into User(id,name,passwd) " +
"select ifNULL(max(id),0)+1,?,? FROM User";
private String selectSQL = "select * from User ";
public jdbcmysql()
{
try {
Class.forName("com.mysql.jdbc.Driver");
//註冊driver
con = DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5",
"","");
//取得connection
//jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
//localhost是主機名,test是database名
//useUnicode=true&characterEncoding=Big5使用的編碼
}
catch(ClassNotFoundException e)
{
System.out.println("DriverClassNotFound :"+e.toString());
}//有可能會產生sqlexception
catch(SQLException x) {
System.out.println("Exception :"+x.toString());
}
}
//建立table的方式
//可以看看Statement的使用方式
public void createTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(createdbSQL);
}
catch(SQLException e)
{
System.out.println("CreateDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//新增資料
//可以看看PrepareStatement的使用方式
public void insertTable( String name,String passwd)
{
try
{
pst = con.prepareStatement(insertdbSQL);
pst.setString(1, name);
pst.setString(2, passwd);
pst.executeUpdate();
}
catch(SQLException e)
{
System.out.println("InsertDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//刪除Table,
//跟建立table很像
public void dropTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(dropdbSQL);
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//查詢資料
//可以看看回傳結果集及取得資料方式
public void SelectTable()
{
try
{
stat = con.createStatement();
rs = stat.executeQuery(selectSQL);
System.out.println("ID\t\tName\t\tPASSWORD");
while(rs.next())
{
System.out.println(rs.getInt("id")+"\t\t"+
rs.getString("name")+"\t\t"+rs.getString("passwd"));
}
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//完整使用完資料庫後,記得要關閉所有Object
//否則在等待Timeout時,可能會有Connection poor的狀況
private void Close()
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stat!=null)
{
stat.close();
stat = null;
}
if(pst!=null)
{
pst.close();
pst = null;
}
}
catch(SQLException e)
{
System.out.println("Close Exception :" + e.toString());
}
}
public static void main(String[] args)
{
//測看看是否正常
jdbcmysql test = new jdbcmysql();
test.dropTable();
test.createTable();
test.insertTable("yku", "12356");
test.insertTable("yku2", "7890");
test.SelectTable();
}
}
資料表 login.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `user`";
$result = $conn->query($sql);
if ($result != null) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. $row["passwd"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Eclipse連結SQL
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcmysql {
private Connection con = null; //Database objects
//連接object
private Statement stat = null;
//執行,傳入之sql為完整字串
private ResultSet rs = null;
//結果集
private PreparedStatement pst = null;
//執行,傳入之sql為預儲之字申,需要傳入變數之位置
//先利用?來做標示
private String dropdbSQL = "DROP TABLE User ";
private String createdbSQL = "CREATE TABLE User (" +
" id INTEGER " +
" , name VARCHAR(20) " +
" , passwd VARCHAR(20))";
private String insertdbSQL = "insert into User(id,name,passwd) " +
"select ifNULL(max(id),0)+1,?,? FROM User";
private String selectSQL = "select * from User ";
public jdbcmysql()
{
try {
Class.forName("com.mysql.jdbc.Driver");
//註冊driver
con = DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5",
"","");
//取得connection
//jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
//localhost是主機名,test是database名
//useUnicode=true&characterEncoding=Big5使用的編碼
}
catch(ClassNotFoundException e)
{
System.out.println("DriverClassNotFound :"+e.toString());
}//有可能會產生sqlexception
catch(SQLException x) {
System.out.println("Exception :"+x.toString());
}
}
//建立table的方式
//可以看看Statement的使用方式
public void createTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(createdbSQL);
}
catch(SQLException e)
{
System.out.println("CreateDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//新增資料
//可以看看PrepareStatement的使用方式
public void insertTable( String name,String passwd)
{
try
{
pst = con.prepareStatement(insertdbSQL);
pst.setString(1, name);
pst.setString(2, passwd);
pst.executeUpdate();
}
catch(SQLException e)
{
System.out.println("InsertDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//刪除Table,
//跟建立table很像
public void dropTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(dropdbSQL);
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//查詢資料
//可以看看回傳結果集及取得資料方式
public void SelectTable()
{
try
{
stat = con.createStatement();
rs = stat.executeQuery(selectSQL);
System.out.println("ID\t\tName\t\tPASSWORD");
while(rs.next())
{
System.out.println(rs.getInt("id")+"\t\t"+
rs.getString("name")+"\t\t"+rs.getString("passwd"));
}
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//完整使用完資料庫後,記得要關閉所有Object
//否則在等待Timeout時,可能會有Connection poor的狀況
private void Close()
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stat!=null)
{
stat.close();
stat = null;
}
if(pst!=null)
{
pst.close();
pst = null;
}
}
catch(SQLException e)
{
System.out.println("Close Exception :" + e.toString());
}
}
public static void main(String[] args)
{
//測看看是否正常
jdbcmysql test = new jdbcmysql();
test.dropTable();
test.createTable();
test.insertTable("yku", "12356");
test.insertTable("yku2", "7890");
test.SelectTable();
}
}
資料表 login.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `user`";
$result = $conn->query($sql);
if ($result != null) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. $row["passwd"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
第三次上課
1.下載阿帕契
2.在網頁打localhost/phpmyadmin/
3.建立新的資料庫 編碼utf_8_unicode_ci
4.建立資料表欄位5
5. name varchar 15
gender char 1
mobile varchar 15
phone varchar 15
address varchar 30
6.案新增
7.輸入資料
8.
2.在網頁打localhost/phpmyadmin/
3.建立新的資料庫 編碼utf_8_unicode_ci
4.建立資料表欄位5
5. name varchar 15
gender char 1
mobile varchar 15
phone varchar 15
address varchar 30
6.案新增
7.輸入資料
8.
第二次上課
1.下載阿帕契
2.到C:\xampp\htdocs\dashboard這裡,新增文字檔demo(.txt)
3.複製貼上
<?php
2.到C:\xampp\htdocs\dashboard這裡,新增文字檔demo(.txt)
3.複製貼上
<?php
// 這是 C++ 風格註解
# 這是 Perl 風格註解
echo
"\n\n"
;
echo
date
(
"Y/m/d"
).
"\n"
;
echo
"↑↑ 上面印出今天幾月幾日\n"
;
echo
strftime
(
"%X"
,
time
()).
"\n"
;
echo
"↑↑ 上面印出現在幾點幾分\n"
;
echo
"\n\n"
;
/* 《程式語言教學誌》的範例程式
檔名:demo.php
功能:示範 PHP 程式
作者:張凱慶
時間:西元 2012 年 11 月 */
?>
http://pydoing.blogspot.tw/2012/11/php-Understanding-of-PHP-Progamming-Language.html
4.把附檔名改為.php
5.找到php.ini檔將UTF-8改為Big5
6.然後到網頁打
http://localhost/dashboard/demo.php
訂閱:
文章 (Atom)