PHP IF Else Function Not Working - php

I have a problem, I can't get the result of my code. When I try to debug the code. I using IF statement in my code.
It's just simple, example : if kelas(based on user_id login) = 1, then redirect it to kelas1.php, if kelas = 2, the redirect to kelas 2, else no have a kelas.
Here it's my code :
<?php
session_start();
if(!isset($_SESSION['user_id'])) {
header('Location: form_login_siswa.html');
}
$user_id = $_SESSION['user_id'];
include ("config.php");
$query = "SELECT kelas FROM t_siswa WHERE user_id = '$user_id'";
$hasil = mysql_query($query);
while ($data = mysql_fetch_array($hasil)) {
$kelas = $data['kelas'];
if($kelas = 1) {
include ("kelas1.php");
}
if($kelas = 2) {
include ("kelas2.php");
} else {
echo "Tidak ada kelas";
}
}
?>
Anyone, please help to solve the problem.
Appreciated with your helps.
Thank you.

You're missing an else and also not using the correct operator for comparison.
Also, switch around the comparison so that the first value is always a valid value.
if(1 == $kelas)
{
include ("kelas1.php");
}
else if(2 == $kelas)
{
include ("kelas2.php");
}
else
{
echo "Tidak ada kelas";
}

$kelas = 1
is assignment
$kelas == 1
is comparison

if($kelas = 1)
should be
if($kelas == 1)

Try using == instead of = . The "=" is an assignment operator and the expression gets the value of the right operand.
if($kelas == 1)
{
include ("kelas1.php");
}
if($kelas == 2)
{
include ("kelas2.php");
}
else
{
echo "Tidak ada kelas";
}
Also, the statement will always echo "Tidak ada kelas"; if ($kelas == 1). Was this intentional?

In your if statements using == for testing comparison rather than single = which is for assignment

Take a look at the elsif structure.
while ($data = mysql_fetch_array($hasil)) {
$kelas = $data['kelas'];
if($kelas == 1) {
include ("kelas1.php");
} elseif($kelas == 2) {
include ("kelas2.php");
} else {
echo "Tidak ada kelas";
}
}
Or the switch structure:
while ($data = mysql_fetch_array($hasil)) {
switch($data['kelas']){
case 1:
include ("kelas1.php");
break;
case 2:
include ("kelas2.php");
break;
default:
echo "Tidak ada kelas";
}
}

Related

How i can use if/else/elseif statment in my case - PHP

How i can use if/else/elseif... in my case,
Because when i try those statment,
It says: syntax error, unexpected 'if' (T_IF)
I need to repeat elseif more than 1x time
$row = array();
$row[] = if($aRow['status'] == "deleted"){'code..'};
This might be what you are after. You would loop over each record in your result array you get from fetchAll(), put your if/elseif block here, then I'm assuming your doing some sort of processing and saving the value to $row array?
$row = array();
$result = $sth->fetchAll();
foreach($result as $aRow){
if($aRow['status'] == "deleted"){
//do something
$row[] = //whatever
}
elseif($aRow['status'] == "something else"){
//do something else;
$row[] = //whatever else
}
}
You have 2 ways for implementing this by PHP:
The 1st way
you can use if/else/elseif:
if ($status_var=="deleted")
{
//code .....
}
elseif ($status_var=="inserted")
{
//code .....
}
elseif ($status_var=="edited")
{
//code .....
}
esle
{
//code .....
}
The 2nd way
You can use switch/case structure:
switch ($status_var)
{
case "deleted":
//code .....
break;
case "inserted":
//code .....
break;
case "edited":
//code .....
break;
default:
//code .....
}
You can use ternary operator to assign the result:
$row = array();
$row[] = $aRow['status'] == "deleted" ? 'code..' : null;
When you need to do more things depending on status, best way would be to introduce a function for that:
function process($aRow) {
if ($aRow['status'] == "deleted") {
// do something when `deleted`
return 1;
} elseif ($aRow['status'] == "new") {
// do something when `new`
return 2;
} elseif ($aRow['status'] == "updated") {
// do something when `updated`
return 3;
} else {
// do something else
return 4;
}
}
$row = array();
$row[] = process($aRow);

if user is blocked php include page, else include other?

Can someone please help me, i am trying to incorporate this piece of code:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
to fit this piece of code as an else statement:
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
I have a table in my database that puts a users block status from 0 to 1. and if a user blocks someone and that user tries to access their profile then i am trying to make it so that the user goes to another page that says blocked. i am doing this through <?php include(.. ?>
At the moment i just tried putting this at the top of the page:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
and whilst it is working and including the page mod_blocked.php its also bringing up mod_profile.php which is the default profile page and overlapping. So basically if a users not blocked they should go to mod_profile.php and if a users blocked they go to mod_blocked.php.
can someone please show me where im going wrong and how to achieve this?
Here's the entire page of code:
<?php
$page_title = "Profile";
include('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
?>
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
<?php
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
}
else if (!isset($profile_id)) {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
else if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include("includes/mod_profile/mod_account.php");
}
?>
I'm not sure if I fully understand what you are up to, but when I got your question correct, you could do it the following way:
$page_title = "Profile";
include('includes/headerframe.php');
// stores the correct include file...
$toInclude = false;
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
// look up for blocked user..
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
// don't do the include inside the loop
// we simply set the $toInclude
$toInclude = "includes/mod_profile/mod_blocked.php";
break;
}
}
$user_info_set = get_user_info();
// in case no include is set, and there is no profile...
if ( !$toInclude && (!$user = mysql_fetch_array($user_info_set) ||
!isset($profile_id)) ) {
// we set the $toInclude now
$toInclude = 'includes/mod_profile/mod_noprofile.php';
}
if($toInclude) {
// either blocked or no-profile
include($toInclude);
} else {
// user is not blocked, and you have a profile
// proceed with your code for normal user handling here
}
Couple options here.
Add break or die() like such
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
break;
}
}
?>
Or, surround it in the else
<?php
$page_title = "Profile";
include ('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset($_GET['id'])) {
$profile_id = $_GET['id'];
die();
}
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_blocked.php");
} else {
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
} else if (!isset($profile_id)) {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include ("includes/mod_profile/mod_profile.php");
} else if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include ("includes/mod_profile/mod_account.php");
}
}
}
?>

If / else if / else with query to a database resulting false.

I have a select box with an option for All, and then a list of users.
What I'm struggling with is creating something like this. I have most of it except trying to query the database to for it to check if the variable is in the database.
if ($variable == 'All') { code here }
else if ($variable != 'ALL' != *[result in database]*) { code here }
else { code here }
I have most of it except trying to query the database to for it to check if the variable is in the database.
Any suggestions how I can encorporate a query of a mySQL database in my if statement.
Thanks
if ($variable == 'All') {
... do something ...
} else {
$sql = "SELECT ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if ($row['somefield'] == 'whatever') {
... do something else ...
} else {
... do something even "elser" ...
}
}
You can't use operators like that.
Replace:
else if ($variable != 'ALL' != *[result in database]*) { code here }
With:
else if ($variable != 'ALL' AND $variable != *[result in database]*) { code here }

if else simple beginner issue

Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}

Optimising a PHP If/Else statement

I'm attempting to optimise the following PHP If/Else statement. Could I rewrite the code to make use to case and switch, or should I leave it as it is, or what?
Code:
if(empty($_GET['id'])){
include('pages/home.php');
}elseif ($_GET['id'] === '13') {
include('pages/servicestatus.php');
}elseif(!empty($_GET['id'])){
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if(!$rawdata){
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
}else{
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if(!$rawdata){
error_404();
}
}
Thanks
I hate switch statements, but its personal preference to be honest. As far as further optimization i'd suggest taking a look at some form of assembly language. It will give you some general ideas on how to make conditional statements more efficient. That is, it will give you a different out look on things.
if(!empty($_GET['id']))
{
if($_GET['id'] == '13')
{
include('pages/servicestatus.php');
}
else
{
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if (!$rawdata) {
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
} else {
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if (!$rawdata) {
error_404();
}
}
}
else
{
include('pages/home.php');
}
switch would be appropriate if you had several discrete values for $_GET['id'] that you were checking for.
One suggestion I can make for the sake of readability is that
} elseif (!empty($_GET['id'])) {
only needs to be
} else {
Well i don't think it's necessary to switch to a swith
but you could change
} elseif (!empty($_GET['id'])) {
to just
}else{
You may want to look into breaking up your code into a MVC form; that would make it much easier to maintain your code. At least put the last clause into another file, probably called default.php and include it. Also, you might create an array of id => file key/value sets, lookup the id, and include the file.
if (isset($_GET['id'])) {
$pages = array(
0 => 'home.php',
13 => 'servicestatus.php'
);
if (isset($pages[$_GET['id']])) {
include('pages/' . $pages[$_GET['id']]);
} else {
include('pages/default.php');
}
}
Yes, switch is evaluate once, is efficient than if elseif,
and is easier to maintain with this given structure
switch ($_GET['id'])
{
case 13: ... break;
case 0 : ... break;
default: ... break;
}
I dont know, if you should, or should not, but here I wouldnt. The main reason is, that there is at least one statement, you can omit, and then, you will have just a if-elseif-else-Statement
if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
elseif (!empty($_GET['id'])) { /* code* }
is the same as
if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
else { /* code* }
In the block after that, the statement if(!$rawdata) is also duplicated.

Categories