function get_rows($id = ''){
if($id != ''){
//fetch single row
$query = mysql_query("SELECT * FROM lamas WHERE id = $id");
$data = mysql_fetch_assoc($query);
}else{
//fetch all rows
$query = mysql_query("SELECT * FROM lamas WHERE Pak = '4' ORDER BY like_num DESC limit 3");
while($row = mysql_fetch_assoc($query)){
$data[] = $row;
}
}
return $data;
}
Code (PHP)
index
<?php
include_once("tutorial.php");
$tutorial = new Tutorial();
$trows = $tutorial->get_rows();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ระบบโหวต</title>
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<style type="text/css">
.row{ margin:20px 20px 20px 20px;}
.ratings{ font-size:25px !important;}
.thumbnail img {
width: 100%;
}
.ratings {
padding-right: 10px;
padding-left: 10px;
color: #d17581;
}
.thumbnail {
padding: 0;
}
.thumbnail .caption-full {
padding: 9px;
color: #333;
}
.glyphicon-thumbs-up:hover{ color:#008000; cursor:pointer;}
.glyphicon-thumbs-down:hover{ color: #E10000; cursor:pointer;}
.counter{ color:#333333;}
.thumbnail img{height:300px;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
/**
* Function Name: cwRating()
* Function Author: CodexWorld
* Description: cwRating() function is used for implement the rating system. cwRating() function insert like or dislike data into the database and display the rating count at the target div.
* id = Unique ID, like or dislike is based on this ID.
* type = Use 1 for like and 0 for dislike.
* target = Target div ID where the total number of likes or dislikes will display.
**/
function cwRating(id,type,target){
$.ajax({
type:'POST',
url:'rating.php',
data:'id='+id+'&type='+type,
success:function(msg){
if(msg == 'err'){
alert('มีปัญหาในการ ให้คะแนน.');
}else{
$('#'+target).html(msg);
}
}
});
}
</script>
</head>
<body>
<div class="row">
<?php foreach($trows as $trow){ ?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="http://127.0.0.1/gohalal_edit_19_6_2559/myfile/<?php echo $objResult1["FilesName"];?>" alt="" />
<div class="caption">
<h4><a href="javascript:void(0);"><?php echo $trow['Name']; ?></a></h4>
<p><?php echo $trow['Address']; ?></p>
</div>
<div class="ratings">
<p class="pull-right"></p>
<p>
<!-- Like Icon HTML -->
<span class="like_num" onClick="cwRating(<?php echo $trow['id']; ?>,1,'like_count<?php echo $trow['id']; ?>')"></span>
<!-- Like Counter -->
<span class="counter" id="like_count<?php echo $trow['id']; ?>">โหวตให้ <?php echo $trow['like_num']; ?> คะแนน</span> <?php echo $trow['id']; ?><!--span class="counterRight" >ติดต่อ : <?php echo $trow['Tel']; ?></span-->
<!-- Dislike Icon HTML -->
<!--<span class="glyphicon glyphicon-thumbs-down" onClick="cwRating(<?php echo $trow['id']; ?>,0,'dislike_count<?php echo $trow['id']; ?>')"></span> -->
<!-- Dislike Counter -->
<span class="counter" id="dislike_count<?php echo $trow['id']; ?>"><?php echo $trow['dislike_num']; ?></span>
</p>
</div>
</div>
</div>
<?php } ?>
</div>
</body>
</html>
ถ้ายังจะใช้ function ลองศึกษาว่า function การแทนค่าตัวแปล การเรียกใช้งานเป็นแบบไหน ผมว่า function ไม่น่าจะแทนตัวแปรแบบนั้นได้ ผิดถูกรอคนแม่นทฤษฎีมาแนะนำ ขอน้อมรับ....