I am working on a website that alternates section depending on the ID, I created this code but noticed one of my parameters is not working correctly
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
if (get_the_ID() != ('34565' or '34565' or '1212' or '1192' or '1219' or '1180' or '1234' or '1221')) {
include 'facebook.html';}
else {
if (get_the_ID() == '1186') {
include 'facebook-1186.html';}else{
echo do_shortcode('[facebook_card]');}}
The ID pointing to 1186 is ok and the else echoing the shot code is ok BUT the array wit the IDs pointing to facebook.html do not load. Is there something wrong with my syntax? Any help is appreciated
Did you try to use the in_array function?
if (in_array(get_the_ID(), $fb_prac)) {
include 'facebook.html';
}
https://www.php.net/manual/en/function.in-array.php
I'm reading the logic like this, if the id matches 1186 include facebook-1186.html, if the id is NOT in your array $fb_prac, then include facebook.html. Else do the shortcode.
<?php
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
$id = get_the_ID();
if ($id == '1186') {
include 'facebook-1186.html';
}
elseif (!in_array($id, $fb_prac)) {
include 'facebook.html';
}
else {
echo do_shortcode('[facebook_card]');
}
Related
I am trying to make a php page where we can input characters and select year of birth
After submitting it should show all the corresponding name but it doesn't work.
For example I input 'a' and the name containing 'a' having the same year as selected should appear.
This is my code:
In data.php I have like this
$person[0]=array('name'=>'Daniel','dateofbirth'=>2000);
$person[1]=array('name'=>'Gaetan','dateofbirth'=>1980);
My function.php
<?php
function comparator($word,$dateofbirth) {
include("data.php");
$p=1;
for($i=0;$i<count($person);$i++) {
$position=strpos(strtolower($person[$i]['name']),$word);
if($position === false) {
$p++;
}
else {
if($dateofbirth==$person[$i]['dateofbirth']) {
echo $person[$i]['name'] ;?><br /><?php
}
}
}
if($p==count($person)) {
echo "No results found";
}
}
?>
And the index.php, I used get method
I think my function.php is the problem but I don't know how to fix it.
I think this should solve your problem - https://3v4l.org/oQ1BC - however I feel it's necessary to say that it's inefficient and in production data are never stored that way, you should store the details of people in a database and connect to it and perform an SQL query. This only answers the original question.
function findPeople($searchString, $YoB, $listOfPeople){
$peopleFound = [];
foreach($listOfPeople as $individual){
if(strpos($individual['name'], $searchString) !== false && $individual['dateofbirth'] == $YoB){
$peopleFound[] = $individual;
}
}
return $peopleFound;
}
$listOfPeople = array();
$listOfPeople[0]=array('name'=>'Daniel','dateofbirth'=>2000);
$listOfPeople[1]=array('name'=>'Gaetan','dateofbirth'=>1980);
print_r(findPeople('a', 2000, $listOfPeople));
The variable in the session only changes if i update the file meaning:
<?php
if(isset($_GET['row']) && isset($_GET['quantity'])) {
if($_GET['quantity'] != 0) {
$_SESSION['basket'][$_GET['row']]['barcode']['quantity'] = $_GET['quantity'];
} else {
unset($_SESSION['basket'][$_GET['row']]);
}
}
?>
Changes the quantity first time and then in other tries doesn't.
If I change it to something like:
<?php
if(isset($_GET['row']) && isset($_GET['quantity'])) {
if($_GET['quantity'] != 0) {
$num = $_GET['quantity'];
$_SESSION['basket'][$_GET['row']]['barcode']['quantity'] = $num;
} else {
unset($_SESSION['basket'][$_GET['row']]);
}
}
?>
I save the file in works once and then it stops changing the quantity.
When echoing the variable in the code in shows that it's changed but in the rest of the site it doesn't. the part where it shows doesn't manipulate the variables only prints them.
It seems as if the code works only if the file's been saved and only for the first time.
Thanks in advance.
Hi guys I wanted to know how to validate 3 things both server and client side.
database table product
my database looks something like this but they are over 400 data in it.
pid name size
1 grey 12 inch
2 blue 16 inch
database table category
pid category
1 vans
2 nike
database table itemised
pid price
1 30.00
2 50.00
I have some fields I need to verify. I have already done a validation to check that it is not empty.
One of the field in my table looks like this
<select id="Category" name="Category">
<?php
dbconnect();
$stmt = $conn->prepare("SELECT Name FROM Category WHERE pid=:id");
$stmt->bindParam('id',$id);
$stmt->execute();
$i = 0;
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
if ($i == 0) {
echo '<option SELECTED value="'.$row['Name'].'">'.$row['Name'].'</option>
';
}
else {
echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
}
$i++;
}
?>
</select>
it is not in a form rather a table. If you notice the size as number and letter it in.
My question
More or less everyone familiar with developer tools to change posted value. I want to validation both client(JS) and client(php) to make sure that no one as mess up the value.
I did this to check that not of it is empty
for an example if have
normal
<option value="blue vans">blue vans</option>
not-normal
<option value="">blue vans</option> // in this one the value is `BLANKET`
The js below check this
function isEmpty(aTextField,errormessage) //null may be used for errormessage to only change the colour of matching fields
{
if (aTextField.value.length<=0)
{
if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,false]; } else { fields[fields.length-1] = [aTextField,false]; }
lfield = aTextField;
if (errormessage !== "")
{
if (counter < error_count || error_count == 0) { errors += errormessage+'\n'; } counter++;
}
return true;
}
else
{
if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,true]; }
lfield = aTextField;
return true;
}
}
in php
function notEmpty($inputname,$error_message)
{
$this->js.='if (!isEmpty(formname.'.$inputname.',"'.$error_message.'")) return false;';
if ( isset($_POST[$inputname]) && ( strlen($_POST[$inputname]) <= 0 ))
{
if ($error_message != null)
{
$this -> error_message .= $error_message.'<br>';
}
}
}
Now you see how I have validate for blanket value for all of the options in my table
How can i verify both in js and php
for the size it will be kinda simple if i didnt have inch at the end but changing over 1000 data in a database will be a pain.
Any idea how I can do this please???
I hope I have explain this clearly and if not please leave a comment and I will try and rephrase the question.
Thanks
I would use regular expressions for both Javascript and php. You can use the same pattern in both languages, so u don't have to write it twice.
php-check for the size:
<?php
if(!preg_match("/^[0-9]+ (inch|othervalue1|othervalue2)$/",$_POST[$inputname]){
$this -> error_message .= 'Wrong Value<br>';
}
?>
For JS, you can take a look at this: http://www.w3schools.com/jsref/jsref_obj_regexp.asp
This might do the trick in JS, but not tested yet:
function checkSize(value){
var patt=new RegExp("^[0-9]+ (inch|othervalue1|othervalue2)$");
return patt.test(value); //returns false for wrong value
}
Furthermore I would suggest to make 2 columns for both value and unit. It would give you several advantages.
You could use intval to convert the string size into an integer, for example intval('12 inch') will return 12.
Try preg_match("/(\d+) inch/", $input_str) or similar to test whether your desired regular expression matches.
You should also be escaping (html special entities) all variable to your HTML output. Otherwise angle-brackets will break the page.
At least you're using prepared statements on the DB side.. thank God. One out of three seems to be good for a PHP "application".
Just to provide you with an alternative way of validating.
You could also post the form via ajax so you only have to validate server-side:
$('#myForm').ajaxForm({
dataType: 'json',
success: function(response) {
if (response.error) {
alert(response.error);
} else {
$('#myForm').text(response.message);
}
}
});
(This is assuming you're using the jQuery plugin http://malsup.com/jquery/form)
Then in you're PHP you should check whether the current request is ajax:
function isAjax() {
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
}
...
if (isAjax() {
$response = array();
if (!empty($this->error_message)) {
$response['error'] = str_replace('<br>', "\n", $this->error_message);
} else {
$response['message'] = 'Form submitted';
}
header('Content-type: application/json');
echo json_encode($response);
exit;
}
Why won't my search function ever execute the "else" (else should echo a text when no resulsts haved been found)? I also have some problems when trying to show all results (with no search criterias selected, just by pressing the search button). I'll upload the whole code of the page because I don't know if you need the HTML part as well or not to figure out the problem. I know it's a big chunk of code but please help out if you can. Thanks!
Here's a link to my code: http://pastebin.com/BXe1C0dr
This is not yet the answer, just a brief code structure of Matei Panchios code. Because it is hard to make sense of long code, so I try to simplify it so that other people might be able to help.
$termeni = mysql_real_escape_string($_POST['termeni']);
$tip=$_POST['tip'];
$judet=$_POST['judet'];
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
$query = "SELECT * FROM oferte WHERE localitate LIKE '%$termeni%' AND
tip_locatie='$tip' AND judet='$judet'";
// do the query and write some HTML
} elseif (isset($tip)) {
$query = "SELECT * FROM oferte WHERE tip_locatie='$tip'";
// do the query and write some HTML
} elseif (isset($judet)) {
$query = "SELECT * FROM oferte WHERE judet='$judet'";
// do the query and write some HTML
} elseif (!empty($termeni)) {
...
} elseif (!empty($termeni) AND (isset($judet))) {
...
} elseif (!empty($termeni) AND (isset($tip))) {
...
} elseif ((isset($judet)) AND (isset($tip))) {
...
} elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni))) {
...
} else {
// I believe this where it does not get executed.
}
Well, it makes sense why it does not get executed because there is other way that the elseif does not cover. If you look from this point of view
If three variable is set
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
If two variables is set
elseif (!empty($termeni) AND (isset($judet)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
If one variable is set
elseif (isset($tip))
elseif (isset($judet))
elseif (!empty($termeni))
When no variable is set
elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni)))
which leave else condition with nothing to cover.
If I were you, I would structure the code as following.
if (!empty($termeni) and isset($tip) and isset($judet)) {
query = '....';
} elseif (!empty($termeni) and isset($judet) {
query = '....';
} // .... the rest of the condition
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
// write HTML table
} else {
// write message that there is no result found
}
This will reduce the size of your code by 6 times.
I'm trying to write a statment that looks at the value in a boolean field (field_solo) and returns one of two template files that I have created in Drupal 7.
My field "field_solo" is correctly outputting a value of 0 or 1 and I have cleared the cache.
Can someone tell me if I am doing this correctly? Right now I am not getting it to display when the statement is TRUE.
function motg_preprocess_node(&$vars) {
$node = $vars['node'];
if($node->field_solo[0]['value'] == 1)
{
$vars['theme_hook_suggestion'] = 'node__solo';
} else
{
$vars['theme_hook_suggestion'] = 'node__video';
}
}
Instead of
if($node->field_solo[0]['value'] == 1)
Make it
if($node->field_solo['und'][0]['value'] == 1)
// OR
if($node->field_solo[LANGUAGE_NONE][0]['value'] == 1)
Have a look at this it could be helpful:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
This worked for me
<?php if ($content["field_hide_title"]["#items"][0]["value"] == 0) { ?>
<?php echo $node->title; ?></h2>
<?php } ?>