PHP nested if using $_GET [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 1 year ago.
Improve this question
Trying to use a $_GET within a $_GET
It's not throwing any errors but isn't working the way I want it to. It's been a while since I've done this so I don't know if I'm overlooking something or not. I'm trying to be able to do something like index.php?chatroom&id=1
$pgtitle = '';
$cractive = '';
$dactive = '';
$acactive = '';
$pgChat = '';
if(isset($_GET['chatroom'])){
$cractive = 'active';
if (isset($_GET['cid']) == "1") {
$pgChat == 'Global Chatroom';
}else if(isset($_GET['cid']) == "2"){
$pgChat == 'AK Chatroom';
}else if(isset($_GET['cid']) == "3"){
$pgChat == 'AZ Chatroom';
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}else{
header('Location: index.php?dashboard');
}

isset() returns a boolean not the content of the variable, and you was using == instead of = in $pgChat == 'Global Chatroom';, here is an example using your code for something like index.php?chatroom&cid=1:
if (isset($_GET['chatroom'])) {
$cractive = 'active';
if (isset($_GET['cid'])) {
if ($_GET['cid'] == "1") {
$pgChat = 'Global Chatroom';
} elseif ($_GET['cid'] == "2") {
$pgChat = 'AK Chatroom';
} elseif ($_GET['cid'] == "3") {
$pgChat = 'AZ Chatroom';
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}
EXTRA: You can do the same thing in that way if you wanna.
$chats = [
'1' => 'Global Chatroom',
'2' => 'AK Chatroom',
'3' => 'AZ Chatroom',
];
if (isset($_GET['chatroom'])) {
$cractive = 'active';
if (isset($_GET['cid']) && isset($chats[$_GET['cid']])) {
$pgChat = $chats[$_GET['cid']];
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}

Related

PHP - IF doing all the time only first condition, why? [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
why the IF(the lastest one with else if and else) is doing all the time only first condition and only the first part ($filtry_1value[$key] = 'min_cena'), even if the condition shouldnt be true. I have another solution (less dynamic), if I will not fix this one, but I would like to know, why it is not working... I think it will be a trivial thing, but I cannot see it.
PS: I am working with laravel.
$filtry_1value = ['stat', 'lokalita', 'patro', 'min_cena', 'max_cena', 'min_uzitna_plocha', 'max_uzitna_plocha'];
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] = 'min_cena' OR $filtry_1value[$key] = 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] = 'max_cena' OR $filtry_1value[$key] = 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
may be-
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
You need to use the double equal sign for comparisons. == not a single =
Your if's should look like:-
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
// ...
} elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
// ...
}

Combining several targeted value's / variable in one piece

We currently use the following piece of PHP code to target (change) several variables. Though this works, is there some way to shorten it? I am guessing there is a shorter way, however we don't have any PHP knowledge.
if($form_id == 10767){
if($data['element_id'] == 13){
if(empty($data['value'])){
$data['value'] .= '';
}else{
$data['value'] = '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
}
if($form_id == 10767){
if($data['element_id'] == 14){
if(empty($data['value'])){
$data['value'] .= '';
}else{
$data['value'] = '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
}
if($form_id == 10767){
if($data['element_id'] == 16){
if(empty($data['value'])){
$data['value'] .= '';
}else{
$data['value'] = '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
}
if($form_id == 10767){
if($data['element_id'] == 17){
if(empty($data['value'])){
$data['value'] .= '';
}else{
$data['value'] = '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
}
I think it's possible to combine those element_id's into one single line, instead of targeting them seperately, right?
Something like this:
if($data['element_id'] == 13,14,16,17)
I tried a few things myself, but every time I get an error or break things.
Thank you kindly in advance.
If you load your targets into an array and then us in_array(needle, haystack) it can be easily reduced.
With a ternary operator instead of an IF it can be even more reduced.
$target_ids = array(13,14,16,17);
if($form_id == 10767){
if( in_array($data['element_id'], $target_ids)) {
$data['value'] = empty($data['value']) ? '' : '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
You're currently use the same if-statements several times, which isn't needed:
Then you should look into the documentation about "if" and about Logical Operators and you will find how to write "or":
if($form_id == 10767){
if($data['element_id'] == 13 || $data['element_id'] == 14 || ...etc... ){
if(!empty($data['value'])){
$data['value'] = '<p style="font-size:13px;">'.$data['value'].'</p>';
}
}
}
I also removed $data['value'] .= ''; on empty, since it doesn't do anything. (notice the ! before empty() which means that it validates if the value is not empty.)

why is php class class giving me error when trying to access the 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 8 years ago.
Improve this question
this is the error I get.
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
in /home/students/000313753/public_html/10065/php/lab2/lab2.php on
line 121
my class
class validate
{
public $errorArray;
function userName($name)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($name))
{
if(strlen($name) <= 5)
{
$this->$errorArray['name'] = 'The username must be 5 characters.';
}
}
else
{
$this->$errorArray['name'] = 'Username cannot be empty';
}
}
}
function validateEmail($email)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$this->$errorArray['email'] = 'Wrong email format.';
}
}
}
function validateYear($year)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($year))
{
if ($year < 1000 || $year > 2100)
{
$this->$errorArray['year'] = 'Year must be a 4 digit number.';
}
}
else
{
$this->$errorArray['year'] = 'Year cannot be empty';
}
}
function validateProvinces($provinces)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($provinces))
{
if (empty($provinces))
{
$this->$errorArray['provinces'] = 'Please select one or more provinces';
}
}
}
}
function validateStatus($status)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($status))
{
if (empty($status))
{
$this->$errorArray['status'] = 'Please select one or more items from status';
}
}
}
}
}
//access class
$validation = new validate()
$validation->userName($_POST["name"]);
$validation->validateYear($_POST["year"]);
$validation->validateProvinces($_POST["provinces"]);
$validation->validateStatus($_POST["status"]);
$validation->validateEmail($_POST["email"]);
?>
I am a beginner and I have over a day trying to debug. Please do not just fix my code provide me with an explanation of why my code do not work.
Thank you in advance for any help.
You're missing a closing semi-colon here:
$validation = new validate()
Should be:
$validation = new validate();
you are missing closing brace } in function validateYear()

Simple PHP issue that I cant seem to get to work [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
Im working on a simple roll a dice program. So it has to generate a random number from 0-5 and when it selects 0-4 it echo's and when it selects 5 it echo's.
To it keeps echo the first echo 'you got 6'. I cant seem to get my mind around this.
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$trows = 0;
$dice = 0;
while ($dice == 0)
{
$dice = rand(0,5);
$trows++;
}
if ($dice = 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
?>
</body>
if ($dice = 5)
Is actually assigning the value of 5 to dice, rather than comparing the values, so the first part of the if statement will be entered, rather than the else.
if ($dice == 5)
is what you are looking for
Edit: From your comments I think this is what you are looking for
$trows = 0;
$dice = 0;
while ($dice != 5)
{
$dice = rand(0,5);
$trows++;
if ($dice == 5) {
echo 'you got 6';
} else {
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
}
Your if statement is not testing for a condition, but rather performing an assignment and then evaluating the truth of that assignment (which will always be true in this case).
Your code should be:
if($dice == 5)
{
echo 'you got 1-5';
}
Notice the == instead of the single =
This will do the trick
while ($trows == 0)
{
$dice = rand(0,5);
if ($dice == 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
$trows++;
}
}

error in write code in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want a system like this :
function check()
{
$sqlquery = mysql_query("SELECT * FROM clients WHERE id='$this->ClientID'
AND username='$this->ClientUSERNAME'") or die(mysql_error());
$showmysql = mysql_fetch_array($sqlquery );
$statusOK = $showmysql['status'];
if ($statusOK == "Active" || "1");{
return true;
}
else if ($statusOK == "Pending");{
$this->Redirect("pending.php");
}
else if ($statusOK == "Susspended");{
$this->Redirect("sus.pjp");
}
}
But When i put this codes in adobe dreamweaver i see the errors in (})... What is my problem?Please write the true code
Remove semicolon ; from if-else
if ($statusOK == "Active" || "1");{
^
else if ($statusOK == "Pending");{
^
if ($statusOK == "Active" || "1") {
else if ($statusOK == "Pending") {
Remove semicolons into your conditions like if, else if
<?php
function check()
{
$sqlquery = mysql_query("SELECT * FROM clients WHERE id='$this->ClientID'
AND username='$this->ClientUSERNAME'") or die(mysql_error());
$showmysql = mysql_fetch_array($sqlquery );
$statusOK = $showmysql['status'];
if ($statusOK == "Active" || "1")
{
return true;
}
else if ($statusOK == "Pending")
{
$this->Redirect("pending.php");
}
else if ($statusOK == "Susspended"){
$this->Redirect("sus.pjp");
}
}
?>
function check()
{
$sqlquery = mysql_query("SELECT * FROM clients WHERE id='$this->ClientID' AND username='$this->ClientUSERNAME'") or die(mysql_error());
$showmysql = mysql_fetch_array($sqlquery );
$statusOK = $showmysql['status'];
if ($statusOK == "Active" || "1") {
return true;
}
else if ($statusOK == "Pending") {
$this->Redirect("pending.php");
}
else if ($statusOK == "Susspended") {
$this->Redirect("sus.pjp");
}
}
Remove semicolon(;) after if else if then it will work. Try it...
Remove Semicolon after if else condition and try it below code
function check()
{
$sqlquery = mysql_query("SELECT * FROM clients WHERE id='$this->ClientID'
AND username='$this->ClientUSERNAME'") or die(mysql_error());
$showmysql = mysql_fetch_array($sqlquery );
$statusOK = $showmysql['status'];
if ($statusOK == "Active" || "1"){
return true;
}
else if ($statusOK == "Pending"){
$this->Redirect("pending.php");
}
else if ($statusOK == "Susspended"){
$this->Redirect("sus.pjp");
}
}

Categories