How to add a else command in php? - php

I have an attribute called "Merchant_Name".
The following code will place the "Merchant_Name" Just below the product name in product page.
Now I need to add an else command to this code like this:
If Merchant_Name is there then it should be "By Merchant_Name" else "XXXXX".
Hope you get me...
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>By <?php echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); } ?>
</div>
Thanks in Advance...

You mean, with an if/else?
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name) {
echo "By " . $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo "By XXXXX";
}
?>
Or maybe this would be cleaner:
<?php
if($_product->getAttributeText('merchant_name')) {
$name = $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
$name = "XXXXX";
}
?>
<div class="Merchant_Name">By <?php echo $name ?>

Once you start the php tag try to finish your task. Don't switch in and out again and again.
<div class="Merchant_Name">
By
<?php
if ($_product->getAttributeText('merchant_name')){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>

It is pretty simple and it would be simpler if you didn't switch in and out of PHP so often:
<div class="Merchant_Name">
By
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>

Related

PHP Echo Empty Check with Photos

i have issue with my php code .
First i have Database with Photo link then i add this with echo .
I Fill the Database with /upload/photo-1.png or what ever from the User !
<?php echo$photo; ?>
I add this with :
<?php if(!empty($photo)) {echo 'uploads/photodefault.png'; } ?>
you can write like this
<?php if(!empty($photo))
{
echo $photo;
}
?>
Edit 1:
If photo is not empty then it will show link.
Change your code to this
<?php
if(empty($photo)) {
echo "uploads/photodefault.png";
} else {
echo $photo;
}
?>
With an <img> tag example
<img src="<?php if(empty($photo)) { echo "uploads/photodefault.png"; } else { echo $photo; } ?>">
try the following code
if ($photo != ''){
echo $photo;
}else{
echo "uploads/photodefault.png";
}
<?php if(!empty($photo)) { echo $photo; } else { echo 'default/default_large.png'; } ?>
This fixed my issue.

changing HTML content in PHP

I am newer to PHP and I have a problem changing the resultDiv contents
if(mysql_num_rows($res)==0)
{
echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
// echo "<script>alert('No Such filght');</script>";
echo "No such flight";
}
else{
echo "<table border='1'><th>flight#</th><th>Dep. City</th><th>Arrival City</th><th>Dep. Date</th><th>Arrival Date</th><th>Total Seats</th>";
while($row=mysql_fetch_array($res))
echo "<tr><td>".$row['id']."</td><td>".$row['depCity']."</td><td>".$row['arrivCity']."</td><td>".$row['depDate'].
"</td><td>".$row['arrivDate']."</td><td>".$row['totalSeats']."</td></tr>";
echo "</table>";
}
</div>
<div id="resultDiv">
Result Div
</div>
Please avoid to use mysql_* function as it is deprecated in latest version. You can use mysqli or PDO.
You can display message like this :-
<?php
$message = (mysql_num_rows($res) == 0) ? 'No Such Flight' : '';
?>
<div id="resultDiv">
<?php echo $message;?>
</div>
You can do it like so:
<div id="resultDiv">
<?php
if (mysql_num_rows($res) == 0) {
echo "No such flight";
} else {
echo "Flight found";
}
?>
</div>
you have misspelled "resultDiv" in get element by id, also define the div before the if condition.
<div id="resultDiv">
Result Div
</div>
if(mysql_num_rows($res)==0)
{
echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
echo "No such flight";
}

Login/logout dynamic

if(isset($_SESSION['username'])) {
echo "Logout";
} else {
echo "Login";
}
?>
i need someting like this thath i adapted to this bur still wont work...
<?php
if(isset($_SESSION['username'])) {
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>;
}
else {
<p>Login</p>;
}
?>
Should i put it in a separed frok html code and include it or? Help me please
I don't know what you want to say and what your actual problem.. but i have checked given url and in my understanding this code may work for you..
<?php if(isset($_SESSION['username'])){ ?>
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>
<?php } else{ ?>
<p>Login</p>
<?php } ?>
Thanks...
I don't quite understand your question, but I can see your code has some problems. Try the below.
<?php if(isset($_SESSION['username'])): ?>
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>
<?php else: ?>
<p>Login</p>
<?php endif ?>

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

if table value does not exist echo text?

Please could someone explain to me how could I adapt my code to make it so that if a record / value doesnt exist in the mysql table it will echo a piece of text? Thank you.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
?>
Use the ternary operator '?:'
Sample:
$you_var ?: 'you_text_if_not_exists'
Check your variables like that :
<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews = mysql_fetch_array($reviews_set))>=1)
{
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
}
} else {
echo 'No reviews available';
}
?>
If you want to check if table has rows, use following:
$num_rows = mysql_num_rows($reviews_set);
$num_rows will contain number of rows.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
<?
}
}else{
echo "No Data";
}
?>
I hope this helps

Categories