0

I used position fixed for my sticky header but it kept coming outside, i am using php to pull the data in and print it out. My code is bellow, i do have divs but the header keeps going outside the box if it is more than 15 for example. is there a way to keep it inside.

   <div class="scrollableContainer">
  <div class="scrollingArea">
<?php


if(!$link = mysql_connect("localhost", "root", "")) {
     echo "Cannot connect to db server";
}
elseif(!mysql_select_db("Disney")) {
     echo "Cannot select database";
}
else {
     if(!$rs = mysql_query("SELECT * FROM DisneyCharacters")) {
          echo "Cannot parse query";
     }
     elseif(mysql_num_rows($rs) == 0) {
          echo "No records found";
     }
     else {

//      Name, Movie, year, ShoeSize, FavoriteColor, FavoriteFood, PhoneNumber, CharacterType, FavoriteDrink, Address, FavortieTvShow, School, Age, HouseSqFoot, RelationShip, Rating, CarModel, CarYear, Boyfriend,
// NumberFriends, CriminalHistory, HappyEnding, FavoriteLocation, AppleDevice, WorkLocation, Weight, RepublicanDemocratic, DressColor, Shampoo, NumberKids) 

          echo "<div class='headerdiv'>";

                  echo "<div class='headertable'>";

          echo "<table  id=\"sortedtable\" class=\"draggable sortable\">\n";
          echo "<thead>\n<tr>";
          echo "<th>Name  </th>";
          echo "<th>Movie  </th>";
          echo "<th>Year  </th>";
          echo "<th>Shoe Size </th>";
          echo "<th>Favorite Color </th>";
          echo "<th>Favorite Food </th>";
          echo "<th>Phone Number </th>";
          echo "<th>Character Type </th>";
          echo "<th>Favorite Drink </th>";
          echo "<th>Address </th>";
          echo "<th>Favorite TvShow </th>";
          echo "<th>School </th>";
          echo "<th>Age </th>";
          echo "<th>HouseSqFoot </th>";
          echo "<th>Relationship </th>";
          echo "<th>Rating </th>";
          echo "<th>Car Model </th>";
          echo "<th>Car Year </th>";
          echo "<th>Boyfriend </th>";
          echo "<th>Number Friends </th>";
          echo "<th>Criminal History </th>";
          echo "<th>Happy Ending </th>";
          echo "<th>Favorite Location </th>";
          echo "<th>Apple Device </th>";
          echo "<th>Work Location </th>";
          echo "<th>Weight</th>";
          echo "<th>Republican Democratic </th>";
          echo "<th>Dress Color </th>";
          echo "<th>Shampoo </th>";
          echo "<th>Number Kids </th>";
echo "</tr>\n</thead>\n";


          echo " <tbody>";
          while($row = mysql_fetch_array($rs)) {


               echo "<tr>
                    <td>$row[Name]&nbsp</td>
                    <td>$row[Movie]&nbsp&nbsp</td>
                    <td>$row[Year]&nbsp</td>
                    <td>$row[ShoeSize]&nbsp</td>
                    <td>$row[FavoriteColor]&nbsp</td>
                    <td>$row[FavoriteFood]&nbsp</td>
                    <td>$row[PhoneNumber]&nbsp</td>
                    <td>$row[CharacterType]&nbsp</td>
                    <td>$row[FavoriteDrink]&nbsp</td>
                    <td>$row[Address]&nbsp</td>
                    <td>$row[FavoriteTvShow]&nbsp</td>
                    <td>$row[School]&nbsp</td>
                    <td>$row[Age]&nbsp</td>
                    <td>$row[HouseSqFoot]&nbsp</td>
                    <td>$row[RelationShip]&nbsp</td>
                    <td>$row[Rating]&nbsp</td>
                    <td>$row[CarModel]&nbsp</td>
                    <td>$row[CarYear]&nbsp</td>
                    <td>$row[Boyfriend]&nbsp</td>
                    <td>$row[NumberFriends]&nbsp</td>
                    <td>$row[CriminalHistory]&nbsp</td>
                    <td>$row[HappyEnding]&nbsp</td>
                    <td>$row[FavoriteLocation]&nbsp</td>
                    <td>$row[AppleDevice]&nbsp</td>
                    <td>$row[WorkLocation]&nbsp</td>
                    <td>$row[Weight]&nbsp</td>
                    <td>$row[RepublicanDemocratic]&nbsp</td>
                    <td>$row[DressColor]&nbsp</td>
                    <td>$row[Shampoo]&nbsp</td>
                    <td>$row[NumberKids]&nbsp</td>
                </tr>\n";



          }
           echo " </tbody>";
          echo "</table><br />\n";
          echo "</div>";
          echo "</div>";

     }

}

?>

user3474037
  • 23
  • 1
  • 7
  • If you mean you want the table head "fixed" and have the table body scrollable, that's one of the big problems with page design. You cannot do what you want with just tables. You'll have to put the "head" in a separate set of elements, adjust each width, and then have the scrollable data below. Lot's of Google hits on this problem. – TimSPQR Mar 30 '14 at 08:58
  • hmmm... but with the way i have my page with php and div's will i be able to do that easily ? – user3474037 Mar 30 '14 at 17:31
  • Anything is possible. But do a Google with html table fixed header and you'll see what I mean. Here's one example - http://stackoverflow.com/questions/3338758/html-fixed-header-table-scrollbar – TimSPQR Mar 31 '14 at 00:00
  • that is true, yeah i saw. But i am wondering for my specific mess :( is there a solution for it with css? and what is it? – user3474037 Mar 31 '14 at 01:37
  • What I've done in the past is to make the "sticky" header in pure HTML and style it. Then below it, roll the data from php in a div. Then slowly adjust the header and php rows to match each other. – TimSPQR Mar 31 '14 at 03:17
  • I have to have the table scrollable within this width – user3474037 Mar 31 '14 at 05:21

1 Answers1

0

Here is an example of code where in a div that contains a series of rows direct from a db table. At the top of the div is a fixed header (NOT part of the table below). This 'label' at the top of the div does not move, and the rows (if there are enough of them) are scrollable in the div. You might be able to adapt this to your situation. You will have to adapt the anchor class 'rootabletitleanch' to a series of divs or a small single row table. And then adjust the width of each of those to match the columns below.

I've update my answer with a specific code I can share. It contains a 'sticky header' - it's from my personal restaurant database.

Here is the HTML header that you need to adjust:

<div class='headerdiv'>
<table class='headertable'>
  <tr>
    <th>Hidden</th>
    <th>Name</th>
    <th>Type</th>
    <th>County</th>
    <th>City</th>
    <th>Phone</th>
    <th>Food</th>
    <th>Amb</th>
    <th>Svc</th>
    <th>Cost</th>
  </tr>
</table>
</div>

<div id='listdiv' class='listdiv'></div>

The div at the bottom is populated with the list from an ajax call:

function fillrestaurantdiv(country2, foodtype2)
  {
   var result = '';  
   console.log(country2 + '-' + foodtype2);
   $.ajax({
           url: 'readrestaurantdatabackend.php',
          type: 'POST',
      dataType: 'JSON',
          data: {
                 country: country2,
                foodtype: foodtype2
                 }
          })
            .done(function(result){
                                   var output = "<table class='restauranttable'>";
                                   $.each(result, function(index, value)
                                                                        { output += "<tr><td id='uniqueid'>" + value.uniqueid +
                                                                                    "</td><td>" + value.restaurantname +
                                                                                    "</td><td>" + value.type +
                                                                                    "</td><td>" + value.country +
                                                                                    "</td><td>" + value.city +
                                                                                    "</td><td>" + value.phone +
                                                                                    "</td><td>" + value.scorefood +
                                                                                    "</td><td>" + value.scoreambience +
                                                                                    "</td><td>" + value.scoreservice +
                                                                                    "</td><td>" + '$' + value.scorecost +                                                                   
                                                                                    "</td></tr>";
                                                                         });
                                                                         output += "</table>";
                                                                         $("#listdiv").html(output);
                                   })
            .fail(function(jqXHR, textStatus, errorThrown){
                                                          $("#listdiv").html('Database not available');           
                                                           console.log(jqXHR.responseText, textStatus, errorThrown);
                                                           });//end ajax
  }
TimSPQR
  • 2,964
  • 3
  • 20
  • 29
  • hi, thank you for helping me, i would like to understand some things what is 'restaurant table'? and for the td's that you have that is all written in java script so its the column titles? or is it each cell? thank you for your help – user3474037 Apr 01 '14 at 01:38
  • It is not working for me, i have updated my question/ code up above to show you what i have done. And i have the same exact java script, i want to see if it is working with my code but it isn't – user3474037 Apr 01 '14 at 02:03
  • Here is the page from whence the code came -http://www.thedentons.us/Food/restaurantlist.php. It works fine in IE10, IE11, Chrome and Firefox. It is a simple page that uses jQuery ajax to read a database table (restauraunt table) and present it to the user (usually me). you can see how I put a simple table th above the div to make it "fixed". The widths of the individual th's were adjusted with CSS to make them match the rows read from the db in the div below. It is not elegant. But it works. – TimSPQR Apr 01 '14 at 09:41