- 同じデータベースに 2 つの MySQL テーブルがあり、それぞれにデータがあります。
- 動的 PHP テーブルの最初の MySQL テーブルのデータを表示する PHP 結果ページがあります。
- 最初の MySQL テーブルのフィールドの 1 つは、2 番目の MySQL テーブルのデータに関連する ID のコンマ区切りリストです。
- 行 ID が最初の MySQL テーブルのコンマ区切りフィールドに表示されると、2 番目の MySQL テーブルからデータを取得する最初の動的 PHP テーブル内に、2 番目のネストされた/埋め込まれた動的 PHP テーブルがあります。(つまり、最初の動的 PHP テーブルの列の 1 つには、2 番目の MySQL テーブルからのデータを含む、ネストされた 2 番目の動的 PHP テーブルが含まれます。
以下の PHP/HTML を参照してください。
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_TravelSite, $TravelSite);
$query_Route = "SELECT * FROM SavedRoutes WHERE RouteCode = '".$_GET['rc']."'";
$Route = mysql_query($query_Route, $TravelSite) or die(mysql_error());
$row_Route = mysql_fetch_assoc($Route);
$totalRows_Route = mysql_num_rows($Route);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<p> </p>
<p> </p>
<table border="0">
<tr>
<td>ID</td>
<td>StartPoint</td>
<td>StartPointLatLng</td>
<td>EndPoint</td>
<td>EndPointLatLng</td>
<td>Waypoints</td>
<td>Name</td>
<td>Details</td>
<td>RouteCode</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Route['ID']; ?></td>
<td><?php echo $row_Route['StartPoint']; ?></td>
<td><?php echo $row_Route['StartPointLatLng']; ?></td>
<td><?php echo $row_Route['EndPoint']; ?></td>
<td><?php echo $row_Route['EndPointLatLng']; ?></td>
<td><p><?php echo $row_Route['Waypoints']; ?></p>
<?PHP $string = $row_Route['Waypoints'];
$slashstring = stripslashes($string);
$trimstring = rtrim($slashstring, "', '");
mysql_select_db($database_TravelSite, $TravelSite);
?>
<p>echo $trimstring = <?PHP echo $trimstring; ?></p>
<table border="0">
<tr>
<td>id</td>
<td>Lng</td>
<td>Lat</td>
<td>Name</td>
</tr>
<?php
mysql_select_db($database_TravelSite, $TravelSite);
$query_Markers = "SELECT * FROM markers WHERE ID IN('".$trimstring."')";
$Markers = mysql_query($query_Markers, $TravelSite) or die(mysql_error());
$row_Markers = mysql_fetch_assoc($Markers);
$totalRows_Markers = mysql_num_rows($Markers);
do { ?>
<tr>
<td><?php echo $row_Markers['id']; ?></td>
<td><?php echo $row_Markers['Lng']; ?></td>
<td><?php echo $row_Markers['Lat']; ?></td>
<td><p>
<select multiple name="waypoints" id="waypoints">
<?php
do {
?>
<option selected value="<?php echo $row_Markers['Lat'] . ", " . $row_Markers['Lng']?>"><?php echo $row_Markers['Lat'] . ", " . $row_Markers['Lng']?></option>
<?php
} while ($row_Markers = mysql_fetch_assoc($Markers));
$rows = mysql_num_rows($Markers);
if($rows > 0) {
mysql_data_seek($Markers, 0);
$row_Markers = mysql_fetch_assoc($Markers);
}
?>
</select>
</p>
<p><?php echo $row_Markers['Name']; ?></p></td>
</tr>
<?php } while ($row_Markers = mysql_fetch_assoc($Markers)); ?>
</table>
<p> </p></td>
<td><?php echo $row_Route['Name']; ?></td>
<td><?php echo $row_Route['Details']; ?></td>
<td><?php echo $row_Route['RouteCode']; ?></td>
</tr>
<?php } while ($row_Route = mysql_fetch_assoc($Route)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Markers);
mysql_free_result($Route);
?>
今、ページを実行するたびに、次のことが起こります。
- ネストされたテーブルに項目が 1 つしかない場合 (つまり、2 番目のテーブルの ID が 1 つだけ、最初のテーブルのコンマ区切りフィールドに表示される場合)、完全にレンダリングされます。
- ただし、複数ある場合 (つまり、コンマ区切りフィールドに複数の ID がある場合)、最初の項目が無限に表示されます (そしてブラウザーがクラッシュします)。
私は何を間違っていますか?明らかに、ネストされたテーブルは、私がやろうとしていることを行う正しい方法ではありません! n00bでも何?