PHP Change function to check multiple id's - php

I would like the below php functions to check for multiple product id's !== (1427, 1428, 1429).
I tried adding:
$ids = array(1427, 1428, 1429)
if (in_array($product->get_id(), $ids) { }
But it did not work.
What is the best approach?
Thank you.
//add multistep
add_action('wapf_before_wrapper', 'wapf_before_wrapper');
function wapf_before_wrapper($product) {
if($product->get_id() !==1427)
return;
?>
<div class="wapf-progress">
<div class="wapf-progress-bar"></div>
<div class="wapf-progress-steps"></div>
</div>
<?php
}
add_action('wapf_before_product_totals', 'wapf_before_product_totals');
function wapf_before_product_totals($product){
if($product->get_id() !==1427)
return;
?>
<div class="wapf_step_buttons">
<button class="button wapf_btn_prev" style="display:none">Previous</button>
<button class="button wapf_btn_next">Next</button>
</div>
<?php
}
//end multistep

Related

How to call a function within a class method property?

I am trying to call an external function within a class method property. It actually does gets called but at the end of the page and whatever is inside the method property, remains separate.
Since I am a self taught student, it is recent that I have started learning PHP classes so I am not really sure if this can be done or not.
Please guide me how this can be done correctly or if not, then what could be the workaround?
The class I have written is as follows:
It will take the input from user while creating of instance and render a modal box based on the input and options selected.
class modalBox{
private $modal_id, $modal_anim, $header_title, $modal_content,$footer;
private $headerOpt,$titleOpt,$footerOpt,$closeBtn;
public function setID($id){
$this->modal_id = $id;
}
public function getID(){
$modal_id = $this->modal_id;
return $modal_id;
}
public function setTitle($title){
$this->header_title = $title;
}
public function getTitle(){
$title = $this->header_title;
return $title;
}
public function setBodyContent($content){
$this->modal_content = $content;
}
public function getBodyContent(){
$modalContent = $this->modal_content;
return $modalContent;
}
public function setFooterContent($footer){
$this->footer = $footer;
}
public function getFooterContent(){
$footerContent = $this->footer;
return $footerContent;
}
public function initiateModal($modal_anim, $headerOpt, $titleOpt, $closeX, $footerOpt, $footerCloseBtn){ ?>
<div class="modal <?php if($modal_anim != 'false'){echo $modal_anim;} ?>" id="<?php echo $this->getID(); ?>" style="z-index: 2;">
<div class='modal-dialog'>
<div class='modal-content'>
<?php
// display if header option is set to true
if ($headerOpt){
?>
<div class="modal-header">
<h4><?php echo $this->getTitle(); ?></h4>
<?php
// display if close button (X) is set to true
if($closeX){
?> <button type="button" class="close" data-dismiss="modal">×</button> <?php } ?>
</div>
<?php } ?>
<div class="modal-body"><?php echo $this->getBodyContent(); ?></div>
<?php if($footerOpt){ ?>
<div class="modal-footer"><?php echo $this->getFooterContent(); ?>
<?php if($footerCloseBtn){ ?>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php
}
}
?>
The function I am trying to call within property is as follows;
This function is not inside a class. This is present independently in functions.php which I have included in index file.
function getDocNameList() {
global $db;
$getDoc = $db->prepare("SELECT id,doc_name from doctor");
$getDoc->execute();
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
// print the returned rows in options list of <select>
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
}
The initiation of class instance is as follows, Please note where I am calling the function
// create class instance
$rangeModal = new modalBox;
//set the modal id
$rangeModal->setID ("rangeFields");
//set the modal title in header
$rangeModal->setTitle("Select Date Range");
// set the body content
$rangeModal->setBodyContent("
<form method='post' action='expenditure.php'>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker1' name='exp_date_from' value='From Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker2' name='exp_date_to' value='To Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<select>" . getDocNameList() . "</select>
</div>
");
//set the footer content
$rangeModal->setFooterContent("
<input type='submit' class='btn btn-success' name='submitRange' />
</form>
");
/*
* #args ---
* modal animation
* modal header (boolean)
* modal title (boolean)
* modal close X (boolean)
* modal footer (boolean)
* modal footer close button (boolean)
*/
// initiate modal
$rangeModal->initiateModal('fade',true,true,true,true,true);
I expect the output of the function to be displayed as .... within the block but instead it gets rendered at the bottom of the page just before tag.
You echo it here, so it will be displayed immediately:
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
So it is not concatenated here, the function does not return anything:
<select>" . getDocNameList() . "</select>
Build it and return it instead:
$output = '';
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output .= "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return $output;
Or build an array and join the elements:
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output[] = "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return implode($output);

How to get PHP echo to jQuery popup alert?

I use the below codes for email conformation:
using URL: /my_web/back-end/email-verify.php?email=email#gmail.com&code=Vruxc9JQwZEKjbz3HbZ2KlGPX10mJneDmH67hILqFooXeJIQb9
After the verification how can I get the jQuery to pop up on the index page?
Is there any possible way to get PHP echo value to jQuery?
if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['code']) && !empty($_GET['code'])){
// Verify data
$email = mysqli_escape_string($conn,$_GET['email']); // Set email variable
$hash = mysqli_escape_string($conn,$_GET['code']); // Set hash variable
$sql="SELECT * FROM users WHERE user_email='".$email."' AND hashid='".$hash."' AND valied_user_1='0'";
$search = mysqli_query($conn,$sql);
if(mysqli_num_rows($search) > 0){
// We have a match, activate the account
$update="UPDATE users SET valied_user_1='1' WHERE user_email='".$email."' AND hashid='".$hash."' AND valied_user_1='0'";
mysqli_query($conn,$update);
echo"success";
}else{
///other echo
}
jQuery Function:
$(document).ready(function(){
if(success){
$('#alert').addClass('alert-success alert-dismissible fade show');
$('#alertText').html('Your account has been activated, you can now login.');
$('.modal4').show();
}
});
HTML Modal4:
<div class="modal4">
<div class="container alert-popup">
<div id="alert" class="alert">
<button type="button" id="alertcloss" class="close">×</button>
<p id="alertText"></p>
</div>
</div>
</div>
Redirect the email_validation.php to index.php page with the GET parameters.
http://sample.com/index.php?success=1 and on the index.php page do like
<div class="modal4">
<div class="container alert-popup">
<div id="alert" class="alert">
<button type="button" id="alertcloss" class="close">×</button>
<p id="alertText"></p>
</div>
</div>
</div>
<?php
if(isset($_GET['success']) {
?>
$(document).ready(function(){
$('#alert').addClass('alert-success alert-dismissible fade show');
$('#alertText').html('Your account has been activated, you can now login.');
$('.modal4').show();
});
<?php
}
?>

How to get new session value after some time using PHP

I need to get the new session value after some second using PHP. My code is below.
<button class="btn nextbtnbgdiv open2" type="button">Next <span class="fa fa-arrow-right"></span></button>
<div class="fyndspacecategory fyndsheightsubc nano">
<div class="nano-content">
<?php
$cat_id= $_SESSION['cid'];
?>
</div>
</div>
Here when user is clicking on next button the div section is opening and the the value is fetching from session. Here i need after some second always the updated $_SESSION['cid'] will fetch at each time click on next button.
In order to fetch the updated $_SESSION['cid'] you will need to make a call to the server to return this value, you can do so by embedding the value into an element i.e.
Back-end (i.e. get_session.php):
<html>
<body>
<div id="session"><?= $_SESSION['cid'] ?></div>
</body>
</html>
Front-end:
<button class="btn nextbtnbgdiv open2" type="button">Next <span class="fa fa-arrow-right"></span></button>
<div class="fyndspacecategory fyndsheightsubc nano">
<div class="nano-content"><?php $cat_id= $_SESSION['cid']; ?></div>
</div>
<script>
jQuery(function($) {
$(document).on( 'click', '.nextbtnbgdiv', function() {
$('.fyndspacecategory .nano-content').load('/get_session.php', '#session');
});
});
</script>
Or you could use a header within a page to access $_SESSION['cid'].
<?php
if ( isset( $_GET['get_session'] ) && $_GET['get_session'] ) {
header( 'X-Session: ' . $_SESSION['cid'] );
exit;
}
?>
<button class="btn nextbtnbgdiv open2" onclick="javascript:getSession();" type="button">Next <span class="fa fa-arrow-right"></span></button>
<div class="fyndspacecategory fyndsheightsubc nano">
<div class="nano-content" id="sessionId"><?php $cat_id= $_SESSION['cid']; ?></div>
</div>
<script>
function getSession() {
var request = new XMLHttpRequest();
request.onerror = function() { console.warn('An error occurred while checking session.'); };
request.open('HEAD', '?get_session=true', true);
request.onload = function() {
if ( request.getResponseHeader('X-Session') ) {
document.getElementById("sessionId").innerHTML = request.getResponseHeader('X-Session');
}
};
request.send();
}
</script>
This is not tested but should be enough to give you a start to complete your goal.

Ajax how to disable div or item

I am new to ajax, is there a way you can help me, how to disable all items once click is successful:
Here is my code for ajax:
if (!parent.hasClass('.disabled')) {
// vote up action
if (action == 'click') {
alert("test");
};
//how do i add disabled function on a particular div
// add disabled class with .item
parent.addClass('.disabled');
};
here is my index:
<?php while($row = mysql_fetch_array($query)): ?>
<div class="item" data-postid="<?php echo $row['recipe_id'] ?>" data-score="<?php echo $row['vote'] ?>">
<div class="vote-span"><!-- voting-->
<div class="vote" data-action="up" title="Vote up">
<i class="icon-chevron-up"></i>
</div><!--vote up-->
<div class="vote-score"><?php echo $row['vote'] ?></div>
</div>
<div class="post"><!-- post data -->
<p><?php echo $row['recipe_title'] ?></p>
</div>
</div><!--item-->
i jst want to disable the loop icon-chevron-up class.not just one but all.
Actually no ajax call needed here. I will only be done by using jquery. See the code
$(document).ready(function(){
$('.item').click(function(){
if (!parent.hasClass('.disabled')) {
parent.addClass('.disabled');
}
});
});
Here you have not mentioned, on what click you need the action, so i consider that on the div contains class='item', the action will be performed. I hope it will help.

How to assign action for earch button in Yii?

I have a page with Groups of Teams which has delete team button next to it.
When team is not in group it has checkbox and button to add team to group.
I wrote in actionView that will render a list of groups with teams.
actionView in GroupController
public function actionView($id) {
$group = $this->loadModel($id);
$teamlst = Group::getAllTeamOfGroup($id);
$teamnotlst = Group::getAllTeamNotInGroup($id);
// Submit
$preSelectedItems = array();
if (isset($_POST['teamlist'])) {
$preSelectedItems = array();
foreach ($_POST['teamlist'] as $selectedItem) {
$preSelectedItems[] = $selectedItem;
}
}
// $teamNo = CHtml::listData($teamnotlst, 'id', 'name');
//Delete
$this->render('view', array(
'model' => $group,
'teamlst' => $teamlst,
'preSelectedItems'=> $preSelectedItems,
'group_id'=>$id,
'teamnotlst' => $teamnotlst,
));
if(isset($_POST['btndeleteteam'])){
TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);
}
}
in view file
<div class="action">
<input type="submit" name="btnupdateteam" value="Update Team">
</div>
<?php echo CHtml::endForm(); ?>
<div class ="team">
<div class="column1">
<?php foreach ($teamlst as $team): ?>
<div class="row">
<?php
echo $team->name;
?>
<input type="submit" name="btndeleteteam" value="Delete Team">
<?php
if(isset($_POST['btndeleteteam'])){
TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);
}?>
</div>
</div><!-- comment -->
<?php endforeach; ?>
<?php
$preSelectedItems = array();
if (isset($_POST['teamlist'])) {
$preSelectedItems = array();
foreach ($_POST['teamlist'] as $selectedItem) {
$preSelectedItems[] = $selectedItem;
}
}
$teamNo = CHtml::listData($teamnotlst, 'id', 'name');
echo CHtml:: checkBoxList('teamlist', $preSelectedItems, $teamNo);
?>
</div>
<div class ="team available">
</div>
My idea is that when you click delete team button it will delete team from group and I has a method for this
TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);
When team not in group it will has checkbox and update button that will add team to group if checkbox is checked.
Thank for advance!
if i understand right what is your problem then you need to read this doc chapter
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action
all your actions i.e. delete or add must reside in controller and not in view
instead of this in view:
if(isset($_POST['btndeleteteam'])){
TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);
}?>
you must add something like this into controller
public function actionDelete($id) {
TeamGroup::model()->deleteTeamGroup($id);
$this->redirect('group/view');
}
and instead of this
<input type="submit" name="btndeleteteam" value="Delete Team">
something like this must be in a view
delete
or you can modify CGridView to suite your needs

Categories