Wordpress - Display text if checkbox output matches the page title - php

I want to show the projects that has had it's checkbox ticked as Branding, if it's on the Branding page (i.e the page title is Branding).
To explain the code a bit:
This line show's all the checkboxes that have been ticked for each project so it will output "Branding", "Web", "Print" if they have been ticked.
implode(', ',get_field('categories')
This next line is just checking the page title is "Branding":
implode(', ',get_field('categories')
I'm trying to put these both in an if statement where it would just output the checked boxes and if they match the title then output them.
<?php if(implode(', ',get_field('categories')) && $grid_title == "Branding"); {
echo "testing";
}
?>
The code above shows what I want to do but it doesn't quite work.
IMPORTANT: I'm using this plugin to create the custom checkboxes so please bear that in mind.
=============================
UPDATE:
Thanks very much to Adam Kiss for solving what I asked, small update to question:
How could I code this neatly - using your answer, Branding was just one example of the check boxes, there's also several other one's like Web, Print, Social so how could I match those to the page title as well?
So it will be along the lines of if checked field equals the page title "branding" do OR checked field equals page title "web" OR checked field equals page title "print".

the function you're looking for is in_array:
<?php
if(
in_array("Branding", get_field('categories'))
&& $grid_title == "Branding"
){
echo "testing";
}
Note: this assumes that result of that implode is array with strings like "Branding", "Web", etc.
Edit: Since we're using implode(), I assume get_field returns type array, so we put the implode away (I got confused for a while)
Edit: Sorry, was away :]
you could use array_intersect
Usage:
$categories = get_field('categories');
$cats_iwant = array("Branding", "Print", "Design");
$inarray = array_intersect($categories, $cats_iwant);
//this '$inarray' now has values like 'Branding', 'Design' which are in both arrays
if (count($inarray) > 0) {
//we have at least one common word ('Branding', ...)
}
//short version
if (count(array_intersect(get_field('categories'),array(
'Branding', 'Design', 'Print'
))) > 0)
{
//do stuff
}

Related

PHP counting each new line In textarea field as a new line and wrapping in a list item

I am using a textarea (advanced custom fields) to capture the users selection.
So the user first answers if there are education areas nearby - yes/no then triggers a radio input with numbers from 1-10, then depending on that, i need to return my information. Right now i am just showing the returned input for the selection of 1 education area.
What want to do, is count the spaces in the text area and wrap each line around an <li> $var </li>.
My code below is only returning the second half of my textarea input.
My textarea is:
School
Distance
The below code is only returning
<li>Distance</li>
My Code
if( $edu_close == 'Yes' ) {
$lines = explode("\n", $edu_close1);
$count = count ($mylines);
foreach ( $lines as $line ) {
$isedu = '<li>'.$lines.'</li>';
}
}
elseif( $edu_close == 'No' ) {
$noedu = 'Nothing Found';
}
$poidetails .= $isedu;
$poidetails .= $noedu;
return $poidetails;
Textarea
Result
Am I doing something wrong that it is only reading the end of the textareas input?
you are overwriting $isedu in every cycle. you should have it like this
...
$isedu .= '<li>'.$lines.'</li>';
...

Compare 2 strings in PHP

I'm in trouble because i can't figured it out how to find my answer.
I'm creating a wordpress website and i have a search engine
I've created an option in my admin panel and code something to write in my option table, but my problem is now
firstly i grab my options from my database with
$mysynonymvalue = get_option( 'synonym-custom' );
I precise that it returne to me something like this ( mango, apple, banana)(this is an example of course)
My Url is something like this :
http://supserwebsite/wordpress/?sfid=2675&_sf_s=toto
Or this
http://superwebsite/wordpress/?sfid=2675&_sf_s=virtualisation cloud devops
so i've created a variable to catch the queries
$motsclefs3= $_GET['_sf_s'];
Now i want to compare the string $mysynonymvalueconvert with $motsclefs3 to find if it match so i write
if (strpos ($mysynonymvalue, $motsclefs3) ){
echo '<script >
$(document).ready(function(){
$(".vc-tabs-li:nth-child(2)").get(0).click();
});
</script>';
}
else{
echo '
<script >
$(document).ready(function(){
$(".vc-tabs-li").get(0).click();
});
</script>';
};
};
The solution seem to work correctly but i can't have the first result, it coompare indeed with all the results but not my first one.
And it doesn't work so fine because with only one letter it return a match ( for example 'a')
Any solution ?
Thanks
*provided url(s) are not accessible.
Solution:
Create a dictionary of option from db and then search the element which you are looking.
So far i move up a bit !So i came with this
I still have
$mysynonymvalue = get_option( 'synonym-custom' );
$mysynonymvalueconvert = preg_split('/[,]+/',$mysynonymvalue);
To grab my words from my database and to convert it into an array.
(the point of this is to get elements that were wrote by an user elsewhere on the admin panel of wordpress)
I also still have
$motsclefs3= $_GET['_sf_s'];
To grab my actual queries ( that will serv me to compare ). I precise that it return me a string. To be more specific, an URL like this (http://mywebsite/wordpress/?sfid=2675&_sf_s=examen) return me (in string) "examen".
Now my point is still to compare if
$motsclefs3;
is inside
$mysynonymvalueconvert
So i've created a "for" loop like this
for ($i = 0; $i <= count($mysynonymvalueconvert); $i++){
if(in_array($motsclefs3, $mysynonymvalueconvert)){
echo'yes';
break;
}
else{
echo 'no';
break;
};
};
But i'm still blocked, this return "yes" only if it match with the first element from
$mysynonymvalueconvert
So any ideas to help me ?
Thanks!

Adding bullets to all items from a data variable

I am using Machform, which is a form creater / maker, for our forms. Unfortunately the developer has no interest anymore in offering support on questions. So I am wondering if someone on stackoverflow can provide some help.
I am using the following piece of code:
if($form_id == 65822){
if($data['element_id'] == 52){
if(!empty($data['value'])){
$data['value'] = '<p>Example text:</p><ul style="color:#444444 !important;font-size:14px;list-style:square;"><li><a style="text-decoration:none !important;color:#444444 !important;" href="#">'.$data['value'].'</a></li></ul>';
}
}
}
This works when there is a single item being displayed. The item gets a square (bullet) in front of the item.
However when there are more items, only the first item gets a square (bullet). The rest don't get a bullet.
First item
Second item
Third item
It should, however, be displayed like this:
First item
Second item
Third item
And so on
So everything which is being displayed should have a square (bullet) in front of it.
Is there an easy fix for this?
The $data['value'] variable is collected from the stuff entered in the form. In this case it's multi-text line / paragraph field.
It would better to modify the area of code that collects the text that goes into $data['value'].
Maybe create a new array variable $data['bullets'] and push each bullet to that individually:
$data['bullets'][] = "'<li><a style="text-decoration:none !important;color:#444444 !important;" href="#">First item</a></li>";
$data['bullets'][] = "'<li><a style="text-decoration:none !important;color:#444444 !important;" href="#">Second item"</a></li>;
$data['bullets'][] = "'<li><a style="text-decoration:none !important;color:#444444 !important;" href="#">Third item</a></li>";
if($form_id == 65822){
if($data['element_id'] == 52){
if(!empty($data['value'])){
$data['value'] = '<p>Example text:</p><ul style="color:#444444 !important;font-size:14px;list-style:square;">'.implode('',$data['bullets']).'</ul>';
}
}
}

Echo of Option Menu Shows as "%"

I have an option menu with several options including show all records. The option menu is fed by a query on a MySQL table. I am trying to echo the option selected and everything works until I select all records. Then the echo shows as %. Would like for it to echo ALL when All records is selected from option menu. I am using the following to echo:
<?PHP echo isset($_POST['selGrade']) ? $_POST['selGrade'] : 'ALL'; ?>
Currently the default option is all records, which echos ALL correctly with my PHP statement above. Problem happens when I select another option and then select all records again. Then it echos "%". What can I add to the PHP statement to echo ALL when a user selects All records?
With the help of Peter Darmis, the solution to my question is:
<?PHP
if(!isset($_POST['selGrade']) || empty($_POST['selGrade']) ||
$_POST['selGrade']=="%"){
$text = "ALL";
} else {
$text = $_POST['selGrade'];
}
echo $text;
?>
I asked a similar question at Option Menu Showing Undefined Index. Thanks to all.
I'm guessing "all records" empties the $_POST['selGrade'] but doesn't unset it. Consider using !empty($_POST['selGrade']) instead of isset().
Function empty()

PHP + MsSQL = Checkbox problems. Need HELP!

I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!

Categories