Is it posible to use CSS in this way in PHP? [closed] - php

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>

Related

echoing javascript not displaying any data [closed]

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 4 years ago.
Improve this question
I am confused as to why this piece of simple code is not displaying any data in either console.log or .actions class. There are no errors in console and I can see the correct value being returned in the payload window in inspector.
I would be grateful if someone could point out my error. Many thanks
<?php
$value = '10';
echo '<script>';
echo 'var value = ' . json_encode($value) . ';';
echo '$(".actions").text(value);';
echo 'console.log(value);';
echo '</script>';
?>
Make sure that jQuery is imported before your script, try this example.
<?php $value = '10';?>
<p class='actions'>Loading...</p>
<script src="jquery.min.js"></script>
<noscript>Javascript is disabled!</noscript>
<script>
$(function(){
var str = 'value = <?=$value?>';
$('.actions').text(str);
console.log(str);
});
</script>

How to modify HTML inside PHP by CSS [closed]

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 wrote this code
<html>
<head>
<title> page 1</title>
<body>
<style>
a{
margin-left:10px;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = 201102820" ;
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)){
if ($row["major"]=="computer engineerig"){
echo "welcome ". $row["name"];
echo '<img src="tran.png"/>';
}
}
?>
</body>
</html>
but when I run it show me like this
Welcome stephen ICON
the icon(picture) that I put comes in front of the text .
Can I do like this
welcome stephen
ICON
I want the icon (picture) comes under the text.
HTML have a tag called Break :) you should echo this :
echo "welcome ". $row["name"];
echo "<br />";
echo '<img src="tran.png"/>';
you need a line break
echo "welcome ". $row["name"] . '<br/>';
echo '<img src="tran.png"/>';
or
echo '</p>' . "welcome ". $row["name"] . '</p>';
echo '<img src="tran.png"/>';
<a> and <img> are inline elements by default. See this article : CSS display: inline vs inline-block
So you have to put the ICON block in a block element to see a separation between the name and the icon.
Like this for example :
if ($row["major"]=="computer engineerig")
{
echo "welcome ". $row["name"];
echo '<p><img src="tran.png"/></p>';
}
Or adding a <br/> like suggested #Noor Adnan
<?php
//here you can add php
?>
<p>welcome <?php echo $row["name"]; ?> </p>
<br />
<img src="tran.png"/>
<?php
// here you can add your php
?>
If you Separate your HTML and PHP then you can easily add css and HTML inside your PHP.

Use PHP to change background image from user input [closed]

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">';
}

Css In Php not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
So this is the part of my code that I'm trying to make it work :
while ($row = mysql_fetch_array($satt)){
echo "<li";
if($row["satake"] == $fns ) {
echo "class=\"selected\"";
}
echo "><a href=\"home.php?fn=".urlencode($row["satake"]).
"\">{$row["satake"]}</a></li>";
}
And upper in page I have defined these:
<?php
if (isset($_GET['fn'])){
$fns = $_GET['fn'];
$ys = "";
} elseif (isset($_GET['yosef'])){
$ys = $_GET['yosef'];
$fns = "";
} else
{
$ys = "";
$fns = "";
}
?>
And this is the css :
.selected { font-weight: bold; }
It doesn't give me any error and it runs smoothly but doesn't work.
While it is true that you were missing an space, you might want to take these steps in the future to avoid this confussion:
PHP is a template engine. Use it as such (put PHP presentation INSIDE html, and not the other way around).
Check out the dynamic generated solution with the browser inspector (Chrome or Firefox).
So, your code becomes much more readable and easier to spot errors when you do:
while ($row = mysql_fetch_array($satt)){
// Some light logic
$satake = $row["satake"];
$liclass = $satake == $fns ? "selected" : "";
$cleansatake = urlencode($satake);
// The presentation
?>
<li class="<?= $liclass; ?>" >
<a href="home.php?fn=<?= $cleansatake; ?>" >
<?= $satake; ?>
</a>
</li>
<?php
} ?>

echo inside an echo - does it work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .

Categories