Jump to content
dedi_naber

Help Getting value in PHP

Recommended Posts

Hi,

Could you please help me about my little code blocks. I am new on PHP. 
There one tool on bootstrap which is select2 I am showing the products from database via this "select2" and after the submitting button I want to show the products which are selected in the below form.
I can list on this form the products but I can not show the total price of the selected products. It is showing all products of sum not only selected . Also I am adding a picture of how it looks. l Could you please help me to solve this problem.

 

Thanks

 

<div class="layout-content">
                  <!-- [ content ] Start -->
                  <div class="container-fluid flex-grow-1 container-p-y">
                     <h4 class="font-weight-bold py-3 mb-0">Teklif Oluştur</h4>
                     <div class="text-muted small mt-0 mb-4 d-block breadcrumb">
                        <ol class="breadcrumb">
                           <li class="breadcrumb-item"><a href="#"><i class="feather icon-home"></i></a></li>
                           <li class="breadcrumb-item">Forms</li>
                           <li class="breadcrumb-item active">Teklif Oluştur</li>
                        </ol>
                     </div>
                     <div class="card mb-4">
                        <h6 class="card-header">Teklif Oluştur</h6>
                        <div class="card-body demo-vertical-spacing">
                           <form action="select.php" method="POST">
                              <!-- Multiple -->
                              <?php  
                                 //$urun_id=$uruncek['urun_id']; 
                                 
                                 $kategorisor=$db->prepare("select * from urun2 ");
                                 $kategorisor->execute();
                                 
                                   ?>
                              <select class="select2-demo form-control" multiple style="width: 100%" name="course[]" multiple="multiple">
                                 <?php 
                                    while($kategoricek=$kategorisor->fetch(PDO::FETCH_ASSOC)) {
                                    
                                      $kategori_id=$kategoricek['urun_id'];
                                    $course = $_POST['course'];
                                    
                                      ?>			  
                                 <option value="<?php echo $kategoricek['urun_id']; ?>"><?php echo $kategoricek['urun_ad']; ?></option>
                                 <?php } ?>
                              </select><br>
							  <button type="submit" name="send" class="btn btn-primary">Ürün Ekle</button>
							  <br><br>
							  <p><strong>Not:</strong> Teklife eklemek istediğiniz ürünü listeden seçerek ekleyiniz.</p>
                              
                           </form>
                        </div>
                     </div>
                  </div>
                  <!-- [ content ] End -->
                  <div class="container-fluid flex-grow-1 container-p-y">
                     <h4 class="font-weight-bold py-3 mb-0">Teklif</h4>
                     <div class="text-muted small mt-0 mb-4 d-block breadcrumb">
                        <ol class="breadcrumb">
                           <li class="breadcrumb-item"><a href="#"><i class="feather icon-home"></i></a></li>
                           <li class="breadcrumb-item active">Teklif</li>
                        </ol>
                     </div>
                     <div class="card">
                        <div class="card-body p-5">
                           <div class="row">
                              <div class="col-sm-6 pb-4">
                                 <div class="media align-items-center mb-4">
                                    <a href="index-2.html" class="navbar-brand app-brand demo py-0 mr-4">
                                    <span class="app-brand-logo demo">
                                    <img src="#" alt="Şirket Logo" class="img-fluid">
                                    </span>
                                    <span class="app-brand-text demo font-weight-bold text-dark ml-2">Şirket İsmi</span>
                                    </a>
                                 </div>
                                 <div class="mb-1">Adres Satırı</div>
                                 <div class="mb-1">Adres Satırı1..</div>
                                 <div>Telefon Bilgileri</div>
                              </div>
                              <div class="col-sm-6 text-right pb-4">
                                 <h6 class="text-big text-large font-weight-bold mb-3">TEKLİFNO: <input style="border:none" type="text" placeholder="TEKLİFNO giriniz."></h6>
                                 <div class="mb-1">Tarih:
                                    <strong class="font-weight-semibold"><?php
                                       date_default_timezone_set('Etc/GMT-3');
                                       echo date("d.m.Y");
                                       ?></strong>
                                 </div>
                                 <div>Geçerlilik Tarihi:
                                    <strong class="font-weight-semibold"><?php
                                       date_default_timezone_set('Etc/GMT-3');
                                       echo date('d.m.Y',strtotime('+15 days'));
                                       ?></strong>
                                 </div>
                              </div>
                           </div>
                           <hr class="mb-4">
                           <div class="row">
                              <div class="col-sm-6 mb-4">
                                 <div class="font-weight-bold mb-2">Teklifi Alan:</div>
                                 <div>Alice Robart</div>
                                 <div>Company LTD</div>
                                 <div>09 Bowman St.</div>
                                 <div>South Windsor, CT 06074</div>
                                 <div>United States</div>
                                 <div>+91 (999) 999 9999</div>
                                 <div>dummy@mail.com</div>
                              </div>
                              <div class="col-sm-6 mb-4">
                                 <div class="font-weight-bold mb-2">Payment Details:</div>
                                 <table>
                                    <tbody>
                                       <tr>
                                          <td class="pr-3">Toplam Fiyat:</td>
                                          <td>
                                             <strong>$6,095.25</strong>
                                          </td>
                                       </tr>
                                       <tr>
                                          <td class="pr-3">Banka İsmi:</td>
                                          <td><input type="text" placeholder="Banka İsmi giriniz." style="border:none"></td>
                                       </tr>
                                       
									   <tr>
                                          <td class="pr-3">Address:</td>
                                          <td><input type="text" placeholder="Adresi giriniz." style="border:none"></td>
                                       </tr>
                                       <tr>
                                          <td class="pr-3">Şehir:</td>
                                          <td><input type="text" placeholder="Şehir giriniz." style="border:none"></td>
                                       </tr>
                                       <tr>
                                          <td class="pr-3">Ülke:</td>
                                          <td><input type="text" placeholder="Şehir giriniz." style="border:none"></td>
                                       </tr>
                                       <tr>
                                          <td class="pr-3">IBAN:</td>
                                          <td><input type="text" placeholder="IBAN giriniz." style="border:none"></td>
                                       </tr>
                                       <tr>
                                          <td class="pr-3">SWIFT code:</td>
                                          <td><input type="text" placeholder="SWIFT Code giriniz." style="border:none"></td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </div>
                           </div>
                           <div class="table-responsive mb-4">
                              <table class="table m-0">
                                 <thead>
                                    <tr>
                                       <th class="py-3">
                                          Task description
                                       </th>
                                       <th class="py-3">
                                          Nakit Fiyat
                                       </th>
                                       <th class="py-3">
                                          Kredi Kartı Tek Çekim Fiyatı
                                       </th>
                                       <th class="py-3">
                                          Kredi Kartı Taksitli Fiyatı
                                       </th>
                                       <th class="py-3">
                                          Senet Fiyatı
                                       </th>
                                    </tr>
                                 </thead>
                                 <tbody>
                                    <?
                                       if($course){
                                        foreach ($course as $c){
                                       
                                       $urunsor=$db->prepare("SELECT * FROM urun2 where urun_id=:ad");
                                       
                                       $urunsor->execute(array(
                                                           'ad' => $c
                                                           ));
                                       $toplam = $uruncek['SUM(urun_fiyat)'];
                                       
                                        while($uruncek=$urunsor->fetch(PDO::FETCH_ASSOC)) { ?>
                                    <tr>
                                       <td class="py-3">
                                          <div class="font-weight-semibold"><?php echo $uruncek['urun_ad'] ?></div>
                                          <div class="text-muted"><?php echo $uruncek['urun_seriNo'] ?></div>
                                       </td>
                                       <td class="py-3">
                                          <strong><?php echo $uruncek['urun_fiyat'] ?> <i class="fas fa-lira-sign"></i></strong>
                                       </td>
                                       <td class="py-3">
                                          <strong><?php echo $uruncek['urun_KKfiyat'] ?> <i class="fas fa-lira-sign"></i></strong>
                                       </td>
                                       <td class="py-3">
                                          <strong><?php echo $uruncek['urun_KKTaksitfiyat'] ?> <i class="fas fa-lira-sign"></i></strong>
                                       </td>
                                       <td class="py-3">
                                          <strong><?php echo $uruncek['urun_senetfiyat'] ?> <i class="fas fa-lira-sign"></i></strong>
                                       </td>
                                    </tr>
                                    <?php }}} ?>
                                    <tr>
                                       <td colspan="4" class="text-right py-3">
                                          <span class="d-block text-big mt-2">TOPLAM:</span>
                                          Nakit Fiyat:<br>
                                          Kredi Kartı Fiyatı:<br>
                                          Kredi Kartı Taksitli Fiyatı:<br>
                                          Senet Fiyatı:
                                       </td>
                                       <td class="py-4">
                                          <strong>: </strong><br>
                                          <strong> 15151515 <i class="fas fa-lira-sign"></i></strong><br>
                                          <strong> 1,219.05 <i class="fas fa-lira-sign"></i></strong><br>
                                          <strong> 1,219.05 <i class="fas fa-lira-sign"></i></strong><br>
                                          <strong> 6,095.25 <i class="fas fa-lira-sign"></i></strong>
                                       </td>
                                    </tr>
                                 </tbody>
                              </table>
                           </div>
                           <div class="text-muted">
                              <strong>Note:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras laoreet, dolor id dapibus dapibus, neque mi tincidunt quam, quis congue ligula risus vitae magna. Curabitur ultrices nisi massa,
                              nec viverra lorem feugiat sed.
                              Mauris non porttitor nunc. Integer eu orci in magna auctor vestibulum.
                           </div>
                        </div>
                        <div class="card-footer text-right">
                           <a href="print.php" target="_blank" class="btn btn-default"><i class="ion ion-md-print"></i>&nbsp; Print</a>
                           <button type="button" class="btn btn-primary ml-2"><i class="ion ion-ios-paper-plane"></i>&nbsp; Send</button>
                        </div>
                     </div>
                  </div>
                  
               </div>

k1JicI.jpg

Share this post


Link to post
Share on other sites

I only see select2 as an HTML ID. I don't see anywhere that you are collecting the sum of the cost for selected items. Unless I am misunderstanding your question, you need to have a variable where you add the cost of each selected item.

Share this post


Link to post
Share on other sites
1 hour ago, gw1500se said:

I only see select2 as an HTML ID. I don't see anywhere that you are collecting the sum of the cost for selected items. Unless I am misunderstanding your question, you need to have a variable where you add the cost of each selected item.

so could you please help me to solve  ?

Share this post


Link to post
Share on other sites

For future reference:

It is much much easier to work with a script that puts all of its php logic in one area of the script and the majority of the html and JS and CSS in another area of the script.  Mixing php and html code all together when it is entirely unnecessary makes for very difficult debugging later on, as you have done here.

Good luck with the current problem but think about your coding style next time you begin a script.  Start with the session being turned on, then static values that need to be set for the script then handle the inputs and then do the logic that needs to be done and finally do your output of your html.  Be sure to save your php results as php vars and bury them inside the html output places rather than burying php code inside your html.  

Of course sometimes you will have to embed html code inside php logic such as when building an html table or a dropdown list but the total output of such an operation can be saved as a single php var that you then place in the appropriate spot of the html output area at the end of the script.  That makes it so much easier to read thru the php logic to debug things.  In this script - I for one am not going to dig thru all of you divs and li and other tags  just to find something wrong.  

Share this post


Link to post
Share on other sites

As ginerjm suggests, rewrite your page to segment it. Then you will find it much easier to debug and perhaps you won't even need any help from us. If you do, post your better organized code.

  • Like 1

Share this post


Link to post
Share on other sites

web servers are stateless. they don't know or care what happens outside of the current request. to do what you are trying, a quote/proposal/shopping cart, you will need to provide a means of propagating the selected item information between page requests. one method of doing this, that is straightforward to implement, is to store the cart information in a session based array variable.

the work-flow would be -

  1. display products with a means of adding them to the cart.
  2. when the 'add to cart' form is submitted, add the item id(s) and quantity(ies) to the session based cart. if you use the item id as the cart's array index and the quantity as the array value, you will end up with the simplest code.
  3. to display the contents of the cart, retrieve the item ids from the session based cart (see the array_keys() function), query the database table holding the item information to get the name, description, price, ... of the items in the cart, loop over the result from the query to produce the output, getting the corresponding quantity from the session based cart to calculate the sub total for each item, add the item sub total to a running total variable, then finally display the total price.

when the quote/proposal/shopping cart is finalized and converted into an order, you will need to store the contents of the session based cart in a database. you would need (at least) two tables to store the information. the 1st table - 'orders', would hold the unique/one-time order information. this will produce an order_id (auto-increment integer primary index). the 2nd table - 'order_items', would hold rows containing the item id and quantity information making up the order, related back to the order they belong to through the order_id value.

Share this post


Link to post
Share on other sites

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.