|
|
|
นำค่าไปค้นหาใน array 2 มิติรบกวนเข้ามาสอนหนูหน่อยมือใหม่ค่ะ |
|
|
|
|
|
|
|
T T
|
|
|
|
|
Date :
2013-01-23 11:05:44 |
By :
nartkar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แล้วถ้ามอง array ที่ว่า
เป็น
record กับ field ละครับ (ไม่เก่งอะเรย์อ่ะ)
|
|
|
|
|
Date :
2013-01-23 12:22:21 |
By :
apisitp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
array.php
<?php require_once(dirname(__FILE__) . '/array_index.php'); ?>
<!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>
<title>PHP OOP</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="post">
<div>
<?php
$my_array = new array_index();
$my_array->show();
?>
<br />
<table>
<tr>
<td>
<span>Row: </span>
</td>
<td>
<input type="text" id="TextBoxRow" name="TextBoxRow" />
</td>
</tr>
<tr>
<td>
<span>Column: </span>
</td>
<td>
<input type="text" id="TextBoxCol" name="TextBoxCol" />
</td>
</tr>
</table>
<br />
<input type="submit" id="ButtonSubmit" Name="ButtonSubmit" value="OK" />
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $my_array->get_value($_POST["TextBoxRow"], $_POST["TextBoxCol"]);
}
?>
</div>
</form>
</body>
</html>
array_index.php
<?php
class array_index
{
private $_array = array();
public function __construct()
{
array_push($this->_array,
array(1, 5, 3, 4, 6),
array(7, 8, 9, 8, 5),
array(4, 5, 4, 4, 5),
array(4, 5, 7, 8, 6),
array(1, 4, 5, 8, 4));
}
public function show()
{
echo "<table>";
foreach ($this->_array as $row)
{
echo "<tr>";
foreach ($row as $col)
{
echo "<td style=\"border: 1px solid black; padding: 5px 10px;\">$col</td>";
}
echo "</tr>";
}
echo "</table>";
}
public function get_value($row, $col)
{
$row_len = count($this->_array);
$col_len = count($this->_array[0]);
return ($row == null || $col == null || $row > $row_len - 1 || $col > $col_len - 1) ? "ERROR" : $this->_array[$row][$col] ;
}
}
?>
|
|
|
|
|
Date :
2013-01-23 12:48:03 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เพิ่งหัด php ครับ แต่พอดีเขียนโปรแกรมเป็น
เปลี่ยนภาษาเลยง่ายเหมือนเปลี่ยนเสื้อผ้า
อย่าว่ากันนะ ขอหัดอะไรเยอะๆ หน่อยนึง
php oop ajax
array.php
<?php require_once (dirname(__FILE__) . '/array.code.php'); ?>
<!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>
<title>PHP OOP AJAX</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
function callAjax(str)
{
if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
{
xmlhttp = new XMLHttpRequest();
}
else // code for IE6, IE5
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("result").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", str, true);
xmlhttp.send();
}
function getArrayValue()
{
var row = document.getElementById("TextBoxRow").value;
var col= document.getElementById("TextBoxCol").value;
callAjax("array.ajax.php?r=" + row + "&c=" + col);
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post">
<div>
<?php
$my_array = new array_index();
$my_array -> show();
?>
<br />
<table>
<tr>
<td><span>Row:</span></td>
<td>
<input type="text" id="TextBoxRow" name="TextBoxRow" autocomplete="off" />
</td>
</tr>
<tr>
<td><span>Column:</span></td>
<td>
<input type="text" id="TextBoxCol" name="TextBoxCol" autocomplete="off" />
</td>
</tr>
</table>
<br />
<input type="submit" id="ButtonSubmit" name="ButtonSubmit" value="OK" onclick="getArrayValue(); return false;" />
<span id="result"></span>
</div>
</form>
</body>
</html>
array.code.php
<?php
class array_index
{
private $_array = array();
public function __construct()
{
array_push($this -> _array,
array(1, 5, 3, 4, 6),
array(7, 8, 9, 8, 5),
array(4, 5, 4, 4, 5),
array(4, 5, 7, 8, 6),
array(1, 4, 5, 8, 4));
}
public function show()
{
echo "<table>";
foreach ($this->_array as $row)
{
echo "<tr>\n";
foreach ($row as $col)
{
echo "<td style=\"border: 1px solid black; padding: 5px 10px;\">$col</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}
public function get_array_value($row, $col)
{
$row_len = count($this -> _array);
$col_len = count($this -> _array[0]);
return ($row == null || $col == null ||
$row > $row_len - 1 || $col > $col_len - 1 ||
is_numeric($row) === FALSE || is_numeric($col) === FALSE) ? "ERROR" : $this -> _array[$row][$col];
}
}
?>
array.ajax.php
<?php require_once (dirname(__FILE__) . '/array.code.php'); ?>
<?php
if (empty($_GET) === FALSE)
{
$r = $_GET["r"];
$c = $_GET["c"];
$my_array = new array_index();
$result = $my_array -> get_array_value($r, $c);
if ($result != "ERROR")
{
echo "array[$r][$c] = " . $result;
}
else
{
echo $result;
}
}
?>
|
|
|
|
|
Date :
2013-01-23 15:37:06 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากค่ะ เขียนออกแล้วจะนำมาบอก ขอบคุณค่ะ
|
|
|
|
|
Date :
2013-01-23 16:28:47 |
By :
nartkar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|