Welcome Guest, Not a member yet? Register   Sign In
load view, refresh current page
#1

[eluser]felixk[/eluser]
Hey guys!
I'm coding a ecommerce and when I'm clicking "buy", I want the product to add to the shopping cart, obviously, and at the same time stay on the same page.

I'm using this code:
Code:
function buy()
    {    
        $data['title'] = "GDI/Kundvagn - Webbshop | Hästtillbehör, båtlås, transportband";
        $data['query_cat'] = $this->categoryModel->getAllCategories();
        $data['myProduct'] = $this->productsModel->getProduct($this->uri->segment(3));
        $data['error'] = '';
        //Kundvagnen
        if(!isset($_SESSION['cart']))
        {
          $_SESSION['cart'] = array();
        }
        $id = $this->uri->segment(3);
        //Om buy-sidan har ett ID:
        if(strlen($id) > 0)
        {
            //Om ID är en siffra
            if(ctype_digit($id))
            {
                //Kolla om produkten finns
                if(count($this->productsModel->getProduct($id)) > 0)
                {
                    if(!array_key_exists($id,$_SESSION['cart']))
                    {
                        $_SESSION['cart'][$id]=0;
                    }
                    $_SESSION['cart'][$id]++;
                }
                // Om produkten inte finns, felmeddelande...
                else { $data['error'] = "Felmeddelande: Felaktigt id för produkten."; }
                
            } //Om ID är en siffra --> SLUT
            else { $data['error'] = "Felmeddelande: Felaktigt id för produkten."; }
        } //Om det finns något ID --> SLUT

                // HERE IS THE PROBLEM, WHAT VIEW SHOULD I LOAD?
                // IS THERE SOME KIND OF "REFRESH" SNIPPET I COULD USE?
        $this->load->view('WHAT VIEW??' ,$data);
    }
Take a look at the 4th row from the bottom, my comments.
The link is something like this: "www.example.com/gdi/prod/3"
#2

[eluser]xzela[/eluser]
yes, you can use this to redirect/refresh the page:

Code:
redirect('gdi/prod/3', 'refresh');

this will tell the page to refresh
#3

[eluser]felixk[/eluser]
Ye, but the thing is that I don't know the URI, because i can add a product from different pages. I tried "uri_string()" but the URI after I've pressed "buy" is "gdi/buy/3" and that will just be an infinite loop.

To sum up: I want to go back to the page I was on when i pressed buy!
#4

[eluser]felixk[/eluser]
A javascript "history go back" would fit perfectly, but it doesn't work in the controller?
#5

[eluser]Andreas Bergström[/eluser]
There are endless ways to solve this... For example $_SERVER['HTTP_REFERER']. Or just include the data you need in the buy-link.

Snyggt med svenska kommentarer där. =P
#6

[eluser]felixk[/eluser]
Haha! En svensk, trevligt! Jag löste det med sessions istället, kanske inte bästa lösningen men funkar för mig.
Tackar, Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB