How would I place the following:
<?php if (function_exists('premium_slider')){ premium_slider(1); }; ?>
Within the echo of this:
<?php if(is_page(2)){ echo ''; } ?>
Obviously I can't do this:
<?php if(is_page(2)){ echo '<?php if (function_exists('premium_slider')){ premium_slider(1); }; ?>'; } ?>
<?php
if (is_page(2) && function_exists('premium_slider')){ echo premium_slider(1); };
?>
With else:
if (is_page(2) && function_exists('premium_slider')) {
echo premium_slider(1);
} else {
echo "SO Rocks!";
}
Alternatively:
echo is_page(2) && function_exists('premium_slider') ? premium_slider(1) : 'SO Rocks!';
<?php
if (is_page(2) && function_exists('premium_slider')) {
echo premium_slider(1);
}
?>
You can use this code exactly:
<?php
if (is_page(2) && function_exists ( 'premium_slider' )) {
echo premium_slider ( 1 );
} else premium_slider ( 2 );
?>
You are using closing } at wrong place.
Related
I have some tabs. In that sometimes first tab is "introduction" and sometimes "outline" is first tab. I have set introduction class=active when it comes first. But when outline comes first I am not getting how to set its class as active.
code is like below
<ul class="nav-tabs" role="tablist">
<?php
$tabs = array();
if(!$sessId == "" && !$checkcourse){
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>true, "comment"=>true);
} else if($sessId == "") {
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>false, "comment"=>true);
} else {
$tabs = array("introduction"=>false, "outline"=>true,"announcement"=>true,"discussion"=>true,"review"=>true, "student"=>true, "comment"=>false);
}
?>
<?php if($tabs['introduction']) { ?>
<li class="active">Introduction</li>
<?php } ?>
<?php if($tabs['outline']) { ?>
<li>Outline</li>
<?php } ?>
<?php if($tabs['review']) { ?>
<li>Review</li>
<?php } ?>
<?php if($tabs['student']) { ?>
<li>Student</li>
<?php } ?>
<?php if($tabs['comment']) { ?>
<li>Comment</li>
<?php } ?>
<?php if($tabs['announcement']) { ?>
<li>Announcement</li>
<?php } ?>
<?php if($tabs['discussion']) { ?>
<li>Discussion</li>
<?php } ?>
</ul>
Simple check with ternary operator is:
<?php if($tabs['outline']) {?>
<li <?=$tabs['introduction']? '' : ' class="active"';?>>Outline</li>
<?php } ?>
So you check if $tabs['introduction'] isset then you don't need class="active", else - add this class.
Update:
But as first item of tab can change, I advise to create simple foreach:
$is_first = true;
foreach ($tabs as $tab_name => $value) {
if ($value) {
echo '<li' . ($is_first? ' class="active"' : '') . '>' . $tab_name . '</li>';
$is_first = false; // fix that we have first value here
}
}
Here your main problem is how to link href value and caption.
I have this code where if something then display a link, else display another link. the if and else are php code, and the links are html as you will know, but I was wondering how do you make it so it will not give me any errors, how do I combine php with html?
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
Leave Society;
} else {
Join Society;
}
This might help you. This one is from the basics of PHP:
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "<a href='file.php' class='socbuttons'>Leave Society</a>";
} else {
echo "<a href='anotherfile.php' class='socbuttons'>Join Society</a>";
}
}
?>
Remember PHP should be surrounded by <?php ?>. Simply end the "PHP part" and then start it again when you finished your HTML.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
?> Leave Society <?
} else {
?> Join Society<?
}
Also, you can write HTML in echo statement, but you should escape some special characters like " to not interfere with the php code itself.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "Leave Society";
} else {
echo "Join Society";
}
See http://php.net/manual/en/function.echo.php to more information.
Try this
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) { ?>
Leave Society;
<?php } else { ?>
Join Society;
<?php }
Use alternate syntax:
<?php
$aUserSocs = array( 'link1', 'link2', 'link3' );
$soca = 'link2';
$iCountUserSocs = count( $aUserSocs );
for( $i = 0; $i < $iCountUserSocs; ++$i ):
?>
<?php if( $soca == $aUserSocs[ $i ] ): ?>
Leave Society;
<?php else: ?>
Join Society;
<?php endif; ?>
<?php endfor; ?>
So basically what am trying to achieve is the following.
I am trying to make it so the following script does something in this instance:
If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."
That is how I would explain what Im trying to do.
This is my current code:
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
?>
I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".
<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>
switch case is the most elegant way to go here:
switch($array['something']) {
case 0: echo 'No';break;
case 1: echo 'Yes';break;
default: echo 'Error.';
}
<?php
if(isset($_POST['resolve'])) {
$api = "http://test.com/php/";
if(!$_POST['name']) {
echo "Please, fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
echo "<div align='center'>";
if($array['something'] == '0') {
echo 'No';
}
elseif($array['something'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
echo "</div>";
}
}
?>
Don't forget to also do a security input check on $_POST['name']
Voila
echo $array['something1'] ? "Yes" : "No";
This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
$array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];
That is the simplest way I know of doing it.
Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...
<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>
<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.
$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)
I want the following javascript code to be echoed in a PHP class file. How I can achieve that?
echo '<script language="JavaScript" type="text/javascript">'."\n";
echo 'var parDoc = window.parent.document;';
if ($result == 'OK') {
echo 'parDoc.getElementById("picture_error").innerHTML = "";';
}
else {
echo "parDoc.getElementById('picture_error').innerHTML = '".$result_msg."';";
}
if($filename != '') {
echo "parDoc.getElementById('picture_preview').innerHTML = '<img src=\'$preview_url$filename\' id=\'preview_picture_tag\' name=\'preview_picture_tag\' width=\'60\' />';";
}
echo "\n".'</script>';
exit(); // do not go futher
<?php
?>
<script language="JavaScript" type="text/javascript">
var parDoc = window.parent.document;
<?php
if ($result == 'OK') {
?>
parDoc.getElementById("picture_error").innerHTML = "";
<?php
}
else {
?>
parDoc.getElementById("picture_error").innerHTML = "<?php echo $result_msg;?>";
<?php
}
if($filename != '') {
?>
parDoc.getElementById("picture_preview").innerHTML = '<img
src=\'<?php echo $preview_url.$filename;?>\'
id=\'preview_picture_tag\'
name=\'preview_picture_tag\'
width=\'60\' />';
<?php
}
?>
</script>
exit(); // do not go futher
Surround it with the PHP code sequence relevant to your installation: <? ?> or <?php ?> depending on your configuration.