PHP if/then statement - php

Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.

Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.

Related

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 syntax - shows text and not link

With this snippet of code, I'm attempting to show a clickable link (if "admin" is logged in), which will redirect me to adminarea.php
Right now it just prints out "Admin" in text. Nothing to click on. Just simple text.
Am I missing anything? Surely I got it wrong but I cannot see what's missing.
Here is the code:
<?php if (getUser("user") == "admin") { ?>
<option value="adminarea.php">Admin</option>
<?php } ?>
You're printing an option, which is part of the select form input. You're probably looking for an anchor?
Admin
Possibly a better way to do this would be to declare two options for a variable in your PHP first. Something like:
<?php
if(getUser("user") == "admin") {
$adminlink = 'Admin';
} else {
$adminlink = NULL;
}
?>
And in the html:
<?php echo $adminlink; ?>
This would show the href link if the PHP condition was true, and would display nothing if not. Hope this helps!
Well based on your title am assuming you want a link. By the way you can use PHP friend html syntax instead of making the code look "dirty".
<?php if(getUser("user") == "admin"): ?>
Admin
<?php endif; ?>

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

i need help on if else statement on php code using wordpress

Basically I have this code. It shows on header_new when the postId of the wordpress page is part of the array.
Now I want a 3rd option, a new header with id . So it shows up when the its part of a new set of array. I tried else if it didn't work tho.
Can you guys help me add a new php line to do just that. Basically a 3rd else if statement to execute a new header given if it is part of an array.
Please be detailed since I'm a noob at PHP
<div id="wrapper" class="<?php echo $post->ID;?>">
<?php $pagesArray = array(7686,7913,7915,8019,8131,8180,8676,8769,8796,8810,8812,8814,8818,8815); ?>`
<?php if(in_array($post->ID, $pagesArray)){ ?>
<div id="header_new"><!--header!-->
</div><!--header!-->
<?php } else {?>
<div id="header-out">
</div>
You cannot order your code like this:
if (something){
xxxxx
} else {
xyxyx
} else if (something else) {
zzzzz
}
because your loop ends with else. else if must come before else case, because else
finishes the loop. The third else if will never be fulfilled.
Edit:
When you put your second else if, you need to specify your test.
it is else if(test clause){some execution}.
Here is a link, this may help you out: Php else if

How Can I Move This <td> Tag To The Next Line?

I have a problem:
Here's a block of the code:
function draw()
{
$out_string="";
$out_string.=$this->script;
reset($this->fields);
$num_list_box=0;
while( $field = each($this->fields) )
{
if (isset($this->fields[$field[1]->field]->options))
{
if (preg_match("/<script type=\"text\/javascript\">/i",$this->fields[$field[1]->field]->options[0][1])&& $this->fields[$field[1]->field]->value!="")
{
if ($num_list_box==0) $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
else $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").forValue(\"".$field_prev[1]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
$field_prev[]=$this->fields[$field[1]->field]->value;
$num_list_box++;
} else
{
$field_prev[0]=$this->fields[$field[1]->field]->value;
$num_list_box=0;
}
}
}
$out_string.=$this->draw_title();
$out_string.=$this->draw_header();
$out_string.= "<table class=\"forms\">\n";
$field=array_keys($this->fields);
reset($field);
$ind_first=true;
while( list($pos,$field_name) = each($field) )
{
if ($this->num_cols>0) {
if ($this->fields[$field_name]->col==1){
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_title\">";}
else $out_string.="<td class=\"field_title\">";
$out_string.= $this->fields[$field_name]->title."</td>";
$colspan="";
if ($this->num_cols>1) {
if ($this->fields[$field_name]->col==1 && array_key_exists($pos+1,$field) && $this->fields[$field[$pos+1]]->col==1)
$colspan="colspan=\"3\"";
}
$out_string.="<td class=\"field_value\" $colspan>";
$out_string.=$this->fields[$field_name]->draw()."</td>";
} else
{
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_value\">".$this->fields[$field_name]->title."<br />";
$out_string.=$this->fields[$field_name]->draw()."</td>";
}
}
$out_string.= "</tr></table>\n";
return $out_string;
}
This above block of code produces something like this:
I want it such that in the example provided that the word "Transaction" is above the text box.
Please help, the person that programmed this part is indisposed and we've got a deadline.
Thanks for the help.
P.S. The CSS class name for the text is: field_title and the one for the textbox is field_value
Thanks once more.
You will have to debug that code to find out when the label "Transaction" is getting inserted into that cludge of table code. Once you find where "Transaction" is inserted you can then create new logic to add another TR that colspans the table and place the label on that new row.
Good luck, looks like a headache.
Your code is apparently building a rather complex form based on some data ($fields property of the class). I guess some "field settings" (like col) describe how the form should look like.
So, your code is doing things that are neither described in your question nor in the code itself. Furthermore, we have no clue what the complete form currently looks like, so we can't even guess the intention of the code.
Your request, to let the description appear above the selection box(?) could be done probably throwing away half of the code, but that won't help you.
PS: Please check the FAQ - this site is for questions, not for finding people to do your (or elses) work. You should really have a programmer solve your problem directly having access to the whole page.

Categories