【问题标题】:Show more info on database item显示有关数据库项目的更多信息
【发布时间】:2012-08-18 06:20:42
【问题描述】:

我需要一个 php 页面的帮助,该页面显示来自 mysql 数据库的有关城镇广场周围建筑物的信息。我希望将其设置为首先显示地址。然后,当有人点击某个地址时,它会向他们显示有关该特定建筑物的更多信息。

我是 PHP 新手。我知道有两种解决方案可行,但除非我必须这样做,否则我不想走那条路。

这两种解决方案是

  1. 为每个建筑物创建一个页面,并将每个地址链接到特定页面和

  2. 将每个数据库项插入到页面中(而不是使用 PHP 循环)和可以为每个地址切换的隐藏 div。


我现在拥有的代码(并且有效)用于显示地址:

   echo "<p><a href="WhatDoIPut???"><h3>   " . stripslashes($rowBuildings[building_address]) . "</h3></a><br>\n";

但是如果(且仅当)他们点击建筑物地址时,我如何显示其余的建筑物信息?对不起,如果这是一个广泛的话题。我已经阅读了几个论坛,但没有运气。我的问题是没有从数据库中获取信息。

【问题讨论】:

  • 您可以将信息存储在隐藏的&lt;div&gt; 中并使用javascript onclick 事件显示,或者使用AJAX 获取数据并显示在&lt;div&gt; 中。无论哪种方式,您都必须使用 javascript。
  • 基本上你想用包含的其他信息来回显你所有的 html,然后使用 javascript 显示/隐藏正确的位。
  • 另外,由于您还是 PHP 新手,我想确保您不会走错路,如果您使用的是mysql_*函数,停止。它们正在被弃用。请改用PDO(PHP 5.1 起支持)或mysqli(PHP 4.1 起支持)。如果您不确定使用哪一个,read this article

标签: php mysql show expand collapse


【解决方案1】:

我找到了解决我的困境的方法。 . .

我使用 Javascript 来切换 div(这很简单)。我的问题是切换每个单独的 div,因为它们是由 PHP 脚本添加的(根据数据库条目的数量)。我不知道如何为链接和 div 增加 div id 名称。示例:

 <!--This was the link to toggle the div --> 
 <a href="#" onclick="toggleMe('divid');"><h3>Address</h3></a>

 <!--This was the div to toggle --> 
 <div id="divid">Hello</div>

我的 PHP 插入了多个 div 和链接,所以当您单击一个地址时,所有名为“divid”的 div 都会出现。为了解决这个问题,我为每个 div id 添加了一个 PHP 变量,例如:

$uniqueID = 0;
$PleaseWork = 0;

然后我把php代码放在最下面:

$uniqueId++; $请工作++;

这让我可以将变量放在链接和 div id 中,这将一致地计算在一起。总而言之,这里是代码:

Java 切换

> <script type="text/javascript">
> 
>       function toggleMe(a){ var e=document.getElementById(a); if(!e)return
> true; if(e.style.display=="none"){ e.style.display="block" } else{
> e.style.display="none" } return true } </script>

PHP 代码 - 链接和可见项

<div class="building" align="left" style="margin-left:100px;">
    <? 
    {
 $uniqueID = 0;
 $PleaseWork = &$uniqueID;
 }
 $selectAddress ="SELECT * FROM `buildings` order by building_address";
 $resultAddress = mysql_query($selectAddress);
  while($rowBuildings = mysql_fetch_array($resultAddress)){;
echo "<p><a id=\"displayText\" href=\"#\" onclick=toggleMe(\"divn$PleaseWork\");><h3>   " . stripslashes($rowBuildings[building_address]) . "</a></h3><br>\n";
   echo "<b>For Sale or Rent:</b> " . $rowBuildings[building_saleorrent] . "";
    if(!empty($rowBuildings[building_permonth]))
      echo "<b>Rent Per Month: </b>" . $rowBuildings[building_permonth] . "";
      if(!empty($rowBuildings[building_saleprice]))
      echo "<b>Sale Price: </b>" . $rowBuildings[building_saleprice] . "";
   echo "<br>\n";

PHP 代码 - 隐藏的 DIV

   echo "<div id=\"divn$uniqueID\" style=\"display:none\"><b>Is the Property Listed with a Realtor? </b> " . $rowBuildings[building_realtor] . "<br>\n";
        if(!empty($rowBuildings[building_target]))
        echo "<b>Best Suited for Building: </b>" . $rowBuildings[building_target] . "<br>\n";
    echo "<b>Owner: </b>" . $rowBuildings[building_owner] . "";
    if(!empty($rowBuildings[building_ownphone]))
      echo "<b>  Phone: </b>" . formatPhone($rowBuildings[building_ownphone]) . "";
      if(!empty($rowBuildings[building_ownemail]))
      echo "<b>  Email: </b>" . $rowBuildings[building_ownemail] . "";
    echo "<br>\n";
     echo "<b> Is the building occupied? </b>" . $rowBuildings[building_isoccupant] . "<br>\n";
    if(!empty($rowBuildings[building_occupant]))
      echo "<b>Current Occupant:</b>" . $rowBuildings[building_occupant] . "<br>\n";
    if(!empty($rowBuildings[building_occupantphone]))
      echo "<b>Occupant Phone:</b>" . formatPhone($rowBuildings[building_occupantphone]) . "<br>\n";
        echo "<b>Utilities: </b>" . $rowBuildings[building_utilities] . "<br>\n";
        echo "<b>Stories: </b>" . $rowBuildings[building_stories] . "<br>\n";
        echo "<b>Total Sq. Footage: </b>" . $rowBuildings[building_square] . "<br>\n";
        echo "<b>Footage Breakdown:</b><br> ";
    if(!empty($rowBuildings[building_residential]))
      echo "<b>Residental: </b>" . $rowBuildings[building_residential] . " ";
      if(!empty($rowBuildings[building_lightindustry]))
      echo "<b>Light Industrial: </b>" . $rowBuildings[building_lightindustry] . " ";
      if(!empty($rowBuildings[building_commercial]))
      echo "<b>Residental: </b>" . $rowBuildings[building_commercial] . " ";
      echo "<br>\n";
        echo "<b>Storage: </b>" . $rowBuildings[building_storage] . "&nbsp; <b>Storage Sq. Footage:&nbsp; </b>" . $rowBuildings[building_storefoot] . "<br>\n";
        echo "<b>Inside of Building: </b>" . $rowBuildings[building_inside] . "<br>\n";
        echo "<b>Outside of Building: </b>" . $rowBuildings[building_outside] . "<br>\n";
        echo "<b>Parking: </b>" . $rowBuildings[building_parking] . "<br>\n";
         if(!empty($rowBuildings[building_issues]))
      echo "<b>Issues With the Building: </b>" . $rowBuildings[building_issues] . "<br>\n";
       if(!empty($rowBuildings[building_features]))
      echo "<b>Main Features of the Building: </b>" . $rowBuildings[building_features] . "<br>\n";
       if(!empty($rowBuildings[building_notes]))
      echo "<b>Notes on the Building: </b>" . $rowBuildings[building_notes] . "<br>\n";     
    echo "</div></p>";
$uniqueId++;
$PleaseWork++;
}


?>

我使用各种网站来收集这些信息。以下是一些:

http://www.dynamicdrive.com/forums/showthread.php?41829-Toggle-Div-in-PHP

http://php.net/manual/en/language.references.unset.php

对不起,如果这令人困惑。感谢您的所有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多