PHP Winkelwagen

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Kevin Ruhl

Kevin Ruhl

07/08/2013 21:35:40
Quote Anchor link
(Voor de link naar de code, zie benenden)

Hallo,

Ik ben bezig met het bouwen van een webshop, maar ik heb een probleem met mijn winkelwagentje.
Ik heb geprobeerd om de producten in de winkelwagen te zetten/onthouden, met session's en array's, maar blijkbaar zit er ergens een fout.

Ik had dus de vraag, wat kan ik veranderen aan mijn script? Of moet ik een hele andere methode hiervoor gebruiken?

Alvast bedankt.

Code hieronder:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Product pagina:
<input name="art_amount" type="text" value="1"/>
<input type="hidden" name="art_id" value="<?=$info[0]['productid'];?>" />
<input type="hidden" name="articlenr"  value="<?=$info[0]['article_number'];?>" />
<input type="hidden" name="page_id" value="<?=$info[0]['id'];?>" />
<input type="hidden" name="art_price" value="<?=$info[0]['price'];?>" />
<input type="hidden" name="art_shippingcosts" value="<?=$info[0]['shipping_costs'];?>" />
<input type="hidden" name="current_url" value="<?=$_SERVER['REQUEST_URI'];?>" />
<input type="hidden" name="art_name" value="<?=$info[0]['title'];?>" />
<input type="hidden" name="tax" value="21" />
<input type="hidden" name="user_cmd" value="add_art" />

Addproduct functie:
<?php
function add_article()
{

      global $settings;
      $errmessg='';
      if(isset($_POST['is_offerte']) && $_POST['is_offerte'] == '1'){
        $_SESSION['is_offerte']=1;
      }
else{
        $_SESSION['is_offerte']=0;
      }



      $article['artid']=(int)$_POST['art_id'];
      $tax=$_POST['tax'];
      $query="SELECT `price`,`discount` FROM `producten` WHERE `id`='".$article['artid']."' LIMIT 0,1";
      $sql=mysql_query($query);
      if(!$sql && mysql_num_rows($sql) != 1){
          $errmessg.='Geen product gevonden!<br />';
        }
else{
          $row_product=mysql_fetch_assoc($sql);
        }


      $art_url=id2url($article['artid']);

      $discount=$row_product['discount'];
      $art_price=(float)$row_product['price'];

      if($discount > 0){
          $art_price2 = $art_price - $discount;
          $art_price = number_format($art_price2 + ($art_price2 / 100) * $tax, 2, ',', '');
      }
    

      if(!$art_price){
        $errmessg.='<div class="contentcontent">Ongeldige prijs!</div>';
      }
else{
        $article['price']=$art_price;
      }


      $article['name']=$_POST['art_name'];
      $article['articlenr']=$_POST['articlenr'];
      if(isset($_POST['art_shippingcosts'])){
        $article['shippingcosts']=$_POST['art_shippingcosts'];
      }
else{
        $article['shippingcosts']=-1;
      }


      if(isset($_POST['art_amount'])){
        $article['amount']=(int)$_POST['art_amount'];
      }
else{
        $article['amount']=1;
      }

      $article['page_id']=(int)$_POST['page_id'];
      if($article['amount'] < 1){
        $article['amount']=1;
      }



      if(isset($_SESSION['articles'])){
        $articles=$_SESSION['articles'];
      }
else{
        $articles=array();
      }


      if($errmessg == ''){
        foreach($articles as $artsessid=>$article_line){

            if($article_line['artid'] == $article['artid']){
                $article['amount']=$article['amount']+$article_line['amount'];
                unset($articles[$artsessid]);
            }

        }


        if ($article['name'] != '' && $article['amount'] >0){
                $articles['art_'.uniqid()]=$article;
                }

      }


      if($errmessg){
        echo '<div style="width:100%">'.$errmessg.'</div>';
        return false;
      }
else{
        $_SESSION['articles']=$articles;
      }

  return true;
}

?>


Listproduct functie:
<?php
function list_articles()
{

    global $url, $settings;
    $is_included='list_articles';
    $discount=0;
    if(isset($_POST['current_url'])){
        if($_POST['current_url'] != '')
        {

            echo '<a class="terugwebshoplink" href="'.$_POST['current_url'].'">Terug naar webshop</a><br />';
        }
    }

    if(isset($_SESSION['articles'])){
      $articles=$_SESSION['articles'];
    }
else{
      $articles=array();
    }

    foreach($_POST as $waarde=>$inhoud){
        if(substr($waarde,0,4) == 'del_' && substr($waarde,-2) == '_x') {
            $this->del_article(substr($waarde,4,-2));
            $articles=$_SESSION['articles'];
        }
    }

    
    $subtotaal=0;
    foreach($articles as $name=>$line){
            if (isset($_POST['number_'.$name]))
            {

                if($_POST['number_'.$name] != $line['amount'])
                {

                    $line['amount']=$_POST['number_'.$name] ;
                    $articles[$name]=$line;
                    $_SESSION['articles']=$articles;
                }
            }

            $bedrag=$line['price'];
            $subtotaal=$subtotaal + ($bedrag * $line['amount']);
        }

    if(!SHOP_BTW_INCL){
        
      $tax=($subtotaal / 100) * $settings['tax'];
      $totaal=($subtotaal - $line['discount']) + $tax;
    }
else{
      $tax=($subtotaal / 100) * $settings['tax'];
      $totaal=($subtotaal - $line['discount']) + $tax;
    }


    if (count($articles) >0){
      include('classes_files/list_articles.php');
    }
else{
      echo 'Uw winkelwagen is leeg op het moment<br />';
    }
}

?>


Url2id funtie:
<?php
function id2url($paginaid){
    $i=0;
    $zoek_url='';
    while ($paginaid != 0 && $i<100) {
        $i++;
        $query1 = "SELECT `url`, `id`, `volg` FROM `pagina` WHERE `id` = '".$paginaid."' LIMIT 0,1";
        $result1 = mysql_query($query1) or die (mysql_error());
        while($row1 = mysql_fetch_array($result1)) {
            $zoek_url = $row1["url"] . "/" . $zoek_url;
            $paginaid = $row1["volg"];
        }
    }

    return $zoek_url;
}

?>
Gewijzigd op 07/08/2013 21:44:59 door Kevin Ruhl
 
PHP hulp

PHP hulp

19/05/2024 09:02:01
 
- Ariën  -
Beheerder

- Ariën -

07/08/2013 21:43:14
Quote Anchor link
Kan je de relevante code in dit topic plaatsen tussen de [code][/code]-tags?
Straks gaat de pastebin-inhoud verloren en heeft dit topic geen nut meer.
 
Kevin Ruhl

Kevin Ruhl

07/08/2013 21:45:18
Quote Anchor link
Bewerkt :).
 
Obelix Idefix

Obelix Idefix

07/08/2013 21:47:48
Quote Anchor link
Ik zie in je code nergens session_start staan. Nodig om met sessies te werken.
 
Kevin Ruhl

Kevin Ruhl

07/08/2013 21:48:30
Quote Anchor link
OK, vandaar, ik ga het even proberen :).

Toevoeging op 07/08/2013 21:51:46:

Obelix en Idefix op 07/08/2013 21:47:48:
Ik zie in je code nergens session_start staan. Nodig om met sessies te werken.


Ja, het werkt :D. Bedankt voor de tip, ik dacht altijd dat session_start(); een andere functie had, blijkbaar niet dus :).

Toevoeging op 07/08/2013 21:52:51:

Is er trouwens een manier om aan te geven dat mijn probleem is opgelost? Of moet ik dit topic gewoon laten voor wat het is?
 
- Ariën  -
Beheerder

- Ariën -

07/08/2013 21:54:27
Quote Anchor link
Nee, zo'n functie kennen we (nog?) niet.
Het topic zal vanzelf wel richting naar beneden zinken. :-)
 
Kevin Ruhl

Kevin Ruhl

07/08/2013 21:55:19
Quote Anchor link
OK :).
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.