When a user clicks on "List View" link then I want to show them the "List View" HTML and when they click on "Grid View" I want to show "Grid View" HTML.
I've defined the following links to click-
List View <br>
Grid View
Then I defined the following condition with PHP get method to show user the desired output-
<?php
if( isset( $_GET['view'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view'] ) == 'grid' ){
echo "This is Grid view";
}
This condition is not working. If I change "view" to "view_1" and "view_2" from URL then my condition is working as well.
<?php
if( isset( $_GET['view_1'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view_2'] ) == 'grid' ){
echo "This is Grid view";
}
?>
<br>
List View <br>
Grid View
But I don't want to change the "view" key. I just want to keep the same key and different value for both URL to do the conditional statement.
Is it possible?
isset() only checks if a variable exists and isn't null and returns a boolean (true/false).
To check the value, you first need to check if it exists (isset) and then check the value, like this::
if (isset($_GET['view']) && $_GET['view'] == 'list') {
// your code here.
}
You can read more here: http://php.net/manual/en/function.isset.php
Alternative
If you want to make your code and conditions slightly more readable, you could to this:
// Get the value of $_GET['view'] once, if it exists (pre PHP 7)
$view = isset($_GET['view']) ? $_GET['view'] : null;
// PHP 7.x (new shorter syntax for the above)
$view = $_GET['view'] ?? null;
if ($view == 'list') {
// your code
}
Did you try like this..
if(isset($_GET['view']))
{
if($_GET['view'] == 'list')
{
//code here
}
elseif ($_GET['view'] == 'grid')
{
//code here
}
}
Related
I need to run a function (show a modal) if a page DOES have a particular field value AND is not a certain page. It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page. They work individually like this:
//if page has field value:
<?php
$values = get_field( 'recast_video' );
if ( ($values) ) {
get_template_part('template-parts/popIn', 'none');
}
?>
//if it is this page, do not show:
<?php
if ( (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
How do I combine the two so that it shows (get_template_part) if the field has the value and is not the specified page
//this fails
<?php
$values = get_field( 'recast_video' );
if ( ($values) || (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
First thing to do is look for the common condition. No matter what, you don't want it to be a certain page, right? So first check that it's not that page:
$result = (!is_page('trading-education-webinars')) ? : ;
It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it
is not the certain page.
Then why check for the field value at all? No need, as long as it's not the page we checked for.
$result = (!is_page('trading-education-webinars')) ? get_template_part('template-parts/popIn', 'none') : return false;
You can change return false; to whatever you want to happen if the page IS 'trading-education-webinars'
EDIT: Clarifying the conditional:
Page X never gets served "content" (modal)
If NOT page X and has $values, show content
If NOT page X and NOT has $values, show some other content
$values = get_field( 'recast_video' );
$x = get_template_part('template-parts/popIn', 'none');
$y = 'some other content';
$return = (!is_page('trading-education-webinars')) ? (($values) ? $x; : $y ) : return false );
Use the && (AND) operator:
<?php
$values = get_field( 'recast_video' );
if ( ($values && !is_page('trading-education-webinars')) || (!$values && is_page('trading-education-webinars')) ){
get_template_part('template-parts/popIn', 'none');
}
?>
|| operator is true when at least one or both statements are true
I would like to display Yes/No Attributes in my view.phtml. I tried some codes but none is working.
Firstly I tried this syntax, which I found only and which is working for Attributes with Text-Values:
<?php
if ($_product->getAn_342() != null && $_product->getAn_342() != "") {
echo $this->__('Deliveryinformation');
echo $_product->getAn_342();
}
?>
This does not work for Attributes with yes/no Values. With this syntax there is displayed the Magento-ID of the Attribut but not the Value.
Next I tried this Syntax, which I found here in this forum:
<?php
if ($_product->getAttributeText($_data['An_56']) == "Yes"):
?>
But this does not work as well.
For explanation:
One Attribut i want to display would be "Bestseller". The Attribut has Label "an_bestsller" and code "an_56", which will be the magento-Id.
So what exactly is wrong with that yes/no syntax above? Some help would be great.
You can try to do as follows :
$attr = $product->getResource()->getAttribute("oversize_type");
if ($attr->usesSource()) {
$oversizeLabel = $attr->getSource()->getOptionText($oversizeId);
}
Please change oversize_type to your attribute code
Above is the way of getting the option text of dropdown type.
hope this helps.
You've to check different translatable values: 'N/A' or 'NO':
if ($_product->getAttributeText($_data['An_56']) =! $this->__("N/A") && $_product->getAttributeText($_data['An_56'] != $this->__('NO')):
It is better to make an extension which extends Mage_Catalog_Block_Product_View_Attributes and overwrites the function getAdditionalData:
public function getAdditionalData(array $excludeAttr = array())
{
$data = parent::getAdditionalData($excludeAttr);
foreach ($data as $code => $item) {
if ($item['value'] == Mage::helper('catalog')->__('N/A')
|| $item['value'] == Mage::helper('catalog')->__('No')
//You can add more values to hide
//|| $item['value'] == Mage::helper('catalog')->__('--')
) {
unset($data[$code]);
}
}
return $data;
}
I am having a problem with getting my PHP script to correctly read and execute my "IF.....ELSEIF" conditions.
In my first file, I have the following code :
if(isset($_POST['submit']) {
$selected_radio = $_POST['selection'];
$_SESSION['my_selection'] = $_POST['selection'];
if (($selected_radio == '25') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
elseif (($selected_radio == '50') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
}
That was the easy part.
If either "radio button" is selected, I have a Javascript function which opens a "new (child) window"
That's also easy.
But, then, comes the hard part : within that new window, the user has to select from another choice of radio buttons :
if(isset($_POST['submit']) {
$selected_radio = $_POST['my_response'];
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
}
Basically, this means : if the user selects "yes" in the current (child) window, then the window closes, and the parent window re-directs to "25.php" or "50.php"..........depending on the value of the $_SESSION['my_selection'] --- which was selected earlier in the parent-window
But, for some reason, it's not working. My code is executing only the FIRST IF-condition :
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
It is completely ignoring the second one.............even if the user had earlier selected "50" in the parent-window.
My first thought was : the SESSION value of the radio-button ---- $_SESSION['my_selection'] --- was not being carried-over into the new (child) window.
But, I used "echo" to verify that this was working properly. The value was indeed being carried-over into the new (child) window.
However, after the child-window closes, and the parent-window is re-directed, I used "echo" again to track any errors........and it showed that : the value of $_SESSION['my_selection'] is always equal to "25" !
In a nutshell : why is the second IF-statement being ignored??
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25')
^ ^
A single equals sign is an assignment and as the value you are assigning is truthy the if will always evaluate to true. Use == for a comparison.
You are using = instead of == in nearly all statements:
if (($_POST['my_response'] = 'yes')
should be
if (($_POST['my_response'] == 'yes')
This way, you don't check if $_POST['my_response'] is equal to "yes", but if it is possible to assign "yes" to $_POST['my_response']. As a result, all your if statements are true.
i want to create if-else statement in codeigniter with my logic
If $language === "english"
it will no change
$q["question_title"] = $q["question_title"]
else
it will change
$q["question_title"] = $q["question_title_**id**"]
the point is, i want to add "_id" in every php if $languege = no "english"
how to do that if php code inside of "[]" like $q["question_title"]
OR
from query.php
// translation language
$site_lang = $this->session->userdata('site_lang');
if ( $site_lang === 'english') {
$question['questions'][$i]['question_title'] = $q->quiz_text;
} else {
$question['questions'][$i]['question_title'] = $q->quiz_text_id;
}
so if i use code in /views and it read cookies in "english" it will get database "quiz_text" but if else it will get "quiz_text_id" from database
but it not work in my query code.
First echo $site_lang what you getting then keep condition simple if title has already you don't need to set same value again so do a check like
$site_lang = $this->session->userdata('site_lang');
if ( $site_lang != 'english') {
$question['questions'][$i]['question_title'] = $q->quiz_text_id;
}
I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}