Jump to content

Show all using Toggle/Switch


theglobe

Recommended Posts

I currently have a filter set it up for my conferences list, everything is working perfect. I want to add a toggle/switch that when is on, it shows all the conferences (even the past ones). Right now is only showing the ones starting from today ('value' => date ('Ymd'),). I was thinking to create a function that checks if the switch is on or off and it depends on the answer it give the 'value' a different value. But I've tried everything and it's not working, maybe I am doing something wrong. 

Ps.: I am using date("Y-m-d", strtotime('2017-02-02 17:02:03')); to show the past conferences. If I add that to the 'value' manually it shows the past ones perfectly. 

Screen Shot 2021-12-20 at 9.03.43 AM.png

Link to comment
Share on other sites

When posting code please do not use images. Use the code icon (<>) at the top menu and specify PHP.

You didn't show how that function is called or any HTML so we can't see what you have for the checkbox. However, as a guess perhaps you are not understanding that PHP is server only and is stateless. Just checking a box will do nothing unless there is another submit to the server. It sounds like you need to output the page with PHP making the old conferences hidden. Then when the check box is activated, use JavaScript to make those visible.

Edited by gw1500se
Link to comment
Share on other sites

php functions have (proper) black-box model local variable scope, so that you can freely write code for a function definition without needing to know what every variable is throughout the rest of the code base (wordpress has thousands of functions) in order to avoid conflicts and improper operation.

the $pega variable inside the get_future_conferences function doesn't exist. if you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting an undefined variable error at the 'value'=>$pega line that would alert you to the problem.

the $pega variable should be an optional (there are undoubtedly existing calls) call-time input to the get_future_conferences function, telling it what date to use to match future conferences. apparently the filtering code used by get_posts() uses the current date when no value is supplied.

 

Edited by mac_gyver
  • Like 1
Link to comment
Share on other sites

thank you mac_gyver, and you were right. i changed, but now it's only getting the value of else ($pega = date('Ymd');

code changed: 

function get_future_conferences() {

$pega2 = show_all();

$args = array(

'numberposts' => -1,

'post_type' => 'conference',

'post_status' => 'publish',

'meta_key' => 'conference_start_date',

'orderby' => 'meta_value',

'order' => 'ASC',

'meta_query' => array(

'key' => 'conference_start_date',

'value' => $pega2,

'compare' => '>=',

),

);

$conferences = get_posts( $args );

return $conferences;

}

 

 

Link to comment
Share on other sites

PHP is a scripting language that allows you to write dynamic webpages that use html to display your ideas.  That html is sent to the client and the user looks at it and clicks on html elements (tags such as <input> and buttons such as 'submit') which causes the page contents to be sent to a php script on the server which receives it and processes it.

So far you have a php function that will do something for you but you first have to have some php code that receives the input data from the html, which you don't have.  That is why I first asked you where is the rest of your html.  

So - if you want to have a webpage, work on that first which means you will need to read a bit on html coding.  Look up the form tag, the different input tags and create a page that contains what you need.  Find a sample of a simple html page and modify it to show your ideas.  Then show it to us in PLAIN text form, not as an 'image'.  And as already mentioned be sure to use the proper <> tag on this forum to present it.  Start small and easy and then we'll proceed to the next stage.

Link to comment
Share on other sites

Feeling magnanimous today.  Here is a VERY SIMPLE BARE BONES html page to show you what yours will have to have in order to provide you something to check and submit to your php script.   Look thru it and read up on whatever you don't understand.  It contains the basic html parts that all html pages need but only enough to do one thing for you for now.  

<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<script type='text/javascript'>

</script>
<style type='text/css'>
body
{
	background-color:#c0c0c0;
}
#form_box
{
	position:relative;
	float:left;
	width:30%;
	margin:2% 5%;
	padding:5px;
	border:2px solid blue;
}
</style>
</head>
<body>
<div id='form_box'>
<form name='form1' method='POST' action='' autocomplete='off'>
<label>Show All? (Check for 'Yes'): 
<input type='checkbox' name='showall' value='Yes'>
</label>
<br><br>
<center>
<input type='submit' name='btn' id='subbtn' value='Submit' accesskey='s'>
</center>
</form>
</div>
</body>
</html>

(Note how the use of the <> icon above places the code I wrote into its own box. )

Besides the "form" code I added a tiny bit of CSS to help format the webpage which is something you will most likely want to learn to do as well.  Play with the css and see how it changes your viewing.

I left a space for future JS code should you get that advanced.  You can ignore it for now.  I placed the html code for the form inside a <div> tag which is something that you will also have to learn about as you build your web page with more elements or titles or text.

Realize that this is simply the html code for showing your design to the user.  You can click on the Submit button but nothing will happen since this form is not pointing to any script to run when you do that click.  Once you get this html completed to show what you want you will give it an 'action' value to cause it to be processed by your php script.  That's when you will have to learn the php portion of this exercise.

Edited by ginerjm
Link to comment
Share on other sites

Thank you very much for your reply, and for the example. I already have the entire HTML page ready and working. My only problem now is to have the switch/toggle checking if it's on or off, and passing that to the function check all. I believe is the 'action' you were talking before.  
So, this is my function to check if the toggle/switch is on or off: 

function show_all () {

if (isset($_POST['checkbox'])) {

$pega = date("Y-m-d", strtotime('2017-02-02 17:02:03')); //if ON - $pega is supposed to get this value

}else{

$pega = date('Ymd'); //if OFF - $pega is supposed to get this value

}

return $pega;

}

This is the function that is receiving the value for $pega:

 

function get_future_conferences() {

$pega2 = show_all();

$args = array(

'numberposts' => -1,

'post_type' => 'conference',

'post_status' => 'publish',

'meta_key' => 'conference_start_date',

'orderby' => 'meta_value',

'order' => 'ASC',

'meta_query' => array(

'key' => 'conference_start_date',

'value' => $pega2,

'compare' => '>=',

),

);

 

$conferences = get_posts( $args );

 

return $conferences;

}

//the problem is that the function show_all is not getting that the 'checkbox' is ON. The 'value' on $args is getting only the else from the function show_all. 

Link to comment
Share on other sites

<?php if ( $display_section ) : ?>

<section class="page-section spt spb bg-light-gray cfilter-section">

<div class="wrap ml0 mw-970 mlra">

<div class="cfilter-inner">

<?php if ( ! empty( $filter_title ) ) : ?>

<div class="f18 fwb tac"><?php echo $filter_title; ?></div>

<?php endif; ?>

<div class="filter-select-wraps">

<?php if ( $focus_select ) : ?>

<?php echo $focus_select; ?>

<?php endif; ?>

<?php if ( $industry_select ) : ?>

<?php echo $industry_select; ?>

<?php endif; ?>

<?php if ( $org_select ) : ?>

<?php echo $org_select; ?>

<?php endif; ?>

<?php if ( $fifty_states_select ) : ?>

<?php echo $fifty_states_select; ?>

<?php endif; ?>

<?php if ( $type_select ) : ?>

<?php echo $type_select; ?>

<?php endif; ?>

<select class="conf_speakers">

<option value="all">All Speakers</option>

<option value="speaker-f">Fernandez</option>

<option value="speaker-marcus">Marcus Levine</option>

<option value="speaker-am">Andrew Michael</option>

</select>

<div class="checkbox">

<input type="checkbox" id="top-conference" name="top-conference">

<label for="top-conference">Top Conference</label>

</div>

<div class="filter-reset">

Reset Filters

</div>

<form method ="POST">

<label class="switch" for="switch-1">

<div class="slider round"></div>

<input type="checkbox" name="switch-1" id="switch-1" type="submit" >

</label>

<input type="submit" value="Salva" name="submitBtn">

</form>

</div>

</div>

</section>

<?php

endif;

 

Edited by theglobe
Link to comment
Share on other sites

function test(){
if(isset($_POST['submitBtn'])) { //form submission occured

    if(isset($_POST['switch-1'])){                       
        $pega = date("Y-m-d", strtotime('2017-02-02 17:02:03'));
    }else{
		$pega = date('Ymd');
	}

} 
   return $pega;
}

//FUNCTION SHOWING THE CONFERENCES
function get_future_conferences() {	
	$pega2 = test();
	
	$args = array(
		'numberposts' => -1,
		'post_type'   => 'conference',
		'post_status' => 'publish',
		'meta_key'    => 'conference_start_date',
		'orderby'     => 'meta_value',
		'order'       => 'ASC',
		'meta_query'  => array(
			'key'     => 'conference_start_date',
			'value'   => $pega2,
			'compare' => '>=',
		),
	);

	$conferences = get_posts( $args );

	return $conferences;
}

 

Link to comment
Share on other sites

Here is how the html could look.  Much less complicated and no need for entering and exiting php mode over and over and over.

//*************************************
if ($display_section)
{
	echo "
	<section class='page-section spt spb bg-light-gray cfilter-section'>
	<div class='wrap ml0 mw-970 mlra'>
	<div class='cfilter-inner'>
	";
	if (!empty($filter_title))
		echo "<div class='f18 fwb tac'>$filter_title</div>";
	echo "<div class='filter-select-wraps'>";
	if ($focus_select)
		echo $focus_select;
	if ($industry_select)
		echo $industry_select;
	if ( $org_select )
		echo $org_select;
	if ($fifty_states_select)	
		echo $fifty_states_select;
	if ($type_select)
		echo $type_select;
	echo "<select class='conf_speakers'>
		<option value='all'>All Speakers</option>
		<option value='speaker-f'>Fernandez</option>
		<option value='speaker-marcus'>Marcus Levine</option>
		<option value='speaker-am'>Andrew Michael</option>
		</select>";
	echo "<div class='checkbox'>
		<input type='checkbox' id='top-conference' name='top-conference'>
		<label for='top-conference'>Top Conference</label>
		</div>
		<div class='filter-reset'>
		Reset Filters
		</div>
		<form method='POST'>
		<label class='switch' for='switch-1'>
		<div class='slider round'></div>
		<input type='checkbox' name='switch-1' id='switch-1' type='submit'>
		</label>
		<input type='submit' value='Salva' name='submitBtn'>
		</form>
		</div>
		</div>
		</section>
		";
}

You have two type attributes on your checkbox tag.  Fix

You have an input tag outside of the form tag so that is meaningless.

You need a 'name=' clause on all form input elements or else your php code won't find them.  See your <select> tag.

Note how I changed all of your uses of " and ' characters.  If you begin a string with " then you can reference your php variables inside of them with no issue.  If you begin with the single quote you have to worry about using a php var inside.

Suggestion.   Don't use upper and lowercase in PHP.  It isn't necessary and since php is case-sensitive it can lead to problems when you mis-type the name somewhere in your code and can't figure out why you have a problem.  Stick to all lowercase.

Another suggestion.  Do not use minus signs in your variable names.  Use the underscore.   Something like "$switch-1" looks like a statement of arithmetic instead of a variable name.  Go with "$switch_1".

 

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.