Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Started working in PHP for the first time this past week. I'm trying to make the background image change depending on the user input from a form. Here is the PHP code:
if ($gender == 'Male')
echo '<body style="background-color: red">';
if ($gender == 'Female')
echo '<body style="background-image: url("../images/female.jpg")">';
I am able to change the background color based on selection, but when I try to change it to an image nothing happens. Image is in the correct path. Not sure if it can even be done this way. I know there are probably other ways to do it, but probably not on my php coding level (just a beginner).
Thanks for the help.
Without setting CSS class:
if ($gender == 'Male')
echo '<body style="background-color: red">';
if ($gender == 'Female')
echo '<body style="background-image: url(../images/female.jpg)">';
But it will be good to define CSS class as said #hungerstar
your css file:
.male {
background-color: red
}
.female {
background-image: url(../images/female.jpg)
}
and your php code with css classes:
if ($gender == 'Male')
echo '<body class="male"';
if ($gender == 'Female')
echo '<body class="female">';
try this, generate class in your css ;)
if ($gender == 'Male'){
echo '<body class="bg-male">';
}elseif ($gender == 'Female') {
echo '<body class="bg-female">';
}else{
echo '<body class="default">';
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm trying to use CSS within PHP code but it seems that I can't use CSS in the way that I'm trying. why?
<?php
$username = "Daved";
$password = "hello!";
?>
<html>
<head>
</head>
<body>
<?php
echo "<div style="font-size: 32px; background-color:#f00; color:#fff; width:200px;">And Operation</div>";
if($username == "Daved" && $password == "hello!"){
echo "<p>The Password and UserName are correct!</p>";
}
else {
echo "<p>You are not a member!</p>";
}
?>
</body>
</html>
Ok, It's a true way to use CSS like this, or I must to try using style in another way?
Actual Problem is your echo statement. It should be noted that the echo prints whatever is in between " " or ''. Hence your statement echo "<div style="font-size: 32px; background-color:#f00; color:#fff; width:200px;">And Operation</div>"; is closing at style=". Hence your code is not working change it to echo "<div style='font-size: 32px; background-color:#f00; color:#fff; width:200px;'>And Operation</div>";. It will surely work.
Its possible but you made a mistake
echo "style=" font-size:32;
Php will search for the method font-size:32.
It wilp not find it and this causes an error.
Use "" to define the string and use ' ' for inline css:
echo"style='font-size:32px'";
Try this it may helps. I just modified your code little bit in the 1st echo. It just the issue of using quote operator. Thanks
<html>
<head>
</head>
<body>
<?php
echo '<div style="font-size: 32px; background-color:#f00; color:#fff; width:200px;">And Operation</div>';
if($username == "Daved" && $password == "hello!"){
echo "<p>The Password and UserName are correct!</p>";
}
else {
echo "<p>You are not a member!</p>";
}
?>
</body>
</html>
I am new to PHP and am coding a template file for a Joomla K2 item layout.
I have an 'extra field' $extrafields[15] configured which outputs as "Yes", "No" or "". $extrafields[16] is a text string.
I have this code, which works but would appriciate advice on how to simplify it, as I know it is probably a bit crude!
if (!empty($extrafields[15])):
if ($extrafields[15] == "Yes") {
echo "<span class=sgl-bold>Sponsored by: </span>";
}
if ($extrafields[15] == "Yes"):
if (!empty($extrafields[16])):
echo $extrafields[16];
endif;
echo "<br>";
endif;
endif;
I'd be inclined to do something like this:
if(!empty($extrafields[15]) && !empty($extrafields[16])){
if($extrafields[15] == "Yes"){
echo "<span class=sgl-bold>Sponsored by: </span>";
echo $extrafields[16];
echo "<br>";
} //endif not empty
} //endif yes
You can make your code more concise with just some simple tweaks:
Get rid of redundant if-clauses where possible,
combine if-clause conditions where it makes sense, and
don't use the alternative syntax for your control structures to cut down noise.
The following snippet preserves the same functionality than your original attempt but is imho more understandable.
if (!empty($extrafields[15]) && 'Yes' === $extrafields[15]) {
echo '<span class=sgl-bold>Sponsored by: </span>';
if (!empty($extrafields[16])) {
echo $extrafields[16];
}
echo '<br>';
}
That said, judging from the context, you probably want to go with the solution BigScar posted here.
To make this snippet even more understandable, you should consider working on the data structure (though I assume that this is something forced upon you by Joomla):
instead of a values in a numeric array like $extrafields[16] use speaking variable names like $showSponsor or the likes, and
instead of the string values of 'Yes', 'No' and '' use boolean values true, false and null.
Remember:
There are only two hard things in Computer Science: cache invalidation and naming things.
if (#$extrafields[15] == "Yes") {
echo "<span class=sgl-bold>Sponsored by: </span>";
echo #$extrafields[16];
echo "<br>";
}
I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">
I'm quite new to PHP now that I've started working with wordpress.
I'm trying to get something to work by using 'if'
Essentialy what I'm wanting to do is
If the status is equal to Open then return Link
If the status is not equal to Open then return Link
Here's what I think would work:
<?php
if ($status) == (open) {
echo "id=flashing"
}
?>
Obviosuly, I'm assuming this doesn't work but what I'm wanting to do is create a link
Any help?
This is a really basic PHP syntax question; please read some documentation, and look at some examples before asking for help with every piece of code you write.
There is a comprehensive online manual for PHP, with many examples. It is available in multiple translations, in case English is not your first language.
Things you have wrong in your example, with links to relevant pages of the manual:
no semi-colon to end the statement
no quotes around the string open
brackets in the wrong place in the if statement
The text of your question also confuses return and echo, which have very different meanings.
Does this help?
if ($status == 'open') {
echo 'Link';
} else {
echo '<a href="#" >Link</a>';
}
Just use the following code:
<?php
if ($status == 'Open') {
echo 'Link';
}
else {
echo '<a href="#" >Link</a>';
}
?>
<?php
if ($status == 'open')
{
echo 'Link';
}
else
{
echo '<a href="link" >Link</a>';
}
?>
Assuming Open is a string, it can be written like this (alternatively to the other answers):
echo '<a href="#" '.(($status == 'Open') ? 'id="flashing"':'').'>Link</a>';
Here is the code I am trying to make work. As you can see, the code as it stands will call a javascript window and that works well. I now want to add the lines that are commented out, in order to make the window display or not display conditionally. I have tried many combinations of escaping quotation marks, or substituting them, but nothing works properly. Usually I lose the link action along with a display of some of the code attached to the link. Could someone have a look and see if they can propose a code solution.
<?php
//if ($review != 0)
//$yes2 =
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);'NAME='var basestring='window'+new Date().getTime();' title=' Results of quiz 'onClick=window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
//echo $yes2;
//if ($review != 1)
//echo "";
?>
If it is any help, I'm using this code elsewhere on the page and it works fine.
<?php
if ($review != 0)
$yes2 = "REVIEW";
echo $yes2;
if ($review != 1)
echo "";
?>
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);' NAME=\"var basestring='window'+new Date().getTime();\" title=' Results of quiz ' onClick=window.open('check/check.php?quizTitle=\". urlencode($quizTitle) .\"', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
Copy this as it is....
There's no need for all the escapes, you should embed the PHP code in the HTML, not the other way around. Example:
<?php if ($row['passState'] == 0): ?>
<p>
<a href="javascript:your_code()">
Check your answers
</a>
</p>
<br />
<?php endif; ?>
I had a hard time deciphering what your javascript was trying to actually do, but this will make it a lot easier for you to read and write HTML without worrying about escaping quotes, using \n characters, etc.
http://php.net/manual/en/control-structures.alternative-syntax.php
You can use addslashes method to escape quotes
PHP Manual
Your onClick needs to have a quote(s) after the =:
...f quiz' onClick=\'window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."',