<? $goldnums = $_POST['gold_num']; $goldprice = $_POST['gold_pri']; <form name="inputnum" method="post" action="pushin.php"> <table width=30% style="text-align:center;" bgcolor="#0066ff" border=0> <tr><td style="color:#ffffff;"><b></b></td></tr> <tr> <td colspan=2><input id="setfocus" type="text" style="text-align:right;" name="gold_num" /> <input type="text" style="text-align:right;" name="gold_pri" /><input type="submit" value="+" size="3"></td> </tr> </table> </form> if($goldnums!="" and $goldprice!=""){ $sql = "select * from goldnum order by goldnumber;"; $result = mysql_db_query($dbs,$sql)or die('error query') ; $num = mysql_num_rows($result); print "<table border=0 style='text-align:center;' width=15%>"; print "<tr><td style='color:#ffffff;' width=50% bgcolor=#0066ff><b>Goods</b></td> <td style='color:#ffffff;' width=50% bgcolor=#0066ff><b>Price</b></td></tr>"; while ($record = mysql_fetch_array($result)){ print "<tr bgcolor=#daf7fa>"; print "<td>$record[1]</td>"; print "<td>$record[2]</td>"; print "</tr>"; } print "</table>"; } ?>
<!DOCTYPE html> <html> <head> <style type="text/css"> table { border-collapse: collapse; } td, th { border: #999999 solid 1px; padding: 8px; text-align: center; } th { background-color: #ff9900 !important; color: #ffffff; } .first-column { background-color: #f0f0f0; border-left: #000000 solid 2px; } </style> </head> <body> <?php // จำลองข้อมูลสินค้า // จำนวนสินค้า (ในที่นี้สุ่ม 50 ~ 100) $num_items = mt_rand(50, 100); for ($i = 0; $i < $num_items; $i++) { $items[] = array('id' => $i + 1, 'price' => mt_rand(100, 1000)); } // กำหนดจำนวนสินค้าในแนวขว (ในที่นี้สุ่ม 3 ~ 5) $num_columns = mt_rand(3, 5); ?> <h3>จำนวนสินค้าทั้งหมด <?php echo $num_items; ?> รายการ</h3> <table border="0"> <tr> <?php // วนลูปแนวขวาง เพื่อสร้างหัวตาราง for ($column_no = 0; $column_no < $num_columns; $column_no++): ?> <th class="first-column">สินค้า</th><th>ราคา</th> <?php endfor; ?> </tr> <?php // หาจำนวนสินค้าในแนวตั้ง (จำนวนแถว) // ด้วยการเอาจำนวนสินค้าทั้งหมดไปหารจำนวนสินค้าในแนวขวาง และปัดเศษขึ้น // เช่น มีสินค้า 50 ชิ้น แสดง 3 คอลัมน์ // 50 หาร 3 ปัดเศษขึ้นได้ 17 แถว $num_rows = ceil($num_items / $num_columns); // วนลูปแสดงสินค้า เป็นลูปสองชั้น // ลูปนอก วนลูปแนวตั้ง (แถว) for ($row_index = 0; $row_index < $num_rows; $row_index++): ?> <tr> <?php // ลูปใน วนลูปแนวขวาง (คอลัมน์) for ($column_no = 0; $column_no < $num_columns; $column_no++): // หา index ของสินค้านี้ โดยคำนวณจาก // คอลัมน์ปัจจุบัน คูณ จำนวนแถวทั้งหมด หลังจากนั้น บวก ด้วยแถวปัจจุบัน $item_index = $column_no * $num_rows + $row_index; // หาก index ที่คำนวณออกมาได้ มากกว่าจำนวนสินค้า ให้ข้ามไป ไม่ต้องแสดง if ($item_index >= $num_items) { continue; } // แสดงผลสินค้า $item = $items[$item_index]; ?> <td class="first-column"><?php echo $item['id']; ?></td> <td><?php echo $item['price']; ?></td> <?php endfor; ?> </tr> <?php endfor; ?> </table> </body> </html>
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง