How to add 2 objects to php code - php

I have 6 product pages with id from 1 to 6. In the shopping cart page I have this code:
<?php if($objcartRS["catid"]==1 ){ echo "xxx"; ?> <?php } else { echo "yyy"; }?>
My problem is how can I add another page for ex the one with id 2 so it will be just 1 and 2 that will take the name xxx and the rest of the pages yyy?
I hope someone can help me.

As you said based on that you have to do very simple change in you condition that is add another condition in if with ||operator like below:-
<?php if($objcartRS["catid"]==1 || $objcartRS["catid"] == 2){ echo "xxx"; ?> <?php } else { echo "yyy"; }?>
Note:- Please note that $objcartRS["catid"] value must be integer type otherwise your condition will false. In that case you need to typecast your value to integer.

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 if/then statement

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.

Check if empty before foreach

I'm modifying some code but I'm not an expert on php level and need some help :)
<div class="emd-main emd-main-<?php echo $emd_state; ?>">
<?php if ( $userpro->memberlist_in_search_mode($args) ) { ?>
<?php $arr = $userpro_emd->users( $args );
if (isset($arr['users']) && !empty($arr['users']) ) {
?>
<?php if (isset($arr['paginate']) && $args['emd_paginate'] && $args['emd_paginate_top'] == 1) { ?>
<div class="userpro-paginate top"><?php echo $arr['paginate']; ?></div>
<?php } ?>
<div class="emd-list" data-layoutmode="<?php echo $args['emd_layout']; ?>">
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
<?php $tk_image_1 = get_field('foto_1', 'user_'. $user_id); ?>
<?php if (!empty($tk_image_1)) { ?>
<div class="emd-user">
I have the following issue; I have a page that shows a grid with members. The number of members per page is 20 (that is set within the plugin settings). The foreach is starting with this:
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
After that I check if the value $tk_image_1 is not empty, if not empty then go on. So far so good.
The only thing now is; when I have 20 members on a page and for 8 of them the $tk_image_1 is empty then it shows 12 members on that page... I think it's something in the array that counts them before checking the $tk_image_1 value.
What is need is to show 20 members per page and only the one if $tk_image_1 is not empty.
Can someone help me with this?
Many thanks!
Regards,
Robert
The source of your problem is that you're working with a set of results that contains users you don't want to show. To properly correct this, you need to alter the query that's taking place in this code fragment: $userpro_emd->users( $args )
That ->users() method may accept additional $args that would allow you to say, "only users who have foto_1". That way you're not trying to perform magic in the loop, and then be left with the problem of not having enough others to make up for those who were missing foto_1.

if statement syntax equal to

I am using Views php in Drupal 6. I need to do an if statement to find if nodehierarchy_parent is equal to the value 0. Then do something. Right now I have it set to if the value contains a 0. My code is below.
<?php
if (strpos($data->nodehierarchy_parent,'0')) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
I am new to stack. John's comment solved my problem but I cant mark his comment as correct answer. Below is the final code that worked.
<?php
if ($data->nodehierarchy_parent == 0) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
echo (strpos($data->nodehierarchy_parent,'0') === false)
? $data->nodehierarchy_parent
: 'hello';

how to unset a specific key value from a session array in php

I have a booking cart for booking for products and at some point, user would be able to delete specific products from their cart.
I need help deleting product from the session array cos I keep getting an error message "Fatal error: Cannot unset string offsets" while trying to delete a product.
Here is the code for deleting
<?php
if(isset($_GET['inh_arr_key']) && isset($_GET['c_id'])) {
$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];
unset($_SESSION['inh_cart'][$inh_arr_key]);
$inh_cart = $_SESSION['inh_cart'];
// Parse the cart session variable
$inh_arr = explode(',',$inh_cart);
if (array_key_exists($inh_arr_key, $inh_arr)) {
echo '<p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>';
}
else{
$del_inh_query_course_info = #mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
$del_inh_course_det = #mysql_fetch_assoc($del_inh_query_course_info);
$del_inh_course_title = $del_inh_course_det['title'];
echo '<p class="ok_msg"><strong>'.$ex_inh_course_title.'</strong> has been deleted from your In-house course booking cart.</p>';
}
}
?>
I've fetched and appended each products array key and value to them. So that's being retrieved by $_GET and they are in these variables
$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];
Just to provide more detail about the question, here is the code for adding products to the cart and it's working fine
<?php
$c_id = $_GET['c_id'];
session_name("inh_cart");
session_start();
$inh_cart = $_SESSION['inh_cart'];
if ($inh_cart) {
$get_inh_arr = explode(',',$inh_cart);
if(in_array($c_id, $get_inh_arr)){
$inh_cart = $inh_cart;
?>
<script language="javascript">
window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
</script>
<?php
}
else {
$inh_cart .= ','.$c_id;
}
} else {
$inh_cart = $c_id;
}
$_SESSION['inh_cart'] = $inh_cart;
?>
<script language="javascript">
window.location = "user_allc_booking.php";
</script>
<?php
$inh_query_course_info = #mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = #mysql_fetch_assoc($inh_query_course_info);
$inh_course_title = $inh_course_det['title'];
?>
And if for example the cart contain 3 product, if I do a var_dump($_SESSION['inh_cart']);
The output will be:
string(8) "20,48,24"
Really need to what's wrong with the codes for deleting products. Need help with this. Thanks!
It would seem that $_SESSION['inh_cart'] is a string at this point for some reason so its trying to unset the char at index $inh_arr_key of the string which isnt allowed.
It looks like you have it as a comma separated list... so in order to do your unset you need to explode it BEFORE calling unset.
But thats a bad way to do it.. you leave alot of room for error in terms of getting the indexes mixed up. You should just make it an array and let php serialize/unserialize it as part of the normal session behavior. Additionally instead of using generic ordered numeric indexes for the keys, use something unique to each product like the SKU or the primary key from the DB record.
So putting that together...
Adding stuff to the cart:
$c_id = $_GET['c_id'];
session_name("inh_cart");
session_start();
if(!isset($_SESSION['inh_cart']) {
// if we dont have a cart - initialize it as an array
$_SESSION['inh_cart'] = array();
}
$inh_cart &= $_SESSION['inh_cart'];
if(in_array($c_id, $inh_cart)): ?>
<script language="javascript">
window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
</script>
<?php else:
// just append the item to the array
$inh_cart[] = .$c_id;
endif; ?>
<script language="javascript">
window.location = "user_allc_booking.php";
</script>
<?php
// not sure what youre trying to do here but ok...
$inh_query_course_info = #mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = #mysql_fetch_assoc($inh_query_course_info);
$inh_course_title = $inh_course_det['title'];
?>
And then deleting from the cart:
<?php
// all processing at the top - easier to read -
// use the $error variable to tell what message to display
$error = false;
if(!isset($_GET['c_id'])) {
$error = true;
} else {
$del_c_id = $_GET['c_id'];
$del_key = array_search($del_c_id, $_SESSION['inh_cart']);
if($del_key) {
unset($_SESSION['inh_cart'][$delkey]);
// get the course info
$del_inh_query_course_info = #mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
$del_inh_course_det = #mysql_fetch_assoc($del_inh_query_course_info);
$del_inh_course_title = $del_inh_course_det['title'];
} else {
$error = true;
}
}
?>
<?php if($error): ?>
<p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>
<?php else: ?>
<p class="ok_msg"><strong> <?php echo $ex_inh_course_title ?></strong> has been deleted from your In-house course booking cart.</p>
<?php endif; ?>
My guess is that your error is here:
unset($_SESSION['inh_cart'][$inh_arr_key]);
It seems that you have a comma-separated value as $_SESSION['inh_cart']. It is a string, not an array. Thus your use of [] syntax on a string is in essence doing something like:
unset `$_SESSION['inh_cart']` at position starting at [some string value] ($inh_arr_key)
Of course that string does not have an offset of [some string value] as its offsets are strictly numerical.
You need to use '$_SESSION['inh_cart']` as an array.
Also, you probably don't want to set/unset items in a cart via $_GET. This is a really bad idea, as as someone navigates your site using forward/back buttons they will (randomly in their mind) see their cart changed.
You can try to make sure key exists.
if (array_key_exists($inh_arr_key, $_SESSION['inh_cart'])) {
unset($_SESSION['inh_cart'][$inh_arr_key]);
}
As per #prodigitalson Make sure $_SESSION['inh_cart'] is array not a string.

Categories