2017-08-09 3 views
0

Ich habe eine products Tabelle, wo ich Produkte aufgelistet habe. Ich möchte ein Stück Code erstellen, der den gleichen Stil, aber unterschiedlichen Preis, Name, Beschreibung, Bild für jedes Produkt zeigt. Ich habe ein Stück Code erstellt, der das macht. Mein Code zeigt nur den ersten in der Zeile an. Ich muss sie alle auflisten. Mein Code ist:PHP - Zeige jedes Produkt aus Tabelle

function grabProducts($con) { 
    ?> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <div class="col-md-12" id="please_wait"> 
     <div class="panel panel-warning"> 
      <div class="panel-heading"> 
       <h3 class="panel-title"><i>Loading, Please Wait...</i></h3> 
      </div> 
      <div class="panel-body"> 

      </div> 
     </div> 
    </div> 
    <script> 
    $(document).ready(function() { 
     $('#please_wait').fadeOut(7000); 
     $('#content_purchase').hide(0).delay(5000).fadeIn("slow"); 
    }); 
    </script> 
    <?php 

    $users = $this->grabUserInfos($con); 
    foreach ($users as $user) { 
     $username = $user[1]; 
     $email = $user[3]; 
    } 

    $site_config = new site_config(); 
    $member_config = new member_config(); 

    $result = mysqli_query($con, "SELECT * FROM products"); 
    $count = mysqli_num_rows($result); 

    // See if there are any products in the database 
    if ($count > 0) { 

     // While loop for each product element 
     while ($row = mysqli_fetch_array($result)) { 
      $id = $row['id']; 
      $name = $row['name']; 
      $description = $row['description']; 
      $picture = $row['picture_location']; 
      $price = $row['price']; 
      $stock_monitering = $row['stock_monitering']; 
      $stock = $row['stock']; 
      $new_stock = $stock - 1; 

      $url_path = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
      $url_path_naked = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH).'/pizza/products.php'; 

      $getValidatedCheck_ = mysqli_query($con, "SELECT txn_id, hasValidated, item_name, amount, currency, payment_date FROM payment_logs WHERE userId = '$username' ORDER BY id DESC LIMIT 1"); 
      $count_success_payment = mysqli_num_rows($getValidatedCheck_); 

      // While loop for our successful payment_logs for each user 
      while ($row = mysqli_fetch_array($getValidatedCheck_)) { 
       $txn_id = $row['txn_id']; 
       $validated_check = $row['hasValidated'];  
       $item_name = $row['item_name']; 
       $item_price = $row['amount']; 
       $item_currency = $row['currency']; 
       $payment_date = $row['payment_date']; 

       $now = strtotime("-10 minutes"); 

       // If there is not enough stock, show out of stock 
       if($stock <= 0) { 
        $display = 'out_of_stock'; 
       } else if($stock > 0 && $now > strtotime($payment_date)) { 
        $display = 'show_products'; 
       } 

       // If there are no transactions for that user show the products like normal 
       if($count_success_payment == 0) { 

        // If they've not already seen the success message, show them it now and update their hasValidated from 0 to 1 so they don't see it again 
        if ($validated_check == '0') { 
         // If stock monitering is on, update our stock to 1 less than we had before the purchase 
         if($stock_monitering == '1') { 
          mysqli_query($con, "UPDATE products SET stock = '$new_stock' WHERE name = '$name'"); 
         } 
         $display = 'show_success'; 
        } 
       } 
      } 

    switch($display) { 
     case "show_products": 
     ?> 
      <div class="col-md-3" id="content_purchase" style="display: none;"> 
       <div class="panel panel-danger"> 
        <div class="panel-heading"> 
         <h3 class="panel-title">Price: <i>$<?php echo $price; ?></i> 
          <div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock 
           (<?php echo $stock; ?>) 
          </div> 
         </h3> 
        </div> 
        <center> 
         <div class="panel-body"> 
          <img src="<?php echo $picture; ?>" 
           style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;"> 
          <form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> 
           <input type="hidden" name="cmd" value="_xclick"> 
           <input type="hidden" name="business" 
             value="<?php $site_config->grabSiteSettings($con, 'paypal_address'); ?>"> 
           <input type="hidden" name="item_name" value="<?php echo $name; ?>"> 
           <input type="hidden" name="item_number" value="<?php echo $id; ?>"> 
           <input type="hidden" name="amount" value="<?php echo $price; ?>"> 
           <input type="hidden" name="quantity" value="1"> 
           <input type="hidden" name="currency_code" value="USD"> 
           <input type="hidden" name="custom" value="username=<? echo $username; ?>&status=<? echo $checkout_status; ?>&product=<? echo $name; ?>"> 
           <input type="hidden" name="notify_url" value="https://benzaofficial.com/pizza/includes/checkout.php"> 
           <input type="hidden" name="return" 
             value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=complete"> 
           <input type="hidden" name="cancel_return" 
             value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=canceled"> 
           <button type="submit" class="btn btn-danger" 
             style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;"> 
            <i class="fa fa-paypal"></i>aypal 
           </button> 
           <form> 
         </div> 
        </center> 
       </div> 
      </div> 
     <?php 
     break; 
     case "show_success": 
     ?> 
     <div class="col-md-12" id="content_purchase" style="display: none;"> 
      <div class="panel panel-success"> 
       <div class="panel-heading"> 
        <h3 class="panel-title"><i>PAYMENT SUCCESS!</i></h3> 
       </div> 
       <div class="panel-body"> 
        <p>You've successfully purchased <i><?php echo $item_name; ?></i> for <i><?php echo $item_price; ?> <i><?php echo $item_currency; ?></i></i> We have emailed you your receipt to <?php echo $email; ?>. You can click <a href="myFiles.php" style="color: white;">here</a> to download your purchased files.<br>Click <a href="<?php echo $url_path; ?>" style="color: white;">here</a> to purchase again.</p> 
       </div> 
      </div> 
     </div> 
     <?php 
     mysqli_query($con, "UPDATE payment_logs SET hasValidated = '1' WHERE userId = '$username' ORDER BY id DESC LIMIT 1"); 
     break; 
     case "no_products": 
     ?> 
     <div class="col-md-12" id="content_purchase" style="display: none;"> 
      <div class="panel panel-danger"> 
       <div class="panel-heading"> 
        <h3 class="panel-title"><i>Currently No Products For Sale</i></h3> 
       </div> 
       <div class="panel-body"> 
        <p>There are currently no products up for sale.</p> 
       </div> 
      </div> 
     </div> 
     <?php 
     break; 
     case "out_of_stock": 
     ?> 
      <div class="col-md-3" id="content_purchase" style="display: none;"> 
       <div class="panel panel-danger"> 
        <div class="panel-heading"> 
         <h3 class="panel-title">Price: <i>$<?php echo $price; ?></i> 
          <div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock 
           (<?php echo $stock; ?>) 
          </div> 
         </h3> 
        </div> 
        <center> 
         <div class="panel-body"> 
          <img src="<?php echo $picture; ?>" 
           style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;"> 
          <p><i><?php echo $name; ?></i> is currently out of stock. Please come back later and try again.</p> 
         </div> 
        </center> 
       </div> 
      </div> 
     <?php 
     break; 
    } 
    } 
    } else { 
     $display = 'no_products'; 
    } 
} 
+0

Ich rate Sie 1), um sich mit den Konzepten der PHP-Template-Engines vertraut zu machen, die erlauben, den PHP-Code vom HTML zu trennen. Sie auf einer Seite zu vermischen, ist eine schlechte Übung (was in den späten 90ern wahrscheinlich ein guter war) 2) sich mit ORM vertraut zu machen, die es erlauben, Geschäftslogik von der Speicher- (oder Datenbank-) Ebene zu trennen. Jetzt mischt man wieder rohen PHP-Code, nicht nur mit HTML, sondern auch mit rohen SQL-Abfragen. Es ist auch eine völlig schlechte Übung, 3) mit PHP-Frameworks, wie Laravel oder Phalcon, in Kontakt zu kommen. – Jacobian

+0

@Jacobian Das ist Off-Thema. Es hat meine Frage in keiner Weise beantwortet. – Benza

+0

Ich habe nicht versucht, Ihre Frage zu beantworten. Ich habe nur versucht darauf hinzuweisen, dass dein Code jetzt wie Spaghetti aussieht und wenn du nicht den guten Praktiken folgst, wirst du auf lange Sicht mit etwas namens BBoM enden. – Jacobian

Antwort

1

müssen Sie zuerst eine Funktion dat alle Ihre Produkte wählen wie diese

function showproducts($conn,$id){ 
    $req="SELECT * FROM products "; 
    $liste=$conn->query($req); 
    return $liste->fetchAll(); 
} 

als Sie diese Funktion aufrufen sollte:

Sie
$list=$cc->showproducts($cc->conn); 

und foreach Reihe kann alle Spalten anzeigen

<?php 
    foreach ($list as $l){ ?> 
    <tr> 
     <td><?php echo $l[0] ;?> </td> 
     <td><?php echo $l[1] ;?> </td> 
     <td><?php echo $l[2] ;?> </td> 
     <td><?php echo $l[3] ;?> </td> 
     <td><?php echo $l[7] ;?> </td> 
    </tr> 
<?php } ?> 
+0

Danke, aber gibt es nicht einen schöneren, weniger Code Weg? – Benza

+0

Mach was @Jacobian in den Kommentaren :) – Borjante

+0

Ich denke, das ist der einfachste Weg, und gut organisierte Code. Ich hoffe, das wird helfen –

Verwandte Themen