PHP while loop with HTML - php

I want to make some mini-blog using php, the code is next:
while($row = #mysql_fetch_array ($fetch)) {
echo '<div class="content-posts">';
echo $row['bio'] ;
'</div>';
}
the .content-posts Div is width:800px; and height 250px;
and I have them 5x echoed. but they go one over another (please check picture to see how it looks)
http://i.imgur.com/N5ogsMu.png
As you can see on picture, not only that divs go one over another, but also every next post goes a bit to the right, duo to border. if I delete border in CSS, they will be in same vertical line, but they will still be one over another.
can anyone suggest me proper code?
Thanks in advance!

while($row = #mysql_fetch_array ($fetch)) {
echo '<div class="content-posts">';
echo $row['bio'] ;
echo '</div>';
}
should work. Check the error-output settings of your PHP configuration. Your code above should result at least in a warning.

Related

"echo" session and logout in the same line

Currently when i showing my username(through session) and logout function , both of them is in different lines
Image: http://prntscr.com/nc2v1b ( This is output )
What i want : https://prnt.sc/nc2w23
Username is on the right side of "Logout"
First of all, remove this part style='float:right;'
Using float:right will move the text right.
Second, need some spacing here:
echo $_SESSION["username"]." "; // you can use `|` to separation
echo '<span>Logout</span></li>';
Float right will push everything to the right (last item being the one the the most on the right).
Your code seem to work if you need to have everything on one line though.
<?php
session_start(); // Right at the top of your script
?>
<li class='active'>
<?php
if($_SESSION['logged']==true)
{
echo '<span>Logout</span>' . $_SESSION["username"];
}
else
{
echo '<span>Login/Register</span>';
}
echo '</li>';
?>

:nth-child() issue selecting every second div

I am trying to give every second div in my container a different background color compared to the first one. The issue I have is, that there is a JavaScript code in between the divs. Here is an example:
echo '<div class="holder">';
echo '<script type="text/javascript"></script>';
echo '<div class="list_item"></div>';
echo '<script type="text/javascript"></script>';
echo '<div class="list_item"></div>';
echo '<script type="text/javascript"></script>';
echo '<div class="list_item"></div>';
echo '<script type="text/javascript"></script>';
echo '<div class="list_item"></div>';
echo '<script type="text/javascript"></script>';
echo '<div class="list_item"></div>';
echo '</div>';
When I now add the following css code:
.holder .list_item:nth-child(even) {
background-color:#fff;
}
it will give all of the divs the white background color.
Does anyone have an idea how to solve this???
P.S: I changed the css code to nth-child(odd) as well to test it. But that didn't work either.
You need nth-of-type instead of nth-child. This will only take <div> tags into account, regardless of what is in between of them.
The :nth-child() pseudo-class will count all siblings sharing the same parent.
Since you have multiple element types in the container, and you are targeting only the divs, you can skip over the script elements by using :nth-of-type() instead.
:nth-of-type() matches only elements of the same type.
So when you say:
I am trying to give every second div in my container a different background color compared to the first one.
Try something like this:
div:nth-of-type(even)
There are two child elements per entry. There is a script child and a div child. You could either address the divs using :nth-child(4n+1) (or +3 for even), or you could use the :nth-of-type selector to just address the div elements:
:nth-of-type(odd / even) {…}

$_GET not working when trying to view php image in a lightbox style

I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.

div inside php not worrking

I am trying to style a sentence within php by trying to echo the div. However the styling is not being applied. The css is correct, I think I have something wrong on how I am calling a div withing php. Can you please advise.
This what I have tried:
echo '<div id="sumtitle">' . "<tr><td>" . "This is a test" . "</td></tr>" . '</div>';
tried also like this:
echo '<div id="test">Test sentence</div>';
thanks
PHP is not responsible for styling.
You have to check your styles file or whatever.
Check your HTML and CSS.
PHP has nothing to do here.
The code looks OK, although the multiple use of the . in the first is unnecessary. You could just put:
echo '<div id="sumtitle"><tr><td>This is a test</td></tr></div>';
BTW: Are you using a <table> for your <tr> and <td>?
Regardless, the styling should work fine if you use something like:
#sumtitle { font-style: italic; }
or
#test {...}
I'd double check the CSS! :)

CSS Class on end MySQL row

I am having trouble creating a solution that will target the end row of a MySQL query. Currently I have a foreach function that works through the query and displays each one as a div with the information inside:
<?php $residents = Resident::find_all();
foreach($residents as $resident): ?>
<div class="submenu">
<p class="menuitem submenuheader"><?php echo $resident->name; ?></p>
<img src="images/<?php echo $resident->image_path(); ?>" width="250" class="image" />
<p><?php echo $resident->info; ?></p>
</div>
.submenu currently has a bottom border. I need to remove this on the last row returned. I have looked at DESC LIMIT 1, however this requires another MySQL query and could make things very messy...
Addd this to your CSS:
.submenu:last-child { border-bottom: 0; }
Note: this is not supported by IE < 9.
You could switch to putting the border on the top of the element, and use the :first-child pseudo selector in CSS to remove it.
http://reference.sitepoint.com/css/pseudoclass-firstchild
The :last-child selector would be nice, but it's not supported in IE before version 9, so it's not a good idea to use it if you want compatibility.
If you separate your HTML and PHP a little this is easily achieved:
<?php
function echoBlock($resident,$pClass="menuitem submenuheader") {
echo "<div class=\"submenu\">\n<p class=\"$pClass\">\n";
echo $resident->name;
echo "</p>\n<img src=\"images/";
echo $resident->image_path();
echo "\" width=\"250\" class=\"image\" />\n<p>";
echo $resident->info;
echo "</p>\n</div>\n\n";
}
$residents = Resident::find_all();
$last=count($residents)-1;//2 element array last pos is 1
for ($i=0;$i<$last;$i++) {
echoBlock($residents[$i]);
}
echoBlock($residents[$last],"menuitem");
?>
echoBlock (which could easily be a method on a class) requires the calling code to know about the classes it uses, which isn't really separating intent but it does prevent the need for an if branch on every loop. That being said it would be less efficient but perhaps more usable to set it up as:
function echoBlock($resident,$isLast=false) {
$pClass="menuitem".($isLast?"":" submenuheader");
//...
Which then doesn't need the caller to know anything about what echoBlock does.
You could try and pop the array using array_pop(), to get the last value out of the array and then inputing it using the special class after the foreach loop.
What about
Instead of echo'ing each line one by one, create one big string in PHP, search for the last entry of "submenu" and change the class.

Categories