我找到了解决我的困境的方法。. .
我正在使用 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] . " <b>Storage Sq. Footage: </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
对不起,如果这令人困惑。感谢你的帮助!