Hey guys!

I am trying to learn how to use the implode and explode function and in one of my columns called subscriptionplan, I have the following information: Level 1, Level 2, Level 3. Can I do something like this for an if statement because it doesn't work for the Level 2 one..

$subscriptionplan = explode(',',$subscriptionplan);
foreach($subscriptionplan as $subscribe) {
if ($subscribe != 'Level 1') {
header("Location: index.php?level1=notexists");
exit();
} else {
header("Location: level1videos.php");
exit();
}
}

gmgiadinh176

Welcome to the forums. Note that I wrapped your code snippet in this forum's [code]...[/code] tags, which maintains formatting like you'd see in a code editor. Please be sure to use them in subsequent posts. 🙂

    If I'm correctly guessing what you want to do, you may want to invert the logic a bit, something like:

    $subscriptionplan = explode(',', $subscriptionplan);
    foreach ($subscriptionplan as $subscribe) {
      if ($subscribe == 'Level 1') {
        header("Location: level1videos.php");
        exit();
      }
    }
    // if we get this far, do the other redirect
    header("Location: index.php?level1=notexists");
    exit();
    

    That being said, probably the more robust and efficient solution would be to normalize your data and not store multiple values in one DB field. You might use a one-to-many relationship to a separate "subscription plan" table, which would probably be the easiest way to make things easy to change (e.g. if you add new plan types).

      Your real problem as noted by @NogDog is that you are storing multiple values in a single column. Do not do that. Normalize your DB structure and go from there.

        Another option (if you do not normalize the data) is to just use array_search() instead of a loop:

        $test = 'Level 1,Level 2,Level 3';
        $levels = explode(',', $test);
        if(array_search('Level 1', $levels) !== false) {
            // do the redirect if Level1 found
        } else {
            // do the not found redirect
        }
        

          gmgiadinh176
          Once you have the data as an array (hopefully by storing it properly and fetching it from the query into an array), just use in_array() or !in_array() (not in_array()) to determine if a value exists or doesn't exist. No loop is necessary.

          Next, you must test on each page request if the current visitor has permission to see or perform any action on the current page. What you are currently doing isn't secure. If someone navigates directly to level1videos.php you must test in the code on that page if they have permission to access that page. Rather than to redirect around on your site, just produce navigation links and the content on each page based on what permissions the current visitor has.

            Write a Reply...