Jump to content

Subscribing a user to a category


TechnoDiver

Recommended Posts

Hi Freaks, I've been trying to put together subscribe functionality on a project. Been at it for hours and can't make any progress.

The categories that a user is subbed to are stored in comma separated string in the database and pulled out into $_SESSION['user_subs']

I have these 2 methods here to start ->

<?php
private function userSubscribed($cat_id) {
    $category = $this->getCategoryByID($cat_id);
    if(in_array($category, $_SESSION['user_subs'])) { 
        echo "user subscribed";
        return true;
    } else {  
        echo "user NOT subscribed"; 
        return false;       
    }
}

public function displaySubscribedButton($cat_id) {
    $subscribed = $this->userSubscribed($cat_id);
    if($subscribed) {
        $action_btn = "<td><a href='category_posts.php?cat_id=$cat_id' name='sub_btn' class='btn btn-secondary'>Unsubscribe</a>";
    } else {
        $action_btn = "<td><a href='category_posts.php?cat_id=$cat_id' name='sub_btn' class='btn btn-danger text-color-									primary'>Subscribe</a>";
    }

    return $action_btn;
}

They've both been well tested and I know they're working correctly.

Then I have this one (which is the problem child) ->

<?php
public function updateSubscriptionCategory($cat_id, $username) {
    $category = $this->getCategoryByID($cat_id);
    $subscribed = $this->userSubscribed($cat_id);

    print_r($_SESSION['user_subs']);

    if($subscribed){
        unset($_SESSION['user_subs'][$category]);
        $subs = $_SESSION['user_subs'];
        $subscribed = false;
    } else {
        $new_sub = ",".$category;
        $subs = array_push($_SESSION['user_subs'], $new_sub);
        $subscribed = true;
    }

    $stmt = $this->conn->prepare("UPDATE ".User::$table." SET subs=? WHERE username=?");

    if($stmt) {
        $stmt->execute([$subs, $username]);
    } else {
        echo "you fucked up";
    }
}

I call it from the category file, here ->

<?php require_once("assets/initializations.php"); 
$post_obj = new Post($conn, $username);
$cat_obj = new Category($conn, $username);

$cat_id = $_GET['cat_id'];
if(isset($_POST['sub_btn'])) {
    $cat_obj->updateSubscriptionCategory($cat_id, $username);
    // header("Location: category_posts.php?cat_id=$cat_id&cat_title=$cat_title");
}

but the page only reloads no changes are made not on the page nor in the db nor do I get any feedback, not from the 'print_r' or the else at the bottom. I feel like it's not even running this method. Was hoping that someone sees something that I've been missing. 8 hours in and I'm still not able to get it to work. TIA for all input

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.