problem z nadpisaniem cookie

0

Bardzo wielka prośba o pomoc. Ponieważ ta aplikacja musi do jutra dzialać :/ Problem polega na tym ze w momencie kiedy zmieniam ilosc sztuk w koszyku, rzecz dotyczy malego sklepu, to raz cookie mi sie nadpisuje z iloscia i wyswietla dobra wartosc a raz mi sie nie nadpisuje....
kontroler:

//zmienia ilosc i nadpisuje cookie
 function zmien_ilosc()
 {
    $id= mysql_real_escape_string(HTMLSpecialChars(addslashes($this->uri->segment(3))));
    $ilosc = mysql_real_escape_string(HTMLSpecialChars(addslashes($this->input->post('produkt_'.$id))));
    $shop_count = $this->db->query("select BOOK_Count from books where BOOK_ID = $id")->row()->BOOK_Count;
   

    

    if($ilosc > 0 AND is_numeric($ilosc) AND $ilosc <= $shop_count)
    {
            if(isset($ilosc) and is_numeric($ilosc))
            {
                setcookie("cookie[$id]", "$ilosc", time()+3600*2, '/', '', '0');

              
            }
            redirect('site/order_product');
    }
    else
    {
            echo '<script type="text/javascript">alert("Nie ma tylu produkuktow");</script>';
            echo '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/site/order_product">';
    }
 }
 
 
//wyswietla dane z koszyka
 function order_product()
 {
            $widok['left'] = $this->get_category();
            $widok['right'] = $this->get_bestseller();
            $widok['zakupy'] = $this->sum_all();
            $widok['licznik'] = $this->counter();
            $widok['tytul'] = 'Twoj koszyk';
            $pozycja = 0;

            $widok['center'] ='
            <table class="basket">
              <tr>
              <th>Pozycja</th>
              <th style="text-align: center;">Nazwa</th>
              <th>Ilość</th><th>Cena brutto</th><th>Wartość</th>
              <th>Usuń</th>
              </tr>';

     if (isset($_COOKIE['cookie']))
     {
        foreach ($_COOKIE['cookie'] as $name => $value)
        {
                $pozycja++;
                $query = $this->Kategorie_model->get_books_by_ID($name);
                $widok['center'] .= $this->load->view('Ksiegarnia/order_product_view', array('data' => $query,'pozycja' =>$pozycja,'ilosc'=>$value),True);

        }

     }

     else
     {
          $widok['center'] .= $this->load->view('Ksiegarnia/order_product_view_empty', '',True);

     }
     $widok['center'] .= '</table></br><a href="'.site_url('site/order_details').'">Złóż zamówienie</a>';
    

     $this->load->view('Ksiegarnia/index_logged',$widok);
 }


widok wyswietlajacy dane:

 
<?php foreach($data->result() as $item)
{
echo
form_open('site/zmien_ilosc/'.$item->BOOK_ID).
   '<tr>
    <td>'.$pozycja.'</td>
    <td>'.$item->BOOK_Title.'</td>

    <td><input type ="text" class ="tekst" name="produkt_'.$item->BOOK_ID.'" value="'.$ilosc.'"/>
    <input type="submit" class="sub" value = "Zmien"/>
    </td>';
    if($item->BOOK_IsPromo == 0)
    {

    echo
    '<td>'.number_format($item->BOOK_Price,2, '.', '').'zł</td>
    <td>'.number_format($item->BOOK_Price*$ilosc,2, '.', '').'zł</td><td>'.anchor('koszyk/deleteCards/'.$item->BOOK_ID,'X').'</td>';
    }
    else
    {

    echo
    '<td>'.number_format($item->BOOK_Price-$item->BOOK_PromoValue,2, '.', '').'zł</td>
    <td>'.number_format(($item->BOOK_Price-$item->BOOK_PromoValue)*$ilosc,2, '.', '').'zł</td><td>'.anchor('koszyk/deleteCards/'.$item->BOOK_ID,'X').'</td>';
    }
'</tr>'
.form_close();

}

?>

0

Po 1) "$ilosc" ?? Powinno byc po prostu - $ilosc
Po 2) Nazwa cookie jako "cookie[$id]"? Nie, nie nie. Po prostu zapisuj w cookie tablice PHP!

Przykladowo:

$arr = array($id => $ilosc);
setcookie('foo', serialize($arr));

Do odczytu zawartosci uzyc unserialize():

$arr = unserialize($_COOKIE['foo']);
0

Nie pomoglo, a wrecz przeciwnie, teraz nawet nie moge dodawac danych do koszyka...
funkcja addCards

 
 function addCards($segment=1, $price='')
           {

            $id= mysql_real_escape_string(HTMLSpecialChars(addslashes($segment)));

            $arr = array($id => 1);
            setcookie('foo', serialize($arr));
            //echo 'value2 '.$value2.'- value1 '.$value1.'= value '.$value;
            redirect('site/order_product');
            
          }
 
 function zmien_ilosc()
 {
    $id= mysql_real_escape_string(HTMLSpecialChars(addslashes($this->uri->segment(3))));
    $ilosc = mysql_real_escape_string(HTMLSpecialChars(addslashes($this->input->post('produkt_'.$id))));
    $shop_count = $this->db->query("select BOOK_Count from books where BOOK_ID = $id")->row()->BOOK_Count;
   

    

    if($ilosc > 0 AND is_numeric($ilosc) AND $ilosc <= $shop_count)
    {
            if(isset($ilosc) and is_numeric($ilosc))
            {
                $arr = array($id => $ilosc);
                setcookie('foo', serialize($arr));
               

              
            }
            redirect('site/order_product');
    }
    else
    {
            echo '<script type="text/javascript">alert("Nie ma tylu produkuktow");</script>';
            echo '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/site/order_product">';
    }
 }


 
 function order_product()
 {
            $widok['left'] = $this->get_category();
            $widok['right'] = $this->get_bestseller();
            $widok['zakupy'] = $this->sum_all();
            $widok['licznik'] = $this->counter();
            $widok['tytul'] = 'Twoj koszyk';
            $pozycja = 0;

            $widok['center'] ='
            <table class="basket">
              <tr>
              <th>Pozycja</th>
              <th style="text-align: center;">Nazwa</th>
              <th>Ilość</th><th>Cena brutto</th><th>Wartość</th>
              <th>Usuń</th>
              </tr>';

     if (isset($_COOKIE['cookie']))
     {
         $arr = unserialize($_COOKIE['foo']);
        foreach ($arr as $name => $value)
        {
                $pozycja++;
                $query = $this->Kategorie_model->get_books_by_ID($name);
                $widok['center'] .= $this->load->view('Ksiegarnia/order_product_view', array('data' => $query,'pozycja' =>$pozycja,'ilosc'=>$value),True);

        }

     }

     else
     {
          $widok['center'] .= $this->load->view('Ksiegarnia/order_product_view_empty', '',True);

     }
     $widok['center'] .= '</table></br><a href="'.site_url('site/order_details').'">Złóż zamówienie</a>';
    

     $this->load->view('Ksiegarnia/index_logged',$widok);
 }




0

chyba to jest wina indeksu przy cookie :D

jednak nie...:/pomocy!

wybaczcie ze tak spamuje. Ale doszedlem do tego ze to nie jest wina cookie ale formularza. Kiedy zaraz na początku funkcji zmien_ilosc wywolam echo $ilosc to wyswietla zmienna z pierwszeej iteracji funkcji foreach w widoku order_product_view. Czy jest jakis spoó zeby to naprawic???

1 użytkowników online, w tym zalogowanych: 0, gości: 1