动态表中的动态表(PHP / MySQL)

时间:2013-01-21 09:07:33

标签: php mysql filter dynamic-tables

  1. 我在同一个数据库中有两个MySQL表,每个表都有数据。
  2. 我有一个PHP结果页面,它显示动态PHP表中第一个MySQL表的数据。
  3. 第一个MySQL表中的一个字段是以逗号分隔的ID列表,这些ID与第二个MySQL表中的数据相关。
  4. 我在第一个动态PHP表中有第二个嵌套/嵌入式动态PHP表,当第一个MySQL表的逗号分隔字段中出现行ID时,该表从第二个MySQL表中提取数据。 (即第一个动态PHP表中的一列包含第二个嵌套的动态PHP表,其中包含来自第二个MySQL表的数据
  5. 请参阅下面的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>&nbsp;</p>
    <p>&nbsp;</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>&nbsp;</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);
    ?>
    

    现在,当我运行页面时,会发生以下情况:

    • 如果嵌套表中只有一个项目(即只有一个ID) 第二个表出现在第一个表的逗号分隔字段中 表),然后它完美呈现。
    • 但是,如果有多个(即多个ID) 以逗号分隔的字段),然后将第一个项目渲染到 无穷大(并使浏览器崩溃)。

    我做错了什么?显然,嵌套表不是我正在做的事情的正确方法! n00b 但是是什么?

2 个答案:

答案 0 :(得分:1)

更好的解决方案可能是为Fav_foods提供一个单独的表。类似的东西:

ID | UserID | FoodID
 1 |   1    |    2
 2 |   1    |    4
 3 |   1    |    5

这就像:约翰最喜欢的食物是ID为2,4,5,......的食物。

答案 1 :(得分:1)

从第一个表中删除fav_food列,然后添加第3个表 - fav_food:

ID | fav_Food
1  | 2
1  | 4
1  | 5
1  | 12

然后,使用JOIN选择所有John最喜欢的食物,详情如下:

SELECT *
FROM `fav_food` JOIN `food`
ON `food`.`ID`=`fav_food`.`fav_food`
WHERE `fav_food`.`ID`=1

(使用不同的查询来选择John的详细信息)

通常,正如您所说,当您可以将常规表与JOIN一起使用时,嵌套表不是最佳选择。