Jump to content

Php If Statement Help


njb182

Recommended Posts

I'm not very experienced with php yet and i need to develop a code for my website that goes something like this:

 

If your on the page "www.website.com/en/light.php" then show the light image.

 

If your on the page "www.website.com/en/dark.php" then show the dark image.

 

Ya get what im trying to do?

Link to comment
Share on other sites

What does that have to do with PHP? Those are two separate pages, so you do not need any programatical differences to show the different images. They can be statically defined in the HTML if you so choose.

 

Now, if you used a single page and passed a variable to determine the image to show, then your questino would be relevant. Examples:

 

www.website.com/en/page?type=light

www.website.com/en/page?type=dark

Link to comment
Share on other sites

What does that have to do with PHP? Those are two separate pages, so you do not need any programatical differences to show the different images. They can be statically defined in the HTML if you so choose.

 

Now, if you used a single page and passed a variable to determine the image to show, then your questino would be relevant. Examples:

 

www.website.com/en/page?type=light

www.website.com/en/page?type=dark

 

Ah! Sorry, i should of explained farther.

 

I have the index file alright. And in there is a a php_require to the file which i want a script to do what im asking.

Link to comment
Share on other sites

So you want to know how to change the color?

 

Use $_GET['type'] to get the color.

 

<?php
$type = $_GET['type'];
if($type == "light") {
$color = "tan"; // Some light color
}
else if($type == "dark") {
$color = "black"; // Some dark color
}
else {
$color = "white"; // Normal color
}
echo <<<END
<html>
<head>
<title></title>
<style type='text/css'>
body { background-color: $color; }
</style>
</head>
<body>
</body>
</html>
END;
?>

Link to comment
Share on other sites

You don't state how you are passing the value to your index page, but I will assume it is sent on the URL as in my example above.

 

<?php

//Light will be the default if no value (or invalid value) passed
$page_type = ($_GET['type']=='dark') ? 'dark.php' : 'light.php' ;
require($page_type);

?>

Link to comment
Share on other sites

So you want to know how to change the color?

 

Use $_GET['type'] to get the color.

 

<?php
$type = $_GET['type'];
if($type == "light") {
$color = "tan"; // Some light color
}
else if($type == "dark") {
$color = "black"; // Some dark color
}
else {
$color = "white"; // Normal color
}
echo <<<END
<html>
<head>
<title></title>
<style type='text/css'>
body { background-color: $color; }
</style>
</head>
<body>
</body>
</html>
END;
?>

 

Close but no. I want it to get an IMAGE.

Link to comment
Share on other sites

You don't state how you are passing the value to your index page, but I will assume it is sent on the URL as in my example above.

 

<?php

//Light will be the default if no value (or invalid value) passed
$page_type = ($_GET['type']=='dark') ? 'dark.php' : 'light.php' ;
require($page_type);

?>

 

No it's not. Its linked by a require_once statement in the index page.

Link to comment
Share on other sites

Ah.

 


<?php
$type = $_GET['type'];
if($type == "light") {
$color = "<img src='light.JPG' />";
}
else if($type == "dark") {
$color = "<img src='dark.JPG' />";
}
else {
$color = "";
}
echo <<<END
<html>
<head>
<title></title>
</head>
<body>
$color
</body>
</html>
END;
?>

Link to comment
Share on other sites

Ah.

 


<?php
$type = $_GET['type'];
if($type == "light") {
$color = "<img src='light.JPG' />";
}
else if($type == "dark") {
$color = "<img src='dark.JPG' />";
}
else {
$color = "";
}
echo <<<END
<html>
<head>
<title></title>
</head>
<body>
$color
</body>
</html>
END;
?>

 

Do I have to use that "type" stuff. I don't want to alter the url at all. I just want it to show the light image if your viewing light.php and dark image for dark.php.

Link to comment
Share on other sites

No you do not.

 

<?php
$type = $_SERVER['PHP_SELF'];  // Get the filename
if($type == "light.php") {
$color = "<img src='light.JPG' />";
}
else if($type == "dark.php") {
$color = "<img src='dark.JPG' />";
}
else {
$color = "";
}
echo <<<END
<html>
<head>
<title></title>
</head>
<body>
$color
</body>
</html>
END;
?>

 

However, that is utterly pointless, as mjdamato said.  In light.php, just show the light image, and on dark.php, just show the dark image.

Link to comment
Share on other sites

No you do not.

 

<?php
$type = $_SERVER['PHP_SELF'];  // Get the filename
if($type == "light.php") {
$color = "<img src='light.JPG' />";
}
else if($type == "dark.php") {
$color = "<img src='dark.JPG' />";
}
else {
$color = "";
}
echo <<<END
<html>
<head>
<title></title>
</head>
<body>
$color
</body>
</html>
END;
?>

 

However, that is utterly pointless, as mjdamato said.  In light.php, just show the light image, and on dark.php, just show the dark image.

Do i have to change the PHP_SELF to anything?

Link to comment
Share on other sites

Do I have to use that "type" stuff. I don't want to alter the url at all. I just want it to show the light image if your viewing light.php and dark image for dark.php.

 

I understand you may be new to PHP, but you are making this much harder than it needs to be by not being very specific.

 

I will state what I think you are trying to do based upon the previous posts.

 

You have two different php files (light.php and dark.php) which determine the background image to show in an index page. You want some logic to determine which file to include using a require_once() function. However, you have not stated "HOW" you are going to determine the value to use to make that choice. I had made an assumption previously that you would pass a value on the URL when calling the index page, such as www.mydomain.com/index.php?type=light

 

In that assumed example the index.php page would hold the content, and the dark.php and light.php scripts wouold be used to detemine the image to show on index.php (as in my first code example above).

 

In the index.php page:

<?php
//Light will be the default if no value (or invalid value) passed
$page_type = ($_GET['type']=='dark') ? 'dark.php' : 'light.php' ;
require_once($page_type);
?>

 

However, some statements you have made can be interpreted to mean that dark.php & light.php are two completly different pages - in which case all of this is unnecessary. If the user calls light.php then they will see what's in that page, the same for dark.php.

 

 

Me:

You don't state how you are passing the value to your index page, but I will assume it is sent on the URL as in my example above.

You:

Its linked by a require_once statement in the index page.

 

That didn't answer the question.

 

Another option that you might not be describing correctly is that you might have the user call dark.php or light.php and each of those pages does a require of the index page. But, again there really isn't enough information to provide relevant help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.