-2

I have similiar problem like I had here: PHP - How to format data for in_array.

I have a comma separated list of URIs in 1 database field. All URIs will be used in an array of excludes like Cookies, URIs, IDs. Excluded Cookies are hardcoded, but not URIs. They come from a DB Query and the result of the query can be multiple, but not more 10 or 20. So how has the output from the query to be formated if there is more than 1 URI in database?

$lsc_exclude_uri_query = xtDBquery("SELECT 
                            lsc.option_value // URIs 
                            FROM lsc_config lsc
                            WHERE lsc.option_id = 9");

while ($exclude_uri_query = xtc_db_fetch_array($lsc_exclude_uri_query)) {
    if (xtc_not_null($exclude_uri_query['option_value'])) {
        $uris = $exclude_uri_query['option_value'];
    }
}


// This is the array of excludes

if (!isset($_COOKIE[$lsc_customer_login]) && !isset($_COOKIE[$lsc_admin]) && !strstr($PHP_SELF, 'result from db query')) {
}
  • What about the good old [explode()](https://www.php.net/manual/en/function.explode.php) function? Beside that: Normalize your database structure. Comma speparated strings in one column are bad design. – Marcel Aug 22 '19 at 07:46
  • Good idea, but doesn't help as much I can generate multiple !strstr($PHP_SELF, 'URI') strings. As I understand explode It generates a new array, so how do you think explode can help? I can't change the design. URIs must be in 1 db field. – Robert Baumann Aug 22 '19 at 07:51
  • Why not using the answer from your other question? FIND_IN_SET fits as answer to your question here pretty well. The explode answer is good as well: Exploding all resultsets in one big array and check against it with in_array(). Simple as pie. – Marcel Aug 22 '19 at 08:26
  • Using FIND_IN_SET from the other answer was my first idea, but can't be used in this task. I don't compare values like in the other answer and I have more than 1 result. The other answer always has only 1 result. – Robert Baumann Aug 22 '19 at 08:37
  • What about dealing with more than one result? You have already a while loop. What about using a flag, which turns true, when a resultset contains the item you are searching for? Have you tried the explode suggestions exploding the found resultsets into one big array and checking against it with in_array() – Marcel Aug 22 '19 at 08:56
  • Yes, working with in_array I already thought about, but once again I don't search for whatever or compare values. There is a list of URIs and if one of each is requested a specific function may not be executed. Maybe my thinking is wrong how to get what I need, so if anybody has better ideas I would be happy about. – Robert Baumann Aug 22 '19 at 09:12

2 Answers2

0

Here comes the answer. @Marcel already solved it, but my brain was blocked because of too much thinking and ignored its help, sorry Marcel :(

$lsc_exclude_uri_query = xtDBquery("SELECT 
                            lsc.option_value 
                            FROM lsc_config lsc
                            WHERE lsc.option_id = 9");

while ($exclude_uri_query = xtc_db_fetch_array($lsc_exclude_uri_query)) {
    if (xtc_not_null($exclude_uri_query['option_value'])) {
        $uris = $exclude_uri_query['option_value'];
    }
}

$urilist = array($uris);
$lsc_customer_login = $lsc_cookie_name;
$lsc_admin = "lsc_admin";
    if (!isset($_COOKIE[$lsc_customer_login]) && !isset($_COOKIE[$lsc_admin]) && !in_array($_SERVER['REQUEST_URI'],$urilist)) {
}
  • Your loop is overwriting the `$uris` variable every time, so it will only contain the value from the last row that isn't null. But you don't have `ORDER BY` in the query, so it's unpredictable which one will be the last row. And why don't you just put `AND option_VALUE IS NOT NULL` in the query, so you don't have to test in the loop? – Barmar Aug 22 '19 at 15:16
  • If the option value is a comma-separated list, use `explode()` to split it up into an array. – Barmar Aug 22 '19 at 15:17
0

Use explode() to split the option value into an array. Then use array_merge() to combine all of them into a single result array.

$urllist = [];
$lsc_exclude_uri_query = xtDBquery("SELECT 
                            lsc.option_value 
                            FROM lsc_config lsc
                            WHERE lsc.option_id = 9 AND lsc.option_value IS NOT NULL");

while ($exclude_uri_query = xtc_db_fetch_array($lsc_exclude_uri_query)) {
    $uris = explode(','. $exclude_uri_query['option_value']);
    $urllist = array_merge($urllist, $uris);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612