I have an if statement in my PHP file in which to include a button base on the index from the foreach loop. But I can figure out what is the problem.
I try to echo the list variable immediately after the foreach and it works. And secondly, if statement works but when I try to echo the variable inside the else part nothing shows up (the value of the id which is one in my case).
Here is the code:
<div id="product_attr_box">
<?php
$attrProductLoop=1;
foreach($attrProduct AS $list) { ?>
<div class="attr-row" id="attr_<?php echo $attrProductLoop ?>">
<div class="attr-item">
<label>Price ₦</label><br>
<input name="price[]" type="text" id="price" placeholder="Enter Product Price" value="<?php echo $list['price'] ?>"/>
</div>
<div class="attr-item">
<label>Quantity</label><br>
<input name="qty[]" type="text" id="qty" placeholder="Enter Product Quantity" value="<?php echo $list['qty'] ?>"/>
</div>
<div class="attr-item">
<label>Size</label><br>
<select name="size_id[]" id="size_id" class="inv-text-inp" value="<?php echo $list['size'] ?>">
<option value="">Size</option>
<?php
$sql=mysqli_query($conn,"SELECT id,size FROM size_master ORDER BY order_by ASC");
while($row=mysqli_fetch_assoc($sql)){
if($list['size_id']==$row['id']){
echo "<option value=".$row['id']." selected>".$row['size']."</option>";
}else{
echo "<option value=".$row['id'].">".$row['size']."</option>";
}
}
?>
</select>
</div>
<div class="attr-item">
<label>Color</label><br>
<select name="color_id[]" id="color_id" class="inv-text-inp">
<option value="">Color</option>
<?php
$sql=mysqli_query($conn,"SELECT id,color FROM color_master ORDER BY color ASC");
while($row=mysqli_fetch_assoc($sql)){
if($list['color_id']==$row['id']){
echo "<option value=".$row['id']." selected>".$row['color']."</option>";
}else{
echo "<option value=".$row['id'].">".$row['color']."</option>";
}
}
?>
</select>
</div>
<div class="attr-item">
<label></label><br>
<?php
if($attrProductLoop==1){
?>
<button type="button" onclick="add_more_attr()">Add More</button>
<?php
}else{
?>
<button type="button" style="background: red" onclick="remove_attr('<?php echo $attrProductLoop?>','<?php echo $list['id']?>')">Remove</button>
<?php
}
?>
</div>
</div>
<?php
$attrProductLoop++;
}
?>
</div>
Aucun commentaire:
Enregistrer un commentaire