php - if statement with more factors - php

I am trying to make this IF statement to work:
" . ($currentpage == '/adver.php' ? 'active' : '' || $currentpage == 'editad.php' ? 'active' : '' ) . "
What I want to do, is that if the $currentpage is /adver.php or /editad.php, then "active" should be printed out.
The above doesn't work. How can I make the IF statement to function correct?

Use basename() with $_SERVER['PHP_SELF'] to get the current script file name and than compare
if(basename($_SERVER['PHP_SELF']) == 'adver.php' || basename($_SERVER['PHP_SELF']) == 'editad.php') {
echo 'class="active"'; //Whatever you want to echo
}
You can simply create a function(I often use) to get the script file name like
function script_name() {
return basename($_SERVER['PHP_SELF']);
}
Now you can use something like
(script_name() == 'index.php') ? '' : '';
When I use this to echo out the active page I often use something like
function is_active($script_name) {
if(basename($_SERVER['PHP_SELF']) == $script_name) {
return 'class="active"';
}
}
Now you can simply use the above function say in your menu like
<a href="index.php" <?php echo is_active('index.php'); ?>>

I believe you're using the ternary operator incorrectly.
You may want to try the following code:
($currentpage == '/adver.php' || $currentpage == 'editad.php') ? 'active' : ''
Remember that the syntax of the ternary operator is the following:
(comparison) ? (if true) : (if false)
Therefore, the full comparison should go at the beginning. Anyway, consider using a normal if/else when possible, as ternary operators might be confusing both for the writer and the reader (and furthermore, you don't really need the else in this case).
EDIT: I recommend using Mr. Alien's solution.

if($currentpage == "adver.php" || $currentpage == "editad.php") {
echo "Active";
}

If you insist on using the if shorthand, you can try something like:
" . ($currentpage == '/adver.php' ? 'active' : ($currentpage == 'editad.php' ? 'active' : '')) . "
Alternatively, you could something like:
" . (in_array($currentpage, array('/adver.php', 'editad.php')) ? 'active' : '') . "

There is a mistake to use '(' and ')'
Your should use like this
" . ($currentpage == '/adver.php') ? 'active' : '' || ($currentpage == 'editad.php') ? 'active' : '' . "
instead of
" . ($currentpage == '/adver.php' ? 'active' : '' || $currentpage == 'editad.php' ? 'active' : '') . "

Related

What does $_POST['sometext'] == 'on' mean in PHP

I learn PHP and in one of examples I found a part of a code I don't understand.
....
if (array_key_exists('submited', $_POST)) {
for ($i = 1; $i <= $_SESSION['counter']; $i++) {
if (!empty($_POST['checkeditem' . $i]))
if ($_POST['checkeditem' . $i] == 'on') {
$rezultat = mysqli_query($conn, "DELETE FROM gas WHERE id=" . $i);
echo "Checked items are deleted";
}
}
} else {
....
In the code above I don't understand this code line:
$_POST['checkeditem' . $i] == 'on'
There is no attribute with the name='on' in entire code, so it is not related to any name attribute. What is value 'on' and is there other values like that one, that are related to $_POST? Could you suggest me what to google, to find more about this? Thank you.
In the code above I don't understand this code line and here on seems checkbox is checked or not?
$_POST['checkeditem' . $i] == 'on'
will compare like
$_POST['checkeditem1'] == 'on'
$_POST['checkeditem2'] == 'on'
till the last iteration of the loop e.g 100th
$_POST['checkeditem100'] == 'on'

php require minimum 3 words form validation

I have this php code for form validation :
if(($_FILES['file']['error'] != 0) || $title == '' || $tags == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
This form redirect you to a page error if you didn't upload a file, put a title or a tag. From this code I want to change this: $tags == '' to something like this: $tags != 3 but it doesn't work, to require minimum 3 words = 3 tags.
You need to count the words, if they are separated by spaces, use
if(($_FILES['file']['error'] != 0) || $title == '' || str_word_count($tags) < 3) {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
if they are separated by comma-space ,, use this
if(($_FILES['file']['error'] != 0) || $title == '' || count(explode(", ", $tags)) < 3) {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}

PHP if condition with or not working

I have this condition here:
if($name != '' || $name != 0 || $name != "0"){
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
My problem with this condition if $name is equal to "0" it still adds the $where to my $where variable. how do I fix this ?
i think, you mean something else, try use && instead of ||
Well, if this statement is true:
$name is equal to "0"
Then this condition evaluates to true:
$name != ''
Therefore the entire conditional check evaluates to true.
It sounds like you want to use && instead of ||:
if($name != '' && $name != 0 && $name != "0")
That way the entire condition will evaluate to true only if all three conditions are met, instead of only one condition.
The best solution is to use :
if(!empty($name))
Just using this should solve your issue:
if($name){
//your condition
}
try this
if ( !in_array($name, array('','0',0), true ) ) {
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}

?page=index $_GET

For this moment using this code:
if ($_GET['page'] == 'index'
and file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')
or !file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')
or !$_GET['page']) {
//code
} elseif ($_GET['page'] == 'multi'
and file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code 2
}
and so on...
Question 1: Does this code "good" ? Doesn't need any escaping or something ?
Question 2: ?page=logout doens't work, so i created logout.php which looks like:
<?php
require_once "./intl/config.php";
SessionDelete('logged_in');
SessionDelete('username');
SessionDelete('userid');
if ($user_admin != null) {
SessionDelete('inadmin');
if (SessionGet('s_order') != null or SessionGet('s_page_show_all') != null) {
SessionDelete('s_order');
SessionDelete('s_page_show_all');
}
}
header('Location: '.$config['indexurl'].'index.php');
?>
Maybe before sessions delete need session start and it's possible do that with ?page=logout ?
The code certainly can be improved:
Reorder the tests so that it will not generate E_NOTICE errors
Parenthesize so that operator precedence is immediately obvious
Use the && and || boolean operators (as garvey's comment says)
Doing this, you 'd have:
if (empty($_GET['page']) ||
!file_exists('./intl/tpl/' . $_GET['page'] . '.tpl') ||
($_GET['page'] == 'index' && file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code
}
} elseif ($_GET['page'] == 'multi' && file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code 2
}
Then, rewrite it some more to make it clear why you are doing what you do. This will also allow you to write simpler code. Simple is good.
// This way it's obvious that 'index' is the default page
$page = !empty($_GET['page']) ? $_GET['page'] : 'index';
if (!file_exists('./intl/tpl/' . $page . '.tpl')) {
$page = 'index'; // comment here saying that if a non-existing page is requested, we display the index instead
}
$template = './intl/tpl/' . $page . '.tpl';
// At this point, we know that $page has a value, and we know that $template exists, so:
switch($page) {
case 'index':
// code
break;
case 'multi':
// code2
break;
}
As for the second question: yes, you need to start the session before you are able to modify or destroy it.

Correct Ternary Condition for IF ElseIf Condition

Here is my If Else Statement
if(isset($row['content']) && strlen($row['content'])) {
$content = $row['content'];
}
elseif(isset($row['description']) && strlen($row['description'])) {
$content = $row['description'];
}
I tried to create a condition using ternerary operator and ended for with a error: Here is my ternerary condition
$content = isset($row['content']) && strlen($row['content']) ? $row['content'] : isset($row['description']) && strlen($row['description']) ? $row['description'] : '';
What is the correct statement?
You're making your code very very unreadable by changing your condition into a ternary operator. Anyhoo, the following works without an error.
$content = (isset($row['content']) && strlen($row['content']))
? $row['content']
: (isset($row['description']) && strlen($row['description'])
? $row['description']
: '');
Wrapped the last expression in parenthesis so PHP doesn't try to evaluate it separately.
Try putting inside bracket the first term of ?: and the last term of first ?:.
$content = (isset($row['content']) && strlen($row['content'])) ? $row['content'] : ((isset($row['description']) && strlen($row['description'])) ? $row['description'] : '');

Categories