Trying to use [] to get value of parameter - php

I'm trying to get the value of some URL parameters in PHP and can't seem to get [] to detect this in the URL.
This works:
<?php if ($params['sms_banner'] === 'true') { ?><div id="sms-banner-legacy"> SMS Banner </div><?php } ?>
This doesn't work:
<?php if ($params['payday[sms_banner]'] === 'true') { ?><div id="sms-banner-legacy"> Legacy SMS Banner </div><?php } ?>
When I go to http://someurl.com/page?payday[sms_banner]=true it should then show the <div> on the page, but instead it can't pick up the value.

As you are passing array in url, you can get the value of same using
$_GET['payday']['sms_banner']

php translates that syntax to an array key value, try $_GET['payday']['sms_banner'] or params['payday']['sms_banner'] in your case.

Related

In my case, i want to make a multiple menu using if statement. How can i make the other if statement work for another menu?

I've made one menu work here, the other is doesn't work. I want to make both menu accessible either the "persegi" or the "persegi panjang"
<div id="content">
<div id="kirikolom">
<?php
if(isset($_GET['menu']))
{
if($_GET['menu']="persegi")
{
input_persegi();
} elseif($_GET['menu']="persegi_panjang")
{
input_persegi_panjang();
}
}
?>
</div>
You need to use "==" to equal variable, if just use "=" you give a variable value, so your code must be :
<?php
if($_GET['menu']=="persegi"){
input_persegi();
}
else if($_GET['menu']=="persegi_panjang"){
input_persegi_panjang();
}
?>
try to print and check the result of print_r($_GET['menu']);
Maybe different output when you access another url so it's not match in you if else statment.

Replace Content on Page Based on URL Parameter with PHP

I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php

PHP/Wordpress: IF/ELSE executing both conditions

Have been reading through multiple similar questions and went over my syntax many times, but I can't figure out why my PHP code is executing both conditions.
I'm trying to replace the url of an element with the string from a custom field if the field is not empty. If the field is empty, I want to output the permalink normally. What happens is that the code concatenates both the string from the custom field and the permalink when the field is not empty. If i remove the string in the field, it works fine.
<div class="profile-content">
<a href="
<?php
if ( the_field('direct_resource_link') != '') {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
Thanks!
Dan.
EDIT after comment from original poster
My initial assessment (left below for reference) was correct. You are using function that will print/echo content instead of returning it. Your if will always evaluate to false, because you are calling function that returns nothing; and PHP thinks that nothing and empty string are the same thing.
You didn't see that when field was empty, because the_field() for empty field printed empty string (or nothing at all), i.e. it didn't modify value printed by the_permalink() in any way/
According to ACF documentation, the_field() is accompanied by get_field() which returns value instead of printing it.
Your code should look like that:
<div class="profile-content">
<a href="
<?php
if ( get_field('direct_resource_link') ) {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
My initial post
What happens is that you run function the_field('direct_resource_link') and compare it's return value to ''; if that value is empty, you run the_permalink($id);.
It's hard to tell what the_field() is and what it is supposed to do, but I guess that it prints value instead of returning it. So if field is empty, it prints nothing, resulting in pure run of the_permalink(). If field is not empty, it prints it content and returns nothing. Since nothing is equal to empty string, PHP proceeds with else branch and invokes the_permalink() that prints additional info.
Solution: modify the_field() to return value instead of printing it, or make additional function that will query for value and return it, and use that function in if statement.
Miroslaw Zalewski already answered your question here, so this is simply to show you the kind of code needed to fix your issue:
function get_the_field($field) {
ob_start();
the_field($field);
return ob_get_clean();
}
This code will start an output buffer (which will capture all echo'd data), run the_field and return (and delete) the output buffer (the echo'd data from the_field). This means you can simply do the following:
...
<?php
$theField = get_the_field('direct_resource_link');
if ( $theField != '') {
echo $theField;
} else {
the_permalink($id);
}
?>
...
This can all be simplified. the_field() echoes a meta value. You don't want that...Instead, you want to return the value to check it, before conditionally echoing it. You can do this using get_field().
In the simplest form, your final code would look like the following:
<a href="<?php get_field('direct_resource_link') ? the_field('direct_resource_link') : the_permalink($id); ?>">
<h5><?php the_title(); ?></h5>
</a>

PHP Show menus/submenus according to the value of a variable

I have a navigation bar in which I am trying to show menus/buttons, according to the type of user. I get the type of user via a variable called $isManager.
The good news is that it works on every browser, except firefox.
Code looks like this:
<?php
if ($isManager === '2'){
?>
<li>View</li>
<?php
}
?>
Can you suggest an alternative to this, or is Firefox somehow ignoring or not accepting the true condition here ?
When you use ===, it is for strict checking. So make sure that your$isManager is string type. If it is integer then try
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>
You are Using === it means you want to check by its typeof too.
and after that you wrote '2', so it will missmatch the results and not going to the condition, instead try the following.
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>

php if inside javascript

I am trying to use the bootstrap typehead plugin.
The idea is:
I have a Manufacturer Option
I Have a Model Option
Only once the Manufacturer option is selected, the model option is enabled.
The model option is filtered by manufacturers.
CakePHP Controller is sending me a 'findall' array of both Manufacturer and Models (Which I call Variety instead of Model for naming convention)
model_source = [];
$('#manufacturer').typeahead({
source: [
<?php
foreach($manufacturers as $manufacturer):
if($manufacturer['Manufacturer']['bow'] == true):
?>
"<?php echo $manufacturer['Manufacturer']['manufacturer']; ?>",
<?php
endif;
endforeach;
?>
]
});
$('#manufacturer').live("change", function(){
if($(this).val()){
$('#model').attr('disabled', false);
manufacturer = $(this).val();
<?php
foreach($varieties as $model):
if($model['Variety']['bow'] == true):
?>
model_source.push("<?php if($model['Variety']['manufacturer_id'] == "+manufacturer+"){echo $model['Variety']['model'];}else{ continue; } ?>");
<?php
endif;
endforeach;
?>
}else{
$('#model').attr('disabled', true);
}
});
The problem I am having is where I try to do
model_source.push("<?php if($model['Variety']['manufacturer_id'] == "+manufacturer+"){echo $model['Variety']['model'];}else{ continue; } ?>");
If I hardcode the manufacturer variable to be all in one line of PHP then it works (without the else statement, that is also giving me problems I get an ILEGAL error on my console)
Any idea in why when I concatenate the php string with the javascript variable it then doesn't work?!
http://jsfiddle.net/mmoscosa/Y6hEA/
Thank you!
Your javascript variable happens to be between <?php ?> tags. You can't use javascript manufacturer variable in your if statement. PHP is server side, JavaScript is client side.

Categories