๑๙ พ.ย. ๒๕๕๐

upload image with php II การอัปโหลดรูปภาพเก็บในฐานข้อมูล II

upload image with php II การอัปโหลดรูปภาพเก็บในฐานข้อมูล ภาคแสดงผล
ครั้งที่แล้วแสดงการอัปโหลดภาพเก็บในฐานข้อมูล ครั้งนี้จะแสดงการนำภาพขึ้นแสดง หน้าเว็บ

ลองสังเกตโค้ดตรงกำหนด ว่าจะให้ตารางแสดงกี่คอลัมน์ นะครับ เห็นถามกันในหลายๆเว็บบอร์ด จะเห็นว่าเราควบคุมได้ด้วยโค้ดแค่ 2 บรรทัด

ดูโค้ดนะครับ


<?php
// ส่วนกำหนดการเชื่อมต่อฐานข้อมูล
$hostname_connection = "localhost";
$database_connection = "db_test";
$username_connection = "root";
$password_connection = "";
$connection = mysql_pconnect($hostname_connection, $username_connection, $password_connection) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query( "SET NAMES UTF8" ) ;
?>

<?php
// ดึงข้อมูลจากฐานข้อมูล ไม่มีโค้ดการแบ่งหน้านะจ๊ะ
mysql_select_db($database_connection, $connection);
$query_rs_image = "SELECT * FROM tbl_image ORDER BY image_id ASC";
$rs_image = mysql_query($query_rs_image, $connection) or die(mysql_error());
$row_rs_image = mysql_fetch_assoc($rs_image);
$totalRows_rs_image = mysql_num_rows($rs_image);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php if ($totalRows_rs_image > 0) { // Show if recordset not empty ?>

<tr>
<?php do { ?>

<?php // รูปภาพให้เอาค่าจากฐานข้อมูลมาต่อ ให้ชี้ไปยังภาพที่อัปโหลดมา ?>
<td><img src="upload_images/<?php echo $row_rs_image['image_name']; ?>" /></td>

<?php
// กำหนดว่า จะให้ตาราง แสดงกี่คอลัมน์ ง่ายๆ ด้วยคำสั่งแค่ 2 บรรทัด
$iLoop++ ;
if ( $iLoop % 3 == 0 ) {echo "</tr><tr>" ;}
?>

<?php } while ($row_rs_image = mysql_fetch_assoc($rs_image)); ?>

</tr>
<?php } // Show if recordset not empty ?>

</table>
</body>
</html>
<?php
mysql_free_result($rs_image);
?>

ไม่มีความคิดเห็น: