Laravel 5.4 Use If/Else inside of a controller [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to use #if & #else inside a controller, eg:
public function deleteCharacter ($char_delete_id)
{
#if (Auth::user()->id == $delCharacterUserID)
$deleteSuccess = 1;
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
#else
$deleteSuccess = 0;
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
#endif
This throws the error:
FatalErrorException in CharacterController.php line 40: syntax error,
unexpected 'if' (T_IF)

In a controller, just use the normal PHP syntax, not blade syntax. So replace your code by:
public function deleteCharacter ($char_delete_id)
{
if (Auth::user()->id == $delCharacterUserID) {
$deleteSuccess = 1;
} else {
$deleteSuccess = 0;
}
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
}
And it should work! A controller is a PHP file, so write PHP in it.

Optimize your code like that
public function deleteCharacter ($char_delete_id)
{
$deleteSuccess = Auth::user()->id == $delCharacterUserID?1:0;
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
}

Related

Comparing two identical arrays returns false [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to compare two arrays with each other with this code:
if($jobids !== null){
if (isset($_COOKIE["djsearchquery"])){
$cookiequery[] = unserialize($_COOKIE['djsearchquery']);
$arrayequal = ($cookiequery == $jobids);
$consolelog = $cookiequery;
$consolelog[] = $jobids;
$consolelog[] = $arrayequal;
if($arrayequal == false){
$response = array(
'jobids' => $jobids,
'markerpositions' => $markerpositions,
'consolelog' => $consolelog
);
setcookie('djsearchquery', serialize($jobids), time()+3600);
echo json_encode($response);
}
}
In the console the arrays are pictured exactly the same:
Can someone explain to me why $arrayequal returns false? I don´t understand it.
try to change
$cookiequery[] = unserialize($_COOKIE['djsearchquery']);
to
$cookiequery = unserialize($_COOKIE['djsearchquery']);

mysql php - check if last variable is the same [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I tried to echo out the $rinse variable, I get nothing
But I do get one for the $rang['email'], any clues would be good
I also tried doing $rinse == $rang['email'];
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] = $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}
my code updated:
echo $rinse;
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email']
This is still not working for me
You need two = in your if statement.
if ($rang['email'] == $rinse){
You should add double ==. This is comparion, a single = means set to
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}

PhP Error: FUNCTION [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<?php
function me1($str) {
$hash = 'http://me1.wink.ws/me1/request.php?a=' . $str;
return $hash
}
$hi = me1('Hi');
?>
Where is my error?
Parse error: syntax error, unexpected '}' in /home/u727762781/public_html/client/me1.php on line 5
Fixed Code:
<?php
header('Content-Type: text/plain');
function me1($str) {
$hash = file_get_contents('http://me1.wink.ws/me1/request.php?a=' . $str);
return $hash;
}
$hi = me1('Hi');
echo $hi;
?>
You forgot the semi-colon at the end of the return statement.

How to define an Array property in a PHP class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a following simple code but it produces syntax error on line this->players[] = 'Tom':
<?php
class club {
var $clubID = 0;
var $players=array();
function __constructor($clubID = '') {
$this->clubID = $clubID;
}
function populatePlayers() {
$this->players[] = 'Tom';
}
}
$myClub = new club(1);
$myClub->populatePlayers();
var_dump($myClub->players);
?>
It should be
$this->players[] = 'Tom';
instead of
$this->$players[] = 'Tom';
You need to add $ before the this keyword and the players variable does not need one.
Demo
try this
function populatePlayers() {
$this->players[] = 'Tom';
}

Syntax error - unexpected ":" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My php script directs to a url depending on which submit button was pressed. However, when I run the test I'm getting an error saying line 4 contains an unexpected ":" but my line 4 is my header script with the url?
I'm confused because I have other scripts similar to this and they don't give me that error. Can anyone tell me what I'm missing, might be simple, I have been caught being simple before.
<?php
if ($_REQUEST['Dish1'] == 'Dish1')
{
header(“Location: http://blahblah”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header(“Location: http://blahblah2”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header(“Location: http://blahblah3”.urlencode($_POST[‘uid’]));
}
etc.....
?>
You are using curly quotes.
Replace all the “ ” and ‘ ’ to " and ' respectively.
You are using the wrong quotes... use "" instead of “”. Refer to Wikipedia, you must use typewriter quotes, not curly or inverted commas.
PD: Also PHP Parse error: syntax error, unexpected '.' on line 15 ; )
Replace you code with following
<?php
if ($_REQUEST['Dish1'] == 'Dish1')
{
header("Location: http://blahblah.urlencode".($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header("Location: http://blahblah2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header("Location: http://blahblah3".urlencode($_POST['uid']));
}
?>
Is it not much easier to write:
$lookup = array('Dish1' = > 'http://blba1', 'Dish2' = > 'http://blba2');
if( isset($lookup[$_REQUEST['Dish1']]))
header("Location: " . $lookup[$_REQUEST['Dish1']]);

Categories