I have this code:
<div style="margin: 0 auto; display:block; width: 916px; overflow: auto;">
<?php echo "";
echo "<i>Owned: $line->phone </i><br><br>";
$query = "SELECT person_id, person.name, person.sex, person.father_id, person.mother_id,PhotosComp.reference as reference
FROM person
WHERE person.id=$currId";
$result = mysql_query($query);
if (mysql_num_rows($result)>0) {
while ($line = mysql_fetch_object($result)) {
echo "<div valign='top'>";
echo "";
echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
echo "";
echo "<a href='details.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b>, <font size='-2'>(";
echo "$line->sex";
echo ", </font><font size='-2'>";
echo "$line->father_id";
echo "<br>";
echo "$line-mother_id";
echo "<br>";
echo "</div>";
}
}echo "";
?>
</div>
The info is shown correctly vertical… but I would like to show lets say 4 results horizontal before it breaks into new line.
So I get this result from the database as it is now:
PICTURE Bob
Mick
Jane
PICTURE Roy
Mack
June
PICTURE Mia
Roy
Jane
PICTURE Lou
Bob
June
PICTURE Bib
Mock
Jine
PICTURE Beb
Muck
Jone
PICTURE Ray
Rob
Mia
And would like it to be shown as this:
PICTURE Bob PICTURE Roy PICTURE Mia PICTURE Lou
Mick Mack Roy Bob
Jane June Jane June
PICTURE Bob PICTURE Roy PICTURE Mia
Mick Mack Roy
Jane June Jane
The results from DB can be 0 to 15–20. I don’t need any upper limit.
Try this , let me know if it works .
if (mysql_num_rows($result)>0)
{
$counter = 0;
while ($line = mysql_fetch_object($result))
{
if($counter %4 != 0)
{
#this will break your div
echo "<div valign='top' style='clear:both;'>";
}
else
{
echo "<div valign='top' style='float:left;display:block;'>";
}
//if ($line->reference = (NULL)) { echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
//else echo "<img src='../temp_pic.jpg' style=float:left; height='50px' border='1'>";}
echo "";
echo "<a href='page.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b><br>";
echo "line->father_id";
echo"<br>";
echo "line->mother_id";
$counter++;
}
echo "</div>";
}
Some scaffolding like this might get you closer to your goal. as #DanFromGermany explained, you need to clear:both underneath your div elements. Try not to put style tags in your markup, unless your page is super optimised or something.
markup
while($result) {
echo "<div class=\"span-3\">";
echo "<div>";
echo "the content father->id line->person_id etc etc";
echo "</div>";
echo "</div>";
}
echo "<div class=\"clear-fix\"></div>";
css
.span-3 {
width:25%;
float:left;
margin:0; padding: 0;
}
.span-3 > div {
margin: 10px; /* or some other value */
}
.clear-fix {
clear:both;
}
Here is a basic bit of code to show pictures in the format you require. You will need to change this to match up with your while loop and also change the clases and css code to match your requirements ie page width etc.
<style>
html {width:100%; background:#ffffff;}
body {max-width: 960px; background:red; margin:0 auto;}
img {max-width: 225px; height: auto;}
.clear {clear:both;}
.no-margin{margin:0 !important;}
.margin-right {margin-right: 20px;}
</style>
<div class="pic-wrap">
<?php
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$count = count($pictures);
$i=1;
foreach($pictures as $picture)
{
if($i % 4 == 0)
{
echo '<img src="'.$picture.'" class="no-margin four">';
}
elseif($i == $count)
{
echo '<img src="'.$picture.'" class="margin-right">';
echo '<div class="clear"></div>';
}
else
{
echo '<img src="'.$picture.'" class="margin-right">';
}
$i++;
}
?>
</div>
Here is an image of the output
Much like #DanFromGermany mentioned this clears floats and also adds a clear at the end by using a count of the results.
$count = count($pictures);
It then determines the last result form this
elseif($i == $count)
With $i being incremented on each iteration of the loop.
$i++;
Hope this helps.
Related
I have a MYSQL database where I store images for reports I can import them to the Tinymce text area but they are being placed below each other. Is there away I can make them place two two next to each other. Some reports have 2 photos and other might have 10 depending on how many was loaded to the DB
This is my current code:
$result = mysqli_query($db, "SELECT * FROM images WHERE file_nr = '$id'");
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='300' height='300' display: inline-block;
src='../../cases_1/overdue/images/".$row['image']."' >";
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
I tried inline-block not working.
I don't think this is the cleanest method but got this working
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='350px' height='350px' style='float: left; padding: 4px;' src='../../cases_1/overdue/images/".$row['image']."' >";
echo "</div>";
}
?>
I have a problem and needed an answer. How can I automatically put the 2 images horizontally.
I think I will use some if(1 ==%3)?
In the example pic I really like to horizontal the 2 images don't know how to do it ?
Here's what I've done:
for($i=0; $rows = $results->fetch(); $i++){
if($dsds=='Commissoner'){
echo $rows['prog_id'].$rows['prog_id'].' '.$rows['prog_name'].' = '.$rows['votes'];
}else {
//this is the part where I put a css
style='margin-left:44px;'><div class='box_img2' style='margin-right:10px;' >";
echo '<img src="candidates/images/'.$rows['image'].'" width="70" height="80px" />'.', '.'<br>'.$rows['lastname'].', '.$rows['firstname'].'<br>'.' = '.$rows['votes'];
echo '<br>';
}
$sdsd=$dsada
?>
<img src="candidates/images/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>
<?php
if ($rows['votes']==0){
echo "<br>";}
else {
echo(100*round($rows['votes']/($sdsd),2)); ?>%<br>
<?php
}
echo '</div>';
}
?>
Here's my css:
.row:before, .row:after { clear:both; }
.row{
display: block;
}
.box_img2 {
float: left;
/*margin-right:85px;*/
text-align:center;
}
.box_img2 img { /* if you want it centered */
display:block;
display: inline-block;
}
Here's my entire codes:
<?php
include('../connection/connect.php');
$result = $db->prepare("SELECT * FROM candposition ORDER BY posid ASC");
$result->bindParam(':userid', $res);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$dsds=$row['posid'];
$resulta = $db->prepare("SELECT sum(votes) FROM candidates WHERE posid= :a");
$resulta->bindParam(':a', $dsds);
$resulta->execute();
for($i=0; $rowa = $resulta->fetch(); $i++){
$dsada=$rowa['sum(votes)'];
}
echo '<div style="margin-top: 18px;">';
echo '<strong>'.$row['pos_name'].' '.'</strong><br>';
$results = $db->prepare("SELECT * FROM candidates,student WHERE candidates.idno=student.idno AND candidates.syearid = '$no'AND posid = :a ORDER BY votes DESC");
$results->bindParam(':a', $dsds);
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
if($dsds=='Commissoner'){
echo $rows['prog_id'].$rows['prog_id'].' '.$rows['prog_name'].' = '.$rows['votes'];
}else {
//this is the part
echo'<?php $src=array("candidates/images/".$rows["image"]); for($i=0;$i<3;$i++){ ?>';
echo '<img src="candidates/images<?php echo .$src[$i];?>" class="image-inner" />';}
$sdsd=$dsada
?>
<!-- <img src="candidates/images/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>-->
<?php
if ($rows['votes']==0){
echo "<br>";}
else {
// echo(100*round($rows['votes']/($sdsd),2)); /
/*?>%<br>*/
/*<?php
}
echo '</div>';*/
}
}
?>
<?php
}
?>
This is the easiest way i can explain using php. Hopes this will help you to understand
.image-inner {
position: relative;
width: 150px;
display: inline-block;
}
<?php
$src = array(
'http://www.google.com/logos/2008/de_doodle4google08.gif',
'http://www.google.com/logos/2012/d4g_poland12-hp.jpg',
'http://www.google.com/logos/2011/colombia-independenceday11-hp.jpg'
);
for($i = 0; $i < 3; $i++)
{
echo "<img src='{$src[$i]}' class='image-inner' />";
}
?>
Display the divs as inline-block like so :
#christmas_promotion_boxes div {
display: inline-block;
}
jsFiddle
I have database table with the following columns (id, title, image, text). So far I only have 3 rows they are:
1 Lorem ipsum dolor [Image-Link] [Text is blank here]
2 [title is blank here] [Image-Link] [Text is blank here]
3 Mediocrem voluptaria [Image-Link] detraxit eleifend pr
This is my code:
HTML/PHP
<?php
$resultSet = $db->query("SELECT * FROM table");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
if ($id <= 3)
{
$images = $rows["image"];
$title = $rows["title"];
echo "<div id=main>";
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
echo "</div>";
echo "</div>";
}
}
}
?>
CSS
body{
position: relative;
}
#main{
position: relative;
width: 70%;
height: auto;
margin: 0 auto;
}
#mainImg{
position: absolute;
width: 65%;
}
#mainImg img{
width: 100%;
}
#mainTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 1.5%;
background-color: rgba(100, 0, 0, 0.7);
}
#mainTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 150%;
opacity: 1;
}
#secDiv{
position: absolute;
right: 0%;
top: 0%;
width: 30%;
height: auto;
}
#secImg{
width: 45%;
float: left;
}
#thirdImg{
width: 45%;
float: right;
}
#secDiv h2{
clear: both;
font-size: 12px;
}
The problem that I am having is that the h2 tag in the else statement is printing the first Title and the third title for some reason. Even when the first title got printed already in the first if statement, it still shows up when $id has to be at least 3. What am I doing wrong?
If we go over your code real fast
if ($id <= 3)
{
//Executes for ID: 1, 2 and 3
if ($id == 1)
{
//Executes for ID: 1
}
if ($id == 2)
{
//Executes for ID: 2
}
else
{
//Executes if ID !== 2
//So this part executes for ID: 1 and 3
}
}
If I understand correctly, you want your last else to execute only for the ID 3. In that case you'll need another IF, or better, take a look at PHP's switch statement (http://php.net/manual/en/control-structures.switch.php).
You script that
if $id == 2 them show secImg else show thirdImg. So if $id != 2, thirdImg shows. So also if $id == 1 thirdImg shows. You should use
elseif($id == 3)
Maybe you want a
else if ($id == 2)
instead of
if ($id == 2)
because if Id == 1
if ($id == 1)
{
// go in here
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
// go in here
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
if Id == 2
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
// go in here
echo "<img id=secImg src=$images>";
}
else
{
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
if Id == 3
if ($id == 1)
{
// go in here
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
// go in here
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
Your are using if condition in a wrong manner. Look at this logic:
$id = 1;
if ($id == 1)
echo "id is 1";
if ($id == 2)
echo "id is 2";
else
echo "id is something else";
If you execute the above snippet it will print both "id is 1" and "id is something else". This is because $id=1 matches with the else part of the if ($id == 2).
The logic should be:
if ($id == 1 )
echo "id is 1"
else
if ($id == 2)
echo "id is 2"
else
echo "id is something else"
If you have all three IDs in result than you can also try this:
if($id <= 3){
if($id == 1){
// code here for id 1
}
elseif($id == 2){
// code here for id 2
}
else{
// code if id is not either 1 or 2
}
}
I have a list of names that are fetched from a while loop coded in PHP.
When I try to display the content of the loop which is embedded in a div, the content gets overlapped.
The style I am using for the content is:
<style>
#cap{
position: absolute;
top: 0px;
right: 0px;
}
</style>
Here is the entire code of the loop content along with style of the div in PHP:
<?php
$result2 = mysql_query("SELECT `player_name` FROM `player_data` WHERE `team_id`=$team2");
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "<div id='cap'>";
echo "" . $name2 . "<br/>";
echo "</div>";
}
?>
How do I get this content $name2 not overlapped?
Something like this:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo '<div class="player">';
echo $name2;
echo '</div>';
}
echo '</div>';
Put the div with that CSS outside the loop:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "" . $name2 . "<br/>";
}
echo "</div>";
I have a generated php table which I would like to apply style in my style sheet, so for example top:15px, left:10px ect..., not sure how call the table and link it with css -
echo "<table border=1>";
for ($i=0;$i<count($calls);$i++){
for ($j=0;$j<count($days);$j++){
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)){
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
}else
echo "<td style='color:gray'>$k</td>";
}
echo "</tr>";
}
echo "</table>";
any help much appreciated, thank you
echo '<table style="top: 15px; left:10px;">';
or
echo '<table class="someClass">';
and then use CSS
.someClass{
top: 15px;
left: 10px;
}
Give the table an id
echo "<table id=\"my_table\" border=1>";
And then use this in the <head> tag of the document:
<style type="text/css">
#my_table {
top: 15px;
...
}
</style>
echo "<table style=\"border: 1px; top:15px; left: 10px;\">";
Is this statement not working?
echo "<table border=1 style=\"top:15px; left:10px\">";
for ($i=0; $i<count($calls); $i++) {
for ($j=0;$j<count($days);$j++) {
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)) {
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
} else {
echo "<td style='color:gray'>$k</td>";
}
}
echo "</tr>";
}
echo "</table>";