|
-
Mar 17th, 2010, 01:25 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Using a variable for image?
I want to use some variables to reference images in my code. It's suppose to make the code more efficient. I'm having trouble understanding how to set it up. I have a config.php file created for the variables, like this:
PHP Code:
<?
$logo = <img src = images/"logo.gif" />
$side_logo = <img src= images/"side-logo.gif" />
$s_logo = <img src= images/"s-logo.gif" />
?>
I want to use these in my program code now. The original code looks like this:
PHP Code:
public function DisplayHeader()
{
<?
<table width="100%" cellpadding="12"
cellspacing="0" border="0">
<tr bgcolor ="black">
<td align ="left"><img src = "logo.gif" /></td>
<td>
<h1>TLA Consulting Pty Ltd</h1>
</td>
<td align ="right"><img src = "logo.gif" /></td>
</tr>
</table>
<?php
}
To use the variables now, do I want to make it something like this?
PHP Code:
public function DisplayHeader()
{
include "config.php";
echo "<table width=\"100%\" cellpadding=\"12\"
cellspacing=\"0\" border=\"0\">
<tr bgcolor =\"black\">
<td align =\"left\"><img src = \"".$logo."\" /></td>
<td>
<h1>TLA Consulting Pty Ltd</h1>
</td>
<td align =\"right\"><img src = \"".$logo."\" /></td>
</tr>
</table>"
}
or this section;
PHP Code:
public function
DisplayButton($width,$name,$url,$active = true)
{
if ($active) {
echo "<td width = \"".$width."%\">
<a href=\"".$url."\">
<img src=\"s-logo.gif\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else {
echo "<td width=\"".$width."%\">
<img src=\"side-logo.gif\">
<span class=\"menu\">".$name."</span>
</td>";
}
}
and replace with this code?
PHP Code:
public function
DisplayButton($width,$name,$url,$active = true)
{
include "config.php";
echo "if ($active) {
"<td width = \"".$width."%\">
<a href=\"".$url."\">
<img src =\"".$s_logo."\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else {
"<td width=\"".$width."%\">
<img src = \"".$s_logo."\" />
<span class=\"menu\">".$name."</span>
</td>"
}
}
Is this the right way to do this?
-
Mar 17th, 2010, 02:04 PM
#2
Re: Using a variable for image?
First off, you'll need to define your variables correctly.
Code:
$logo = '<img src="images/logo.gif"/>';
// OR
$logo = "<img src=\"images/logo.gif\"/>";
Your variables are strings and therefore need to be encased in either single or double quotes for PHP to understand them as such. There are some implications in using ' vs ", but the thing to recognize in the above example is that if you use double quotes to encase your string, then you need to escape any double quotes that occur in the string so PHP doesn't think you're ending the string prematurely. To escape a character, put a back slash in front of it: \" If you instead choose to use single quotes, escaping of double quotes is not necessary (but escaping of single quotes would be). Makes sense? Also, end those lines with a semicolon.
Moving right along, you probably don't need to make functions out of the code you're using. Functions are useful for code that needs to be reused; outputting a header's markup is not likely to recur on a page. Also, the "public" keyword is irrelevant if you're not within a class.
While not technically wrong, echoing large blocks of markup is discouraged for several reasons (I'll let other members come and rant on this), one of which is that it's just inconvenient. Keep your original HTML and embed your PHP...
Code:
<?php
include "config.php";
?>
<table width="100%" cellpadding="12" cellspacing="0" border="0">
<tr bgcolor ="black">
<td align ="left"><?php echo $logo;?></td>
<td>
<h1>TLA Consulting Pty Ltd</h1>
</td>
<td align ="right"><?php echo $logo;?></td>
</tr>
</table>
<?php
// More PHP...
?>
You can enter and exit the PHP context at any time with <?php and ?>. So enter it, echo your variable, then exit back to HTML. Also note that since your variable contains the full HTML <img> tag, you can't do something like <img src = \"".$logo."\" /> or it would come out as <img src = "<img src="images/logo.gif"/>" />. If you change your variable values to just the image's src ($logo = "images/logo.gif"; ), then it would be appropriate to use <img src="<?php echo $logo;?>"/>
How about try rewriting your second example with the above instructions in mind.
Last edited by SambaNeko; Mar 17th, 2010 at 02:09 PM.
Reason: Forum converted some of my code into a smiley; I object - my code is not happy code.
-
Mar 17th, 2010, 06:14 PM
#3
Thread Starter
Addicted Member
Re: Using a variable for image?
SambaNeko, Thanks for your explanation. It helps but I'm still struggling with turning the PHP off and on. I took the source code from the cd that came with the text.
Ok, now with the section you wanted me to work on. This comes from a file called page.inc. I'm not sure what the .inc extension means, it's not html or php? This is what the book had in case I altered it from the original:
PHP Code:
public function
DisplayButton($width,$name,$url,$active = true)
{
if ($active) {
echo "<td width = \"".$width."%\">
<a href=\"".$url."\">
<img src=\"s-logo.gif\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else {
echo "<td width=\"".$width."%\">
<img src=\"side-logo.gif\">
<span class=\"menu\">".$name."</span>
</td>";
}
}
I tried to follow what you were doing but I don't think I was doing it right. It was really awkard. Sorry if I didn't get it. I don't know if I turned php on too soon or if the "if" statement is suppose to be in the html?
PHP Code:
public function DisplayButton($width,$name,$url,$active = true)
{
include "config.php";
?> //turn off php
if ($active) {
<td width = <?php echo\"".$width."%\"?></td>
<a href= <?php echo\"".$url."\"?></a>
<?php echo $s-logo;\" alt=\"".$name."\"?> border="0" /></a>
<a href= <?php echo\"".$url."\"?><span class="menu"><?php echo".$name.";?></span></a>
</td>
} else {
<td width = <?php echo\"".$width."%\"?></td>
<?php echo $side-logo;?></td>
<span class="menu"> <?php".$name."?></span>
</td>
<?php
//more php
Again, sorry if I didn't get it.
-
Mar 17th, 2010, 09:52 PM
#4
Re: Using a variable for image?
er, no. that's not how it works. I would hope you know by now the difference between HTML and PHP, yes? if not, here is PHP:
PHP Code:
function DisplayButton($width, $name, $url){ include("myfile.php");
if($somevariable == "somevalue"){
//do something
}else{
//otherwise, do something else
} }
this is HTML:
HTML Code:
<html>
<head>
<title>
My page.
</title>
</head>
<body>
<table>
<tr>
<td>My data</td>
</tr>
</table>
</body>
</html>
as you can see, HTML is merely a mark-up language, and PHP is a scripting language. PHP is an embedded language, and so if we have a variable $myVar and want to print it out to the browser, we can use PHP to embed this variable within our HTML:
PHP Code:
<?php $myVar = "Hello world!"; ?> <html> <head> <title> My page. </title> </head>
<body>
<table> <tr> <td><?php echo $myVar; ?></td> </tr> </table>
</body> </html>
from this example, at the top of the script we're defining our variable $myVar with the value "Hello world!"; then, we close our PHP tags because we want to display HTML. within our HTML table, we want to display our variable -- we embed our variable within this table by quickly jumping into PHP (<?php), calling the language construct echo to print our variable $myVar (echo $myVar;), and then jumping out of PHP (?>).
basically -- PHP code needs to be used within the "PHP space" (within <?php and ?> tags), and HTML should be used outside of it.
so, in your case, your IF statement is outside of the PHP space; as soon as you use the closing tag (?>), you are telling the PHP interpreter to stop looking for PHP code to execute until it finds an opening tag (<?php).
I hope that helps you distinguish between the two; if not, please ask questions.
-----
now, to your question. your function is PHP code, but the HTML inside of it is not. thus, we can embed our PHP inside of the HTML when we're doing stuff:
PHP Code:
<?php //define our DisplayButton function function DisplayButton($width, $name, $url, $active = true){
//include our file include("config.php");
//is this active? if($active){ //$active is true -- print HTML ?> <td width="<?php echo $width; ?>%"> <a href="<?php echo $url; ?>"> <img src="<?php echo $s-logo; ?>" alt="<?php echo $name; ?>" border="0" /> </a> <a href="<?php echo $url; ?>"> <span class="menu"><?php echo $name; ?></span> </a> </td> <?php //end if($active) }else{ //$active is false -- print some other HTML ?> <td width="<?php echo $width; ?>%"> <img src="<?php echo $s-logo; ?>" alt="<?php echo $name; ?>" border="0" /> <span class="menu"><?php echo $name; ?></span> </td> <?php //end the function } ?>
now, on top of your confusion of the way PHP works, you have very badly formed HTML as well. if it helps, write your HTML out beforehand and then use PHP to embed data into it.
a quick observation, though -- you seem to have a lot of this going on:
PHP Code:
<td width = <?php echo\"".$width."%\"?>
now, from what it looks like, you're expecting to echo out $width, concatenated with a percentage sign. however, this will not actually print anything (you'll get an error). this is because you're trying to escape a quote before you even tell echo to do anything. you want $width and your percentage sign to be enclosed in quotes (I can only assume) -- but wait a minute. quotes and a percentage sign are simply plain-text. you can do this:
PHP Code:
<td width="<?php echo $width; ?>%">
all you need to do is use PHP to echo your variables, not the plain-text that goes along with it. that would be considered more mark-up, and would belong (generally) in the non-PHP space.
hope that helps! ask more questions if you need more help.
-
Mar 17th, 2010, 10:05 PM
#5
Re: Using a variable for image?
oh, and as an aside: .inc is an extension commonly used to refer to PHP includes. I would suggest using .inc.php instead, as a INC file is technically just a text file. while you can tell your webserver to recognize an INC file as a PHP file for the PHP interpreter, this is generally not the case. if you upload an INC file to your webserver with sensitive information (database passwords, account passwords, whatever else), then anyone at all could look at it by browsing to that file specifically.
generally, it's a bad idea to make any sensitive information public on your webserver. this includes PHP files. you would generally put these sensitive include files in the "top" directory of your webserver, while the files that would include these files would reside in your /www/ or /publichtml/ (or equivalent) directories. the optimal directory structure might look something like this:
Code:
/home/blue1974/ (generally your "root" directory when connecting to an FTP site)
--> my_sensitive_include.inc.php
--> database_connect.inc.php
--> credit_card_storage.php
/www/ (or /publichtml/)
--> index.php
--> signup.php
--> purchase.php
again, hope that helps!
-
Mar 17th, 2010, 10:36 PM
#6
Thread Starter
Addicted Member
Re: Using a variable for image?
Ok, thanks, I probably have a lot of questions but I'll try to look this over for awhile.
about this one you fixed:
PHP Code:
<img src="<?php echo $s-logo; ?>" alt="<?php echo $name; ?>" border="0" />
Do you still put the <img src="<?php echo $s-logo; ?> if the variable $s-logo already equals <img src= images/"side-logo.gif" /> ?
-
Mar 17th, 2010, 11:40 PM
#7
Re: Using a variable for image?
no, but then you would be unable to easily change the alt attribute and whatever else you might want to do. I would suggest storing your images as filepaths, not mark-up. this will make it much, much easier to deal with.
so, $s-logo would contain the filepath "images/side-log.gif" (I saw that you typed the filepath wrong in your post, though it could have been a typo).
-
Mar 18th, 2010, 08:29 AM
#8
Thread Starter
Addicted Member
Re: Using a variable for image?
Your saying then that if the variable already contains the path to the file then you wouldn't want to use this code, in this way?
PHP Code:
<img src="<?php echo $s-logo; ?>" alt="<?php echo $name; ?>" border="0" />
The variable is holding this information:
PHP Code:
$s_logo = "<img src=\"images/s-logo.gif\"/>";
I think SambaNeko pointed out that I will be repeating it and it would look something like this:
PHP Code:
<img src = "<img src="images/s-logo.gif"/>" />.
Just trying to clarify. Your right would be confused about handling the other attributes like the alt tag.
I'm getting confused about the escape characters and integrating it with the php.
I wasn't sure what you meant about my code being poorly formed? I believe that it is. I don't know either well. The code came straight from the source file on my cd. They want me to modify it. This main body of code isn't in a html or php file. It was in the ins that I mentioned and now for some reaon it has become a pas extension. I'm not quite sure how that happened, but it seems to work the same. I'll add the php extension that you recommneded.
I just posted the small segments since that's where the images were stored but the php is turned off and on througout the file and I was getting confused about turning it off and on. I know you and SambaNeko and have been trying to get me up to speed about when to turn off and I'll try it again and see if I can make some sense out of it.
Thanks for helping with this.
This is what this monster of a file looks like and why I didn't post all of it.
PHP Code:
<?php
class Page
{
// class Page's attributes
public $content;
public $title = "TLA Consulting Pty Ltd";
public $keywords = "TLA Consulting, Three Letter Abbreviation,
some of my best friends are search engines";
public $buttons = array("Home" => "home.php",
"Contact" => "contact.php",
"Services" => "services.php",
"Site Map" => "map.php"
);
// class Page's operations
public function __set($name, $value)
{
$this->$name = $value;
}
public function Display()
{
echo "<html>\n<head>\n";
$this -> DisplayTitle();
$this -> DisplayKeywords();
$this -> DisplayStyles();
echo "</head>\n<body>\n";
$this -> DisplayHeader();
$this -> DisplayMenu($this->buttons);
echo $this->content;
$this -> DisplayFooter();
echo "</body>\n</html>\n";
}
public function DisplayTitle()
{
echo "<title>".$this->title."</title>";
}
public function DisplayKeywords()
{
echo "<meta name=\"keywords\"
content=\"".$this->keywords."\"/>";
}
public function DisplayStyles()
{
?>
<style>
h1 {
color:white; font-size:24pt; text-align:center;
font-family:arial,sans-serif
}
.menu {
color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
td {
background:black
}
p {
color:black; font-size:12pt; text-align:justify;
font-family:arial,sans-serif
}
p.foot {
color:white; font-size:9pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
a:link,a:visited,a:active {
color:white
}
</style>
<?php
}
public function DisplayHeader()
{
?>
<table width="100%" cellpadding="12"
cellspacing="0" border="0">
<tr bgcolor ="black">
<td align ="left"><img src = "logo.gif" /></td>
<td>
<h1>TLA Consulting Pty Ltd</h1>
</td>
<td align ="right"><img src = "logo.gif" /></td>
</tr>
</table>
<?php
}
public function DisplayMenu($buttons)
{
echo "<table width=\"100%\" bgcolor=\"white\"
cellpadding=\"4\" cellspacing=\"4\">\n";
echo "<tr>\n";
//calculate button size
$width = 100/count($buttons);
while (list($name, $url) = each($buttons)) {
$this -> DisplayButton($width, $name, $url,
!$this->IsURLCurrentPage($url));
}
echo "</tr>\n";
echo "</table>\n";
}
public function IsURLCurrentPage($url)
{
if(strpos($_SERVER['PHP_SELF'], $url )==false)
{
return false;
}
else
{
return true;
}
}
public function
DisplayButton($width,$name,$url,$active = true)
{
if ($active) {
echo "<td width = \"".$width."%\">
<a href=\"".$url."\">
<img src=\"s-logo.gif\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else {
echo "<td width=\"".$width."%\">
<img src=\"side-logo.gif\">
<span class=\"menu\">".$name."</span>
</td>";
}
}
public function DisplayFooter()
{
?>
<table width="100%" bgcolor="black" cellpadding="12" border="0">
<tr>
<td>
<p class="foot">© TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our <a href ="">legal
information page</a></p>
</td>
</tr>
</table>
<?php
}
}
?>
-
Mar 18th, 2010, 08:54 AM
#9
Re: Using a variable for image?
if the function you posted earlier was straight out of the book, then you may have had a lot of mistypings. I would check it over and make sure.
but, yes -- if you store the <img> tag in your variable then you would need to echo out just the <img> tag. so, I would suggest you only store the path to the image in the variable instead. this will let you change its attributes whenever you're using it in the <img> tag if you want different sizes or whatever else.
anyway, after seeing your entire source code, I would urge you to take a step back (and a step away from this PHP book you have), and find some beginning tutorials on what PHP is and how to use it. If you're planning on getting anything done, there is no reason for you to be copying a [badly written] class that simply calls a bunch of functions to display a page. on top of that, I'm going to assume you're not very familiar with programming in general, and so introducing classes and objects to you without you knowing the other fundamentals of programming really seems pointless to me.
you could try taking a look at W3Schools' PHP tutorial.
-
Mar 18th, 2010, 09:01 AM
#10
Thread Starter
Addicted Member
Re: Using a variable for image?
kows, thanks for the explanation on the directory. All my files are junk and mock ups, nothing anyone would ever want to see, but if I pursue this farther I'll try to understand the directory organization. I'll just try to know for now that there is a more protective way to organize them.
I was hoping if I worked with it a little I could catch on. This text was highly recommended to me as being easy to learn from.
Last edited by Blue1974; Mar 18th, 2010 at 09:24 AM.
-
Mar 18th, 2010, 09:57 AM
#11
Re: Using a variable for image?
if the code you posted is from something early on, then I don't know who told you it would be easy to learn from (unless they assume you are a programmer and are simply learning syntax). if this is an example shown later on, then you didn't absorb much from the rest of the book. either way, this book is looking like it's probably not for you as a beginner. start with the W3Schools tutorial and try to go forward from there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|