Tables inside of divs

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Tables inside of divs

Post by Perry »

How can I put a table inside of a div? I tried doing this.

Code: Select all

echo "<u><b>Shop inventory:</b></u>";
           echo "<div id='invshop1'><div align='left'>";
           echo "<table border='0' width='70%' cellspacing='20'>";
           echo "<tr><td width='25%' valign='top'>";
           echo "</td>";
           echo "<td valign='top' width='75%'>";
           $selectshopinv="SELECT * from materialshop where shop_id='$shop_id' ORDER BY name";
           $selectshopinv2=mysql_query($selectshopinv) or die("could not select shop inventory");
           echo "<table border='1' bordercolor='white' bgcolor='#23A0A8'>";
           echo "<tr><td>Name<font color='23A0A8'>________</td><td>Type<font color='23A0A8'>____</td><td>Modifies<font color='23A0A8'>__</td><td>Price<font color='23A0A8'>_______</td></tr>";
           while($selectshopinv3=mysql_fetch_array($selectshopinv2))
           {
                $mastertable = $selectshopinv3['class'];
                $selectiteminfo="SELECT * from $mastertable where name='$selectshopinv3[name]'";
                $selectiteminfo2=mysql_query($selectiteminfo) or die("could not select item info");
                $selectiteminfo3=mysql_fetch_array($selectiteminfo2);

                echo "<tr><td>$selectshopinv3[name]</td><td>$selectiteminfo3[type]</td><td>$selectiteminfo3[modif]</td><td>$selectiteminfo3[value]</td></tr>";
           }

           echo "</center></div>"; 
But for some reason that div only moved everything down the number of pixels in the div and not just the code right there even tho I closed the div.

Without divs it turns out like this
Image

The one right below the 'Items to add' is not correct there. That table should be under the 'Shop inventory' So as you can see it is very mixed up so I tried using divs, which didn't work as I expected. Does anyone see what I am doing wrong?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Tables inside of divs

Post by Jackolantern »

Tables can go into divs just fine. Since there is a lot of complexity in that code, I suggest first writing up the structure of the table you want first with placeholder values, and then cut it up and add in the PHP after it is displaying the way you want.

Are you nesting a table inside another table? You open a table, a table row and then make some table data cells, and then you immediately start another table within one of the table data cells from the first table. I have a feeling that if you take out all of the PHP, it will be a lot easier to fix.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Tables inside of divs

Post by hallsofvallhalla »

you never closed your table

</table>
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Tables inside of divs

Post by Jackolantern »

hallsofvallhalla wrote:you never closed your table

</table>
That is what I was thinking with the table opened with another immediately opened after it, but wouldn't most browsers have fixed that anyway? I still think there could be more problems that would become obvious without all of the PHP in the way. This is one of the great uses for PHP Designer 7, since it would darken all of the PHP when the HTML is clicked.
The indelible lord of tl;dr
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Tables inside of divs

Post by Perry »

I do need to straighten out that code. I am not sure what I have going on. It has been a long day and I am not thinking completely clear right now. Thanks a lot for both of your help. halls was right, I closed the table and it worked just like I had expected it to.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Tables inside of divs

Post by Xaleph »

Just don`t use tables. Period. Honestly, there`s no real argument to use it unless you are actually displaying TABULAR data. Which, in most cases the tables are being used, is not.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Tables inside of divs

Post by hallsofvallhalla »

Tables used with CSS are awesome and I can find no argument against tables. Tables are 3x easier than CSS and used with CSS can give you what you need with no downside.

Not closing a table with result in the things below it being pushed upwards or other random weirdness. I have seen it a hundred times :)
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Tables inside of divs

Post by Perry »

Xaleph wrote:Just don`t use tables. Period. Honestly, there`s no real argument to use it unless you are actually displaying TABULAR data. Which, in most cases the tables are being used, is not.
Wouldn't you consider a shop inventory tabular data though?
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Tables inside of divs

Post by ConceptDestiny »

I'm actually starting to prefer css over html tables, depending what I'm using it for. Here's a quick example of how I sometimes output db data, such as for an inventory or shop:

Code: Select all

$querydata = mysql_query();
echo "<div style='width: 600px; height: 500px; overflow: auto;'>";
  while($rowdata = mysql_fetch_array($querydata));
  {
    echo "<div style='position: relative;  float: left; width: 150px; height: 100px;'>
          Name: ".$rowdata['name']."<br>
          Type: ".$rowdata['type']."<br>
          Modifier: ".$rowdata['modifier']."<br>
          Value: ".$rowdata['value']."<br>
          </div>";
  }
echo "</div>";
It's a quick example, obviously move the css on the div styles to an external style sheet, etc. If you're looking to increase the speed at which your data is rendered, i.e. if you have a significant amount of data being output from your db, then go with css. I vaguely reckon it's about 40% faster.

Sadly alot of css behaves oddly in different browsers, so that's really the only issue which is preventing websites from becoming entirely css/div, while html tables remains largely more stable.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Tables inside of divs

Post by Xaleph »

Like I said before, tables should not be used. Period. Tables screw pretty much any good template. You limit yourself down. It has limited options, CSS does not apply well to tables so there`s no real reason to use it. So, like I said before, tables should only ONLY be used for tabular data. Is a shop list tabular data? I suppose you can use a table for it. However, the options for the shop are limited, so a simple <ul> will do fine. But what I see happening is more tables nested in tables which are again nested in some layout table.

Let me explain this one thing: HTML is a symantic language. This means, HTML should add structure to your document. Like in Word or other text processors. You don`t write your complete documents in tables right? So HTML is for structure, which has nothing to do style.

CSS is for styling, this means, CSS should be used to visually display/style your document. CSS is not tough, it`s a backward language, I agree, but it works well once you mastered it.
Post Reply

Return to “Beginner Help and Support”