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).
Related
I want to create a div where an image is on the left side and the text on the right side. I want to create this with PHP. It looks like this in Firefox:
but the situation is different in Chrome on Windows: But on Ubuntu appears totally correctly!
I create the HTML with this PHP script:
echo'<div class="callout large clearfix card">';
echo'<div class="float-left">';
echo '<img src="img/'.$card['cardimg'].'">';
echo '</div>';
echo'<div class="float-right carddescription">';
echo '<div class="title">';
echo $card["description"];
echo "</div>";
echo "<br>";
echo '<div class="additionalinfo">';
echo htmlspecialchars_decode($card["additionalinfo"], ENT_QUOTES);
echo '</div>';
echo "<br>";
echo '<div class="creator">';
echo "creator: ".$card["uploader"];
echo '</div>';
echo '</div>';
echo '</div>';
And I use foundation CSS with this few rules:
#media screen and (min-width: 800px) {
.card{
margin-top: 2%;
margin-bottom: 2%;
}
.card img{
max-width: 20em;
height: auto;
}
.card .carddescription{
max-width: 70%;
}
.card .title{
font-size: 2.5em;
background-color: black;
color: white;
padding-left: 5%;
padding-top: 1%;
padding-bottom: 1%;
}
.card .additionalinfo{
font-weight: bold;
padding-top: 1%;
}
.card .creator{
padding-top: 2%;
text-align: right;
font-style: italic;
}
}
I also have very similar rules for the screens which are smaller, but those work correctly, as I see, on Chrome too.
How and why can this happen? I don't use any browser-specific rule as I know. How can I solve this problem?
If you are using PHP with the the Foundation CSS framework, you should check out a package I wrote to address problems like this, PHPFUI. It is a completely object oriented PHP solution to producing web pages with PHP. Basically you use patterns in the framework to solve problems like this. I wrapped all the basic Foundation concepts in PHP, so you don't even need to figure out the HTML, which can get pretty gnarly. Of course you could write all that HTML by hand, but why bother? It is a solved problem. You don't write assembly language for the same reason, we have tools (languages, including markup), to abstract all this. Just write to the concept and let the tools do the work.
To do what you want, it would look like this:
namespace PHPFUI;
// Your autoloader here
include '../vendor/autoload.php';
$page = new Page();
$page->addStyleSheet('/css/styles.css');
$mainColumn = new \PHPFUI\Cell(12);
$mainColumn->addClass('main-column');
$gridX = new GridX();
$colA = new Cell(4);
$colA->add(new Image('https://foundation.zurb.com/sites/docs/assets/img/rectangle-1.jpg'));
$gridX->add($colA);
$colB = new Cell(8);
$colB->add(new SubHeader('PHPFUI Rules!'));
$colB->add('This is some text next to the photo on the left');
$gridX->add($colB);
$page->add($gridX);
echo $page;
Pretty simple!
First question, so be forgiving if you can:
Using PHP I've created a site where users enter info and it is saved to database. Users then style each entry (font, size, color, and location). A string is saved into the database for each parameter, and then I use PHP to echo out a DIV with the custom styling:
echo "<div id=\"customized\"";
if(isset($backtype)){if($backtype == "image") {echo "style=\"background-image:url('".$backurl."');
background-repeat: no-repeat; background-size: 400px 200px\"";}}
if(isset($backtype)){if($backtype == "color"){echo "style=\"background-color:".$backcolor."\"";}}echo ">";
if($namepos !== ""){echo "<div id=\"".$namepos."\" style=\"font-family:".$namefont."; font-size:".$namesize."; color:".$namecolor.";\">".$name."</div>";}
After getting strings from database this becomes:
<div id="customized" style="background-image:url('raptor.gif'); background-repeat: no-repeat; background-size: 400px 200px ">
<div id="3_1" style="font-family:Courier New; font-size:20pt; color:#f80a8c;">Text here</div></div>
When this DIV is created all the inline styling works, but the location it display is supposed to be dictated by the $namepos, which echoes out "3_1".
I have an external CSS as follows
#customized {
border: 1px solid grey;
margin: 3px;
position: absolute;
top: 7%;
left: 1%;
width: 400px;
height: 200px;
}
#3_1{
position: absolute;
white-space: nowrap;
top:10%;
left:5%;
}
The final output completely ignores this CSS for the div that is produced by doing echo $namepos, but works perfectly fine for the containing div id=" customized".
I fear that I already know the answer and that it just doesn't work for the dynamically named DIV, but I'm an amateur and if this is true then it's a serious setback, so I come here hoping for a savior.
Any help would be greatly appreciated.
It was the DIV starting with a number, wouldve never figured that out. much thanks for speedy reply. will now sleep without debugging nightmares.
I have a title in my header that changes depending on the page you're on using this simple PHP code..
<?php if(empty($title)) $title = "ASK REAL QUESTIONS, <br>GET FREE ANSWERS.";
echo $title; ?>
Here is an example of a page titled "QUESTIONS AND ANSWERS".. The title would say "QUESTIONS.ANSWERS" instead of "ASK REAL QUESTIONS, GET FREE ANSWERS."
<?php
$title = "QUESTIONS<br>.ANSWERS";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
and the CSS of $title..
.title {
margin: 0 auto;
color: #fff;
box-sizing: border-box;
line-height: 24px;
padding-top: 12px;
font-size: 125%;
position: absolute;
text-shadow: 2px 2px 5px #313131;
display: inline-block;
text-align: center;
}
Now here's what I want...
Where it would say "QUESTIONS.ANSWERS".. I want the QUESTIONS to be colored #fff as declared in the CSS... but I want the ".ANSWERS" to be colored #39f.. how can I declare this in PHP?
I tried this... but I get syntax errors.
<?php
$title = "QUESTIONS<br><font color="#39f">.ANSWERS"</font>;
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>
THANKS! :)
PHP can be injected into css. How I would do this is make my .css file a .php instead and preface my css scripts with a <style> tag. Then I would, in $title, add a span tag in front of both QUESTIONS and ANSWERS with different id's. Then I would go into my new php file and add conditional statements that dynamically change the color of the text when my conditions are met.
For example:
We place <span> tags in front of the two words and give them unique id's. This acts as a specific reference point for css.
<?php
$title = "<span id='question'>QUESTIONS</span><br>.<span id='answer'>ANSWERS</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>
$path is what changes so we will use that as our condition. (As that is what will change when we want the style to change.
title in my header that changes depending on the page you're on using
We then change our title.css to titlecss.php. It look like this now.
<style> //need style tags now that it is .php document
.title {
margin: 0 auto;
color: #fff;
box-sizing: border-box;
line-height: 24px;
padding-top: 12px;
font-size: 125%;
position: absolute;
text-shadow: 2px 2px 5px #313131;
display: inline-block;
text-align: center;
}
//with the new id's we inject a php tag after `color:` that echoes the new color
//based on the conditional statement we make (still in `titlecss.php`):
#question {
color: <?php
if $path === "(whatever $path would be for this style)"{
echo "color: #fff;" }
?>
}
#answer { } //same as #question
</style>
Now when $page meets our requirements titlecss.php will dynamically change the color of the text.
In header.php you will need to use an include function instead of <link> to bring titlecss.php to the page.
Your code not working because you closed string with " character before it's really ends.
Try this (I changed old deprecated tag "font" to "span" and replaced double quotes to single quote):
<?php
$title = "QUESTIONS<br><span style='color:#39f'>ANSWERS</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>
I am using a bunch of divs (created with PHP) to generate a block of clickable elements. What I need to do is apply some styles to these generic elements, rather than to specific ones, yet using the code below seems to be invalid.
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div:hover {
background-color: blue;
}
<div id="Container">
<div style="background-color: red; width: 100px; height: 100px;">
</div>
http://jsfiddle.net/XD2eZ/
So I am not sure if it is an issue that a generic div element cannot be styled as a sub-element AND have a :hover attribute that operates properly. I know that classes or id's can be specified to handle this, but have thousands of unique divs. I also cannot use
#Container:hover div{ background-color: blue;}
As it ALSO seems to be invalid, but I need to select the one element from a block, and not all at once.
Any ideas here? Thanks in advance.
This will work if you remove the background color from the HTML, and apply it using css:
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div {background-color: red;}
#Container div:hover {
background-color: blue;
}
Example: http://jsfiddle.net/XD2eZ/1/
The reasone is CSS Specificity - a style attribute rule is much more specific (stronger) than an ID + element rule.
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.