简体   繁体   中英

global settings php function

I'm busy developing a website where a blog is included. Now I'd like to add a dev_mode check. If I'm editing the article page, the check should return 'true' so I can publish a message like 'This page is currently being edited. Please come back later'. I want just ONE function for all the pages, but it's givin me no luck..

My current function:

function development_mode($mysqli, $dev_blog=false, $dev_article=false, $dev_cv=false, $dev_contact=false, $dev_bio=false, $dev_cpanel=false, $get_development_status=false) {
    $get_development_status = mysqli_query($mysqli,"SELECT * FROM global_settings") or die (mysqli_error($mysqli));
    $status = mysqli_fetch_array($get_development_status);
    $dev_blog = $status['dev_blog'];
    $dev_article = $status['dev_article'];
    $dev_cv = $status['dev_cv'];
    $dev_contact = $status['dev_contact'];
    $dev_bio = $status['dev_bio'];
    $dev_cpanel = $status['dev_cpanel'];

    if ($dev_blog == 'yes') {
        return true;
    } else {
        return false;
    }

    if ($dev_article == 'yes') {
        return true;
    } else {
        return false;
    }

    if ($dev_cv == 'yes') {
        return true;
    } else {
        return false;
    }

    if ($dev_contact == 'yes') {
        return true;
    } else {
        return false;
    }

    if ($dev_bio == 'yes') {
        return true;
    } else {
        return false;
    }

    if ($dev_cpanel == 'yes') {
        return true;
    } else {
        return false;
    }
}

This is how I'd like to show the visitor the 'come back later' message when the article page is being edited

if (development_mode($mysqli, $dev_article) == true) {
    echo "This page is being edited. Come back later!";
    die();
} else {
//Content
}

Thank you :)

It won't work as expected for settings besides dev_blog because it does not take into consideration the value you pass to it as parameter, in your example $dev_article .

No matter what you try to pass to it, the page will ALWAYS execute the first if

if ($dev_blog == 'yes') {
    return true;
} else {
    return false;
}

and return true or false depending on the setting dev_blog column returned by the query.

As is, execution will NEVER continue to check $dev_article , $dev_contact , $dev_bio or $dev_cpanel no matter what their values are or what parameters you try to pass to the development_mode() function.


Solution:

One way to check correctly would be to try something like below, calling the function using a string index of the columns:

 if (development_mode($mysqli, 'dev_article') == true) {

and then in your function have:

function development_mode($mysqli, $status_column) {
    $get_development_status = mysqli_query($mysqli,"SELECT * FROM global_settings") or die (mysqli_error($mysqli));
    $status = mysqli_fetch_array($get_development_status);

    $dev_status = $status[$status_column];

    if ($dev_status == 'yes') {
        return true;
    } else {
        return false;
    }
}

this could be even further optimized to return only the needed column since you only care about that one, but you get the point of what is different between the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM