Divs are overlapping on accident - php

I have posts that are echoed out of my mysql database. If there is more than one, they are echoed in separate divs in order of decreasing rank number (taken from DB). However, when the divs are echoed, the all overlap on the top. I believe this is a CSS problem. The thing is that each div has several sub divs. I think the "position" attribute might have contributed to this. I would like for each div to be echoed out with about 100px between each one. Thanks.
CODE:
$post = array();
$f=0;
while ($row=mysql_fetch_assoc($g)){
$post[]=$row['post'];
echo "<div id='area'>";
echo "<div id='badge'><span style='color: gray;'>Answered by:</span>";
include 'badge.php';
echo "</div>";
echo "<div id='areapost'><pre>$post[$f]</pre></div>";
$f++;
}
echo "</div>"; /*end area*/
CSS CODE:
#area {
background-color: #fff;
border: 1px solid red;
width:500px;
height: 300px;
}
#badge{
position: absolute;
top: 0px;
left: 0px;
}
#areapost{
position: absolute;
top: 0px;
right: 0px;
height: 300px;
width: 380px;
background-color: #E0E0E0;
overflow: -moz-scrollbars-vertical;
overflow-x: hidden;
overflow-y: scroll;
}
The "area" is the entire post container. The areapost and badge are elements inside "area"

All elements in the page must have a unique id, otherwise you get unexpected behavior.
Fix this, and see where it puts you.

Try moving the opening "area" div tag out of the conditional:
while ($row=mysql_fetch_assoc($g)){
$post[]=$row['post'];
echo "<div id='area'>";
should be:
echo "<div id='area'>";
while($row=mysql_fetch_assoc($g)){
$post[]=$row['post']
since you want area to contain everything else

You almost always need to open and close divs at the same level of looping. Here you are opening the <div id='area'> inside the while loop and closing it outside the while loop. They need to either both in, or both out. Also, your id's ought to be unique, over your whole page, else you should be using classes on those divs.
You also need to not position all these areas absolutely. I've added a content div around everything. Position this absolutely, and the area class relatively. You don't need the styling on #area, change it to .area.
$f=0;
echo "<div id='content'>"
while ($row=mysql_fetch_assoc($g)){
$post[]=$row['post'];
echo "<div id='area-'" + $f + " class='area'>";
echo "<div class='badge'><span style='color: gray;'>Answered by:</span>";
include 'badge.php';
echo "</div>";
echo "<div class='areapost'><pre>$post[$f]</pre></div>";
echo "</div>"; /*end area*/
$f++;
}
echo "</div>"

also try using relative positioning, with 100px space on each. this way each div will be spaced relative to the previous div, rather than one spot, causing them to overlap.

Related

Account info Formatting issues

so I have this little box on my site that displays links about the user when logged in. I just added a feature where it gets how many unread messages the user has and displays it next to messages "Messages (1)". It just messed up the formatting completely and I tried to fix it but it isn't cooperating. I have been fighting it for over an hour, any help would be greatly appreciated.
To see what I am talking about, go to the link and the box is in the top right corner. I am trying to get the links to be inline. So it should read:
My Account Messages (0) Logout
right now, the logout is
in a weird position and I can't seem to resolve it.
URL: http://www.clanrippgaming.net/
Thanks a bunch
Here is the php code:
echo "<div class='logged_in'>";
echo "<div class='welcome'>".$user->data['username'] ;"</div>";
echo "<div class='account'>My Account</div>";
echo "<div class='account'>Messages (".$user->data['user_new_privmsg'].')';"</div>";
echo "<div class='account'>Logout</div>";
echo "</div>";
And the CSS for it:
.logged_in {
background-image:url(images/bg_login2.png);
border:3px solid#000000;
outline:1px solid#BDBDBD;
width:304px;
height:55px;
margin-left:-2px;
padding-top:5px;
}
.logged_out {
margin-right: 22px;
position: relative;
}
.logged_out_txt {
margin-top:-20px;
}
.welcome {
color:#B40404;
font-size:22px;
font-family:"Rockwell";
padding-left:15px;
float:left;
font-weight:bold;
}
.account {
font-weight:normal;
list-style:none;
float:right;
position:center;
padding:0px;
margin-top:-5px;
font-size:16px;
text-decoration:underline;
margin-right:15px;
display: inline;
}
.account a:hover {
color:#D8D8D8
}
echo '<div class="account">Messages ('.$user->data["user_new_privmsg"].')</div>';
I think that should do the trick.
My tip: be consistent, choose what you want to use: double quote for attributes? Okay, then stick with that, don't go switching that over mid-code.

using php to add div class property

I am using WAMP. I want to take background image URL from my database and want to show this in div class 'box'. I tried it by followed way but couldn't succeed. the last background image is appearing on each box while I want to show different images. Code I am using is
<?php
$feild_set = get_all_feilds();
while($feild = mysql_fetch_array($feild_set)) {
$url = $feild['background_image_url'];
echo "<style>
.box {
width: 300px ;
height: 100px;
background-image: $url;
background-visibility: visible;
border: 1px #00FF33;
margin-bottom: 10px;
display: inline-table;
margin-right: 10px;
}
</style>";
echo "<div class=\"box\">";
echo "<a href=\"content.php?feild=" . $feild['id'] . "\" ><block_holder>{$feild['menu_name']}</block_holder></a>";
echo "</div>";
}
?>
Thanx in advance
Try this:
<?php
echo "
<style>
.box {
width: 300px ;
height: 100px;
background-visibility: visible;
border: 1px #00FF33;
margin-bottom: 10px;
display: inline-table;
margin-right: 10px;
}
</style>";
$field_set = get_all_feilds();
while ($field = mysql_fetch_assoc($field_set)) {
echo '
<div class="box" style="background-image:'.htmlspecialchars($field['background_image_url']).';">
<a href="content.php?feild='.htmlspecialchars($field['id']).'">
<block_holder>'.htmlspecialchars($field['menu_name']).'</block_holder>
</a>
</div>';
}
?>
What have I done?
Placed the declaration of the .box CSS class outside the loop so it is only output once
Changed mysql_fetch_array() to mysql_fetch_assoc() as it is less confusing and more efficient
Removed the background-image: property from the .box class
Added a background-image: property to an inline style= attribute for each div and wrapped the URL of the image in url() (this has been undone as it seems the URLs are stored in the database with this already done
Passed data from the database through htmlspecialchars() before outputting it
Corrected the spelling of feild to field where it can be done without breaking the rest of your code
Some general indentation and quote tidying and readability fixes
As it was, your CSS .box class was declared more than once. Because of the cascading nature of CSS, only the values used in the last declaration would have been used - each declaration of a property overrides the last, which is why you were only seeing the last image. You also would not need to declare those details more than once - the whole idea of a class is that you can declare it once and use it multiple times. If you want element-specific properties, use IDs or inline styles (preferably IDs, but I have used inline styles here for simplicity).

HTML/CSS question: Part of div being floated to next line, I want it on the same line

I am loading names/grades from a database which happens perfectly... however, the grade assosciated with the person is for some reason getting floated down to the next line and I can't figure out why, as you can see in this picture: http://i.stack.imgur.com/rqhjp.png
I want that number grade to be on the same line, aligned on the right, but I cant figure out why it's getting pushed down to the next line. Here is my php:
echo '<div class = "name">' . $firstname . " " . $lastname . "<strong>" . '<p align="right">' . $avg . '</p></strong></div>';
And the CSS it refers to:
}
#content .name{
background: #FFF;
float:left;
width: 220px;
padding: 0px;
position: relative;
margin: 0px;
font-size: 1.4em;
border: 2px solid #CCC;
border-radius: 10px;
}
Instead of using p and align, put the grade in a span and float it to the right. You can see it working in this fiddle.
'p' is a block element and takes, by default, 100% width.
You can either set a width on the p tag or float right.
I think that if you want to display every person you should use table instead of divs

How to center this <ul>?

So far i have this code:
function changeGeoLoc($a,$b,$c){
echo "<ul style='list-style-type: none; display:inline;'>";
while(list(,$geoloc) = each($a) AND list(,$Details) = each($b)) {
echo "<li style='list-style-type: none; display:inline; padding-right:5px;'>$Details {$c->$geoloc} </li>";
}
echo "</ul>";
}
which produces this:
Does anyone know how to make the above centered so its in the middle?
Any ideas appreciated.
Thankyou
Update:
I have tried styling as a block and doing margin:0 auto with a fixed with but thats a problem, i need it to stretch along 100% of the width because the fields 'IP, City, Region, Country Name and Country Code will be filled in via GeoLoc services automatically, so having a fixed width of say 500px is not big enough therefore it needs to be the whole width, I want it centred because if there is someone viewing my webpage that there info doesnt use the whole width it would look better centered.
I hope this makes sense.
Try this:
<ul style='list-style-type: none; display:block; width:500px; margin:0 auto;'>
If you want to center the content of the UL without providing a fixed width probably the easiest way is the following:
function changeGeoLoc($a,$b,$c){
echo "<ul style='list-style-type: none; text-align: center;'>";
while(list(,$geoloc) = each($a) AND list(,$Details) = each($b)) {
echo "<li style='list-style-type: none; display:inline; padding-right:5px;'>$Details {$c->$geoloc} </li>";
}
echo "</ul>";
}
Wrap the ul in a div and then add this CSS:
.className {
margin:0 auto;
}
HTML:
<div class="className">
<ul style='list-style-type: none; display:inline;'>
<li style='list-style-type: none; display:inline; padding-right:5px;'>Stuff</li>
</ul>
</div>
Change the ul style to block

How to prevent HTML element moving when resizing window?

I have a simple php page, with the main content placed in a table which is centred. I want to place a menu which starts at the upper right corner. The code is like this:
echo "<table class=\"center\">";
while($row= mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>";
... content...
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table class=\"topRightTable\">";
while($row= mysql_fetch_array($query2))
{
echo "<tr>";
echo "<td>";
... content ...
echo "</td>";
echo "</tr>";
}
echo "</table>";
With this css:
table.center
{
margin-left: auto;
margin-right: auto;
text-align: left;
}
table.topRightTable
{
position:absolute;
top:50;
right:50;
}
body
{
min-width: 600;
}
This works fine, until the window resizes. Then the right menu will float along with the window, be positioned over other elements. How to avoid the menu item moving along with the window?
I have tried to set the min-width of the page to 600, and hoped this would prevent the right menu from moving further, but with no luck.
The qustion is: how to prevent the right-menu-table from colliding with the center-table
The other answer is correct. Assuming you table's parent is body:
body, html{
margin:0px;
padding:0px;
width:100%;
min-width:600px;
}
table.center {
margin-left: auto;
margin-right: auto;
text-align: left;
}
table.topRightTable{
float:right;
}
If the <body> is not the parent, change it to .parent{ min-width:600px;}
You've positioned the table absolute which means it won't be rendered in the same plane as other elements. If you want the table to stay position:absolute, make the other elements also positioned absolute and put all of them into a container that's positioned:relative with a set width. Or (even better) float the elements.

Categories