Special offer section

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
donboe
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Aug 21, 2018 10:59 pm

Wed Aug 22, 2018 2:11 am

I am working on developing a special offer section for a Paragliding website. The largest part has been done but I'm having problems initiating the process and the order in which certain actions should take place. I hope someone can point me in the right direction. The situation is as follows:

The special offers include, for example, a free day if booked for a certain number of days or a discount on certain paragliding items. The special offers are only shown to visitors who have registered to the mailing list. I have chosen to use Cookies for the entire process. And it works like this.

A visitor to the website is presented with a form and a Cookie is setto prevent the form from being shown time and time again

PHP

Code: Select all

if (!isset($_COOKIE['mailinglist_popup'])): 
include_once "mailinglist_modal.php";
setcookie("mailinglist_popup", 1, time()+1000*24*60*60);
JS

Code: Select all

$(window).scroll(function () { 
	if($(document).scrollTop() > $(document).height()/2){
		$('#mailinglistModal').modal('show');
	} 
});
The registration requires verification. On activating the registration two more cookies are set:

PHP

Code: Select all

setcookie("subscription_activated", 1, time()+1000*24*60*60);
setcookie("offers_seen", 0, time()+1000*24*60*60);
The first cookie (subscription activated) is there to indicate that the registration is completed and the visitor is eligible for the special offers:

The second Cookie (offers_seen) will be used to indicate which special offers the visitor has already seen so that they are not shown again. The visitor has not yet seen any special offers yet, so the value is set to 0

The elseif statement, following the if statement for displaying the mailing list form, checks whether Cookie (subscription activated) exists and whether special offers are available:

PHP

Code: Select all

<? php elseif (isset ($ _ COOKIE ['mailinglist_activated']) && is_array ($ special_offers)):
include_once "offer_modal.php";
?>
JS

Code: Select all

<script>
    $ (window) .scroll (function () {
        if ($ (document) .scrollTop ()> $ (document) .height () / 2) {
            $ ('# offerModal') modal ('show');
        }
    });
</ script>
In other words when Cookie (mailinglist_activated) exists and there is a special offer available, show it.

The variable $special-offer represents the function get_special_offer in the Model class page

Code: Select all

public function get_special_offer($sanitized) {
    $sql    =    "SELECT *
                    FROM `special_offers`
                   WHERE `isActive` = 1
                     AND `offer_id` NOT IN(?)
                ORDER BY `offer_id` DESC
                   LIMIT 1";
					  
    $stmt    =    $this->pdo->prepare($sql);
    $stmt->execute(array($sanitized));
    return $stmt->fetch(PDO::FETCH_ASSOC);		
}
The $sanitized variable is a string of special offer IDs that have already been shown, which is generated within the above elseif statement when a special offer is shown:

Code: Select all

$offerId = $offer_id;
$offers_seen = $_COOKIE['offers_seen'] . "," . $offerId;
setcookie("offers_seen", $offers_seen, time()+1000*24*60*60);
First it is checked which special offer is active. The ID is then added to Cookie (offer_seen) by a comma, giving the Cookie (offers_seen) a new value which is used in the above query (`offer_id` NOT IN). This works fine so far which I can see in the console. And this is the point where I get stucked. I was thinking of passing the get_special_offers() function from within the Main controller (available on all pages) to the View like this:

Code: Select all

protected function render($template, $data = array()){
    $cookiestring         =    urldecode($_COOKIE['offers_seen']);
    $sanitized            =    implode(",", array_map('intval' , explode(",", $cookiestring)));
    $special_offer        =    $this->page->get_special_offer($sanitized);	
					
    $layoutData = array(
        'menu_items'      =>   $this->page->get_main_menu(),
        'special_offer'   =>   $this->page->get_special_offer($sanitized),
        'offer_id'        =>   $special_offer['offer_id'],
        'content'         =>   new View($template.'.php', $data)
    )
    $view = new View($this->layout.'.php', array_merge($layoutData, $data));
    echo $view->render();
}
But that doesn't work at all. First, the Main Controller is one of the first classes called by the auto_loader() function. So immediately errors are generated (Cookie (offers_seen) does not exist etc) and $special_offer, which should only be available when the registration process is finished, should not be there either. So I only get error messages. And not to mention the fact that when there are no special offers at all. This is clearly not the right location for Initiating the special offers section. But I have no idea what could be a better place?

I really hope someone can point me in the right direction. Any help would be highly appreciated

Post Reply