php code only work if files has been changed - php

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.

Related

Creating a simple php page that allows the user to input characters to search for names

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));

PHP array selection not working correctly

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]');
}

Laravel 5 Simple Comparison Fails

This should be simple but for some reason code in my if block is executing despite the fact that it resolves to false and it's making me very unhappy... My user_id in this case is 2.
$note = Notification::where("user_id",Auth::user()->id)->first();
$wall = $note->pluck('wall');
if($wall != 0)
{
//This code is executing!
}
else{
array_push($data,"Your First Time!");
//This code is not!
}
As you can see, my $wall should be zero so I don't understand why $wall != 0 runs.
Remove pluck
$note = Notification::where("user_id",Auth::user()->id)->first();
$wall = $note->wall; //This changed
if($wall != 0)
{
//This code is executing!
}
else{
array_push($data,"Your First Time!");
//This code is not!
}

if statement when NULL (PHP)

I want to have if the variable = null then he makes it to 1.
if the variable exist do nothing and dont make it again to 1.
i got this code:
if (isset($_POST["register"])) {
if(!$l_NextPage){
$l_NextPage = 1;
echo "helaas" . "</br>";
}
if($l_NextPage == 1){
echo "hoi";
$l_NextPage = 2;
}else if($l_NextPage == 2){
echo "doei";
}
}
only the code dont work i tried empty, isset, $var == FALSE but everytime he makes $l_NextPage to 1. is there any solution i tried this too with session but even it don't work!
What am I doing wrong?
what happen when you refresh page, it assign $l_NextPage = 1 every time, thats why all the time hoi printed
you can use sessions for preserving value of variable after page refresh
try this code
// write this line of code at top of php block
session_start();
if (isset($_POST["register"]))
{
if (!isset($_SESSION["l_NextPage"]))
{
$_SESSION["l_NextPage"] = 1;
echo "helaas" . "</br>";
}
if($_SESSION["l_NextPage"] == 1)
{
echo "hoi";
$_SESSION["l_NextPage"] = 2;
}
else if($_SESSION["l_NextPage"] == 2)
{
echo "doei";
//unset( $_SESSION['l_NextPage'] ); unset varibale
}
}
after reaching at prefixed condition you can unset varible using
unset( $_SESSION['l_NextPage'] );
i have not tested code but this should work
you should try:
if(!isset($l_NextPage)) {
$l_NextPage = 1;
echo "helaas" . "</br>";
}
elseif($l_NextPage == 1) {
(...)
try like this,
if(!empty(trim($l_NextPage)))

If/then statement on field value in drupal 7

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 } ?>

Categories