Search code examples
phpmysqlshow

Show more info on database item


I need help with a php page that shows information from a mysql database concerning buildings around a town square. I want it set up so that the addresses are the only thing displayed at first. Then when someone clicks on a certain address it shows them more information on that particular building.

I am new to PHP. There are two solutions I know will work but I don't want to go that route unless I must.

Those two solutions are

  1. create a page for each building and link each address to the specific page and

  2. Insert each database item into the page (instead of having a PHP loop) and hidden div that could be toggled for each address.


The code I have right now (and it works) is for displaying the address:

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

But how do I display the rest of the building info if (and only if) they click on the building address? Sorry if this is a broad topic. I've read several forums but with no luck. My problem isn't getting the info from the database.


Solution

  • I found the fix to my dilemma . . .

    I was using the Javascript to Toggle a div (that was fairly simple). My problem was toggling each separate div as they were added by the PHP script (according to the number of database entries). I couldn't figure out how to have incremental div id names for both the link and the div. Example:

     <!--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>
    

    My PHP was inserting multiple divs and links though so when you would click on an address all the divs named "divid" would appear. To fix the problem I added a PHP variable to each div id such as:

    $uniqueID = 0;
    $PleaseWork = 0;
    

    I then placed at the bottom of the php code:

    $uniqueId++; $PleaseWork++;

    This allowed me to place variables in the link and the div id that would count consistently together. So altogether, here are the codes:

    Java to toggle

    > <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 CODE - THE LINK & Visible Items

    <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 CODE - The hidden 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++;
    }
    
    
    ?>
    

    I used various sites to gather this information. Here are some:

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

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

    Sorry if this is confusing. Thanks for all your help!