Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 08:42 PM   PM User | #1
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Need help with simple print

Okay so this is what I'm trying to do;

In a loop I want to print different divs that all have a different background image (the background image is stored in a table in a row called "thumbnail").

Code:
 print("
<div id=\"thumbnail\" style=\"background: url(\"images/portfolio/{$row['thumbnail']}\"); width: 240px; height: 160px;\">
				
title
				
</div>
");
But only the titles show up, not the background.

I'm totally not familiar with anything of PHP and this seems very simple, but I have no idea on what I do wrong.

Could someone maybe help me out?

Last edited by apotd; 01-12-2013 at 10:15 AM..
apotd is offline   Reply With Quote
Old 01-10-2013, 09:12 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
PHP Code:
print('<div id="thumbnail" style="background: url("images/portfolio/'.$row['thumbnail'].'"); width: 240px; height: 160px;">title</div>'); 
Use that (just cleaner), and could you post what the result is?

I have a feeling you may be forgetting the file extension
TFlan is offline   Reply With Quote
Old 01-10-2013, 09:14 PM   PM User | #3
jimutt
New Coder

 
Join Date: Sep 2011
Posts: 47
Thanks: 1
Thanked 15 Times in 14 Posts
jimutt is an unknown quantity at this point
I would write it like this:
PHP Code:
echo '<div id="thumbnail" style="background: url("images/portfolio/'.$row['thumbnail'].'"); width: 240px; height: 160px;\">title</div> '
It's by the way better practice to use echo instead of print as echo is slightly faster than print and there are therefore no reason to use print. Well if you don't need the return value of print in an expression for example.

EDIT: oops, didn't see you were quicker than me TFlan

Last edited by jimutt; 01-10-2013 at 09:19 PM..
jimutt is offline   Reply With Quote
Old 01-10-2013, 09:28 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
BTW if this is in a loop then you are giving all your divs the same id; id's must be unique on the page.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-10-2013, 09:40 PM   PM User | #5
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Mhh I tried it, but it's still not displaying any background image.

My extension is included in the table at this moment, when I print (I'll use echo from now on) them just as images like this:

Code:
<img src=\"images/portfolio/{$row['thumbnail']}\" alt=\"{$row['title']}\" title=\"{$row['title']}\" />
they do show up. I'm really confused now.

Quote:
Originally Posted by AndrewGSW View Post
BTW if this is in a loop then you are giving all your divs the same id; id's must be unique on the page.
Yes I was already thinking that, but how do I do that? For example give it the name of some unique ID in the table or? Or just make a class of it or won't that work?

Last edited by apotd; 01-10-2013 at 09:42 PM..
apotd is offline   Reply With Quote
Old 01-10-2013, 11:01 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If the DIVs don't need IDs then you could just add the same class-name to each for styling. Otherwise you could number them sequentially:

PHP Code:
$i 0;

echo 
'<div id="thumbnail"' $i 
$i += 1
Try clearing your browser's cache; and use the browser's Console - generally, right-click and Inspect Element - to see if the background image is there, but just not displaying for some reason.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-10-2013, 11:55 PM   PM User | #7
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Thanks for that code. When inspecting the element I'm getting this:

Code:
<div id="thumbnail" style="background: url(" images="" portfolio="" 1.png");="" width:="" 240px;="" height:="" 160px;"="">
				
title
				
</div>
The 1.png of course is the image but I don't understand the rest of it anymore.
apotd is offline   Reply With Quote
Old 01-11-2013, 12:13 AM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
The quotes " are all out-of-whack?! View the page, right-click to View Source, find and copy the relevant HTML; post it here. And re-post your echo statement as it is now.

Try this:

PHP Code:
echo '<div id="thumbnail" style="background: url("images/portfolio/{$row["thumbnail"]}"); width; 240px; height; 160px;">'
(it doesn't show errors in my editor )
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-11-2013 at 12:21 AM..
AndrewGSW is offline   Reply With Quote
Old 01-11-2013, 02:15 PM   PM User | #9
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Code:
style="background: url(" images="" portfolio="" 1.png");=""
What the hell is that?

Please post your code for what assigns the value to $row["thumbnail"]

If you read that, you'll see that it's all wrong. It is trying to find an image at 'images="" portfolio="" 1.png' Which as you might of guessed is not a valid URL
TFlan is offline   Reply With Quote
Old 01-12-2013, 10:15 AM   PM User | #10
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Okay so I just removed the entire print I already had and simple replaced it with the echo posted here, and now it works fine! I'm still confused about why it have this weird result but at least I'm glad it's solved, thanks!
apotd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:52 AM.


Advertisement
Log in to turn off these ads.