I know that it's not possible to style any given PHP but I do know that it is possible to style the output HTML that the PHP provides you with. I was wondering if there was any method available for me to use that would enable me to style the output from my IF ELSE statement.
if ($result != false) {
print "Your entry has successfully been entered into the blog.";
}
Just use HTML in your printed string. Nothing fancy needed.
if ($result != false) {
print "<p class=\"success\">Your <strong>entry</strong> has successfully been entered into the blog. <img src=\"smileyface.png\" alt=\"Smiley face\" /></p>";
}
You mean this?
if ($result != false) {
print "<span style='color:#ff0000'>Your entry has successfully been entered into the blog.</span>";
}
What I like to do, just to make life simple, is to switch between HTML and PHP in my page. For example,
<?php if ($fooSuccess = true) {?>
<p><span style="color: green;">Success!</span></p>
<?php } else {?>
<p><span style="color: red;">Error!</span></p>
<?php } ?>
You need to print HTML then.
if ($result != false) {
print "<b>Your entry has successfully been entered into the blog.</b>";
}
Or even
if ($result != false) {
print "<div class='Style1'>Your entry has successfully been entered into the blog.</div>";
}
Related
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
i'm using JSON to get data and then PHP to display. so...
i'm showing everything available to a person and i want to echo a message when the loop is blank/empty that "there's nothing available" because right now it just shows a blank screen when there is no data... any ideas??
<?
foreach($json['available'] as $r) {
echo '<li>' .$r['item'].'</li>';
}}
?>
Just use an if statement and check if $json['available'] is empty with empty().
if( empty( $json['available'])) {
echo '<li>No items are available</li>';
} else {
foreach($json['available'] as $r) {
echo '<li>' .$r['item'].'</li>';
}
}
Use empty to check if $json contains something or not.
Assuming that $json['available'] is going to be an empty array at worst:
if (!$json['available']) {
echo "nothing to show!";
}
else {
// your current code
}
If it's possible that $json['available'] might not even exist, a more "heavy-handed" alternative is
if (empty($json['available'])) {
echo "nothing to show!";
}
I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1
I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
$dellink="Delete";
$editlink="Edit";
$admin = "MJH";
if(($instby == $username)||($instby == $admin))
{
echo "<div id=\"editcont\">$editlink $dellink</div>";
}
else if($inst == $username)
{
echo "<div id=\"editcont\">$editlink <font face=\"Trebuchet MS, Arial, Helvetica, sans-serif\" size=\"2\">Delete</font></div>"
}
else
{
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
.in my code above what i want to do is to show a containing links depending on who is logged in.. however, when the admin logs in he will be able to see the containing $editlink and $dellink.
.I can't seem to find where exactly have i gone wrong.. please help me with this guys! TIA! More Power!
.this is what my code looks like.
if(($instby == $username)||($username == $admin))
$admin = "MJH";
if(($instby == $username)||($instby == $admin)) {
if($inst == $username) {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcont\">$editlink <font>Delete</font></div>";
}
else {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
}
Thats how i percieved you wanted it, but really, it is very hard to understand whats going on here. Are $inst and $instby meant to be different variables? Also, out of curiousity, why have you double bracketed your if(); statements?
Hope I deciphered it correctly.
PS. I don't think echo();ing html is good practice.