simple if/else in PHP - php

I display the form validation error in codeigniter as below:
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
i want to do it so that if there is error, then it should print error, otherwise it should print the info div.
For example,
if form_error, then
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
else
<div class="info">Your first and last name. </div>
As form_error is not just a simple variable that i can check if it is empty then print info. How can i do it? Thanks.

You can do something like this:
if ( form_error('name') )
{
echo form_error('name');
}
For form_error might not be a variable but it's a function that returns a string. If the string is empty (NULL, FALSE, "", 0, ...), the if statement will fail (meaning there is no error) and the form_error('name') won't be called.

This sould do it :
if (form_error('name')){
echo form_error('name', '<div class="form_error">', '</div>');
} else {
echo '<div class="info">Your first and last name. </div>';
}

Related

How to include <?php echo esc_html($product->get_review_count()); ?> in function.php?

I'd like to add :<?php echo esc_html($product->get_review_count()); ?>
in my function which is :
function commentaires() {
print '<p class="commentaires" Id="liste-des_commentaires">Commentaires</p>';
}
How can I write that properly ?
To explain, I want to display next to my word "Commentaires", the number of comments.
Thanks for your help !
If number of comments in $product->get_review_count(), than:
function commentaires($count) {
print '<p class="commentaires" Id="liste-des_commentaires">Commentaires '.$count.'</p>';
}
commentaires($product->get_review_count());

How to add variable in error message (php) multilanguage?

Need error message in two languages, and have some trouble with single quotes...
This is how it looks like:
else if(trim($FullName) == '')
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
And how when I put variable
else if(trim($FullName) == '')
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>';
}
So, when I put it like this, syntax becomes incorrect because single quotes..
did u try
else if(trim($FullName) == '')
$error = '<div class="errormsg">'. $lang["error"] .'</div>';
}
EDIT : echo is a php instruction that does not require you to re-open and re-close php tags (<?php and ?>). You can compute strings thanks to concatenation which is some basic stuff in PHP
I would use empty, i.e.:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
the following, doesn't make sense since you're inside a php block:
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>
Change for:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">'.$lang['error'].'</div>';
}

Wordpress IF statement not working

I have an IF statement stating the following...
<?php
if (empty($data['footer_text'])) {
echo'<p>© ';
print(Date("Y"));
echo'<span class="sep"> | </span><a href="';
echo get_settings('home');
echo'" title="';
bloginfo( 'name' );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></p>';
}
else{
echo'<p>';
global $data;
echo $data['footer_text'];
echo'</p>';
}
?>
The problem I'm running into is that when I call it like this.
<p><?php global $data; echo $data['footer_text']; ?>;</p>
It displays my text correctly. But when I use the IF statement it always defaults to the showing the site name even when I know it's displaying the text correctly.
Is my syntax screwed up? I can't figure out why it thinks nothing is there but still shows up when I display in a p tag.
You don't declare $data to be global until you're INSIDE the if(), meaning that $data is undefined at the point you're doing the
if (empty($data[...])) {
you probably want
global $data;
if (empty($data[...])) {
instead.
Try to debug the variable $data['footer_text']:
<?php var_dump($data['footer_text']); ?>
just before IF statement. Remember, when empty() returns true.

how to check if foreach loop empty when using json and php

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

Default Option and returning NULL

Trying to figure out how to better write this chunk of code. I'm wanting to get the list of the roster members and then create an array of options for the view dropdown to display inside the select dropdown and also have it have an option to display "Please Select An Option". However what if what is returned from the getAllRoster function is NULL which is what I have returned if no results are returned from a query. How should I handle that which I just want the empty option displayed.
Also I need to think about is do a function to retrieve all the allies for that specific matter and then display that ally as the default ally in the dropdown for each dropdown.
Controller:
$rosterList = $this->bios->getAllRoster();
$allies = array();
$allies[''] = 'Please Select An Opion';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php echo form_dropdown( 'ally1', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 2', 'ally2'); ?>
<div>
<?php echo form_dropdown( 'ally2', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 3', 'ally3'); ?>
<div>
<?php echo form_dropdown( 'ally3', $allies, ''); ?>
</div>
EDIT :
What I am wanting to do is if the allies array is empty it needs to display the message No wrestlers in database but its instead giving me an error in my view file.
Controller:
pastebin.com/1Bf721zJ
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php if ($allies[''] == 'No Wrestlers In Database') {
echo $allies[''];
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
</div>
I also am curious about something. I have the alliesList variable that either has a value of a resultset or null and what I want to do if its a result set is have each of the allies be the default value in each of the dropdowns.
You could do something like this:
$rosterList = $this->bios->getAllRoster();
$allies = array();
if (empty($rosterList) {
$allies[] = 'nothing to display';
}
else
{
$allies[] = 'Please Select An Option';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
}
also in your view, if you don't want to display a drop down you could put a conditional in to display something else, e.g.:
<?php if ($allies[0] == 'nothing to display') {
echo $allies[0]
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
Im not sure i fully understand your question but if im right cant you just do
if $_GET['allies'] == "Please select an option"{
$something = Null
}
else{
$something = $_GET['allies']
}
and use $something where you would have used $_GET['allies']?

Categories