2016-04-10 10 views
-1

Bir sınıf projesi uğruna mock-up mağazası oluşturmaya çalışıyorum. Benim araçlarım XAMPP 3.2.2 ve phpMyAdmin'dir. Gerçek, işlevsel bir "Sepete ekle" düğmesi daha sonra gelecektir (Şu anda sadece bir yer tutucu olarak bir bağlantı kullanıyorum), fakat şimdilik düğme/bağlantıyı nasıl değiştireceğimi anlamaya çalışıyorum. Stok 0 olduğunda, "Stoklar tükendi" mesajı görüntülenir."Stokta Yok" bağlantısını "Stokta Kalmadı" ile değiştirme

Ürün sayfasını görüntülemek için kullanıyorum kod İşte; hemen hemen kesinlikle dağınık ve orada bunu yapmak için bir düzine iyi yollar muhtemelen, ama şimdi, bu iş bitmiş olur:

<?php 
$sql = "SELECT * FROM webstore.Products order by category"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 

     echo '<table border="2" width="100%">'; 
     echo '<tr>'; 
        echo ' <td width="20%">' . $row["Name"] . '</td>'; 
        echo ' <td width="20%">' . $row["Descr"] . '</td>'; 
        echo ' <td width="20%">' . $row["Price"] . '</td>'; 
        echo ' <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>'; 
        echo ' <td width="20%">' . '<a href ="http://localhost:81/shopping_cart.php">Add to cart</a>' . '</td>'; 
        echo ' </tr> '; 
        echo '</table>'; 
} 
} else { 
echo "0 results"; 
} 

çıktı şuna benzer: Results

Bakmıyorum "daha iyi uygulamalar" için Şu anda, sadece ürün stok 0.

+0

stok olduğunu webstore.Products miktarı? –

+0

"Borsa" satırı nerede? Koşullu ifadeyi kolayca "if (stok> 0) {// echo url} else {// echo stock 0}" – Chay22

cevap

1

while döngüsüne bir işareti eklenir olduğunda "Stokta" ile alışveriş sepeti bağlantısını değiştirmek için bir yol gerekir bazı ne gibi:

while($row = $result->fetch_assoc()) { 

    echo '<table border="2" width="100%">'; 
    echo '<tr>'; 
    echo ' <td width="20%">' . $row["Name"] . '</td>'; 
    echo ' <td width="20%">' . $row["Descr"] . '</td>'; 
    echo ' <td width="20%">' . $row["Price"] . '</td>'; 
    echo ' <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>'; 
if($row['stock'] >0){ 
echo ' <td width="20%">' . '<a href ="http://localhost:81/shopping_cart.php">Add to cart</a>' . '</td>'; 
}else{ 
echo '<td width="20%">Out of Stock</td>'; 
} 

echo ' </tr> '; 

    echo '</table>'; 
} 
+0

Mükemmel olarak ekleyebilirsiniz, teşekkürler. İşe yarayacak başka bir şey takas etmek zorunda kaldı (neden yok ki), ama işe yaradı. Teşekkür ederim. –

İlgili konular