Home > OS >  if statement in html php page
if statement in html php page

Time:11-20

I am creating a website. The front page looks like this.

The page is created using PHP Html mysqli database. Anyway, what I wish to do is put an if statement, meaning that, for example, the page should only show information of customer inquiries in the database that have an annual income of more than 4500. I do not know where and in what way I should put this if statement since in HTML, the if statement is not supported. Here's the code:

    <?php 
    include('config/db_connect.php');

    // write query for all pizzas
    $sql = 'SELECT * FROM customers ORDER BY Monthly_salary/GREATEST(Existing_loan_amount, 0.01) DESC';
    // get the result set (set of rows)

    $result = mysqli_query($conn, $sql);

    // fetch the resulting rows as an array
    $customers = mysqli_fetch_all($result, MYSQLI_ASSOC);

    // free the $result from memory (good practise)
    mysqli_free_result($result);

    // close connection
    //mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
    <?php include('templates/header.php'); ?>
    <h4 class="center grey-text">Enquiries!</h4>
    <div class="container">
        <div class="row">
            <?php foreach($customers as $customer): ?>
                <div class="col s6 m4">
                    <div class="card z-depth-0">
                        <img src="img/pizza.svg"class="pizza">
                        <div class="card-content center">
                            <h6><?php echo "Form_Submission_Time: ". htmlspecialchars($customer['Form_Submission_Time']); ?></h6>
                            <ul class="grey-text">
                                <?php foreach(explode(',', $customer['Gender']) as $Gender): ?>
                                    <li><?php echo "Gender: ". htmlspecialchars($Gender); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Job'])as $Job): ?>
                                    <li><?php echo "Job: ". htmlspecialchars($Job); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Monthly_salary']) as $Monthly_salary): ?>
                                    <li><?php echo "Annual income: ". htmlspecialchars($Monthly_salary*12); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Existing_loan']) as $Existing_loan): ?>
                                    <li><?php echo "Existing Loan: ". htmlspecialchars($Existing_loan); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Existing_loan_amount']) as $Existing_loan_amount): ?>
                                    <li><?php echo "Existing loan amount: ". htmlspecialchars($Existing_loan_amount); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Residential_Type']) as $Residential_Type): ?>
                                    <li><?php echo "Residential Type: ". htmlspecialchars($Residential_Type); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['Job_Type']) as $Job_Type): ?>
                                    <li><?php echo "Job Type: ". htmlspecialchars($Job_Type); ?></li>
                                <?php endforeach; ?>
                                <?php foreach(explode(',', $customer['MPF_account']) as $MPF_account): ?>
                                    <li><?php echo "MPF account: ". htmlspecialchars($MPF_account); ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                        <?php 
                        $newID = ($customer['id']); 
                        ?>
                        <div class="card-action right-align"> 
                            <a class="brand-text" href="payment.php">Payment</a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    <!--<a  href="details.php?id=<//?php echo $customer['id'] ?>">more info</a>-->
    <?php include('templates/footer.php'); ?>
</html>

CodePudding user response:

Either

add a WHERE Monthly_salary >= 4500 on the server

or use JS:

<li class="annual"><?php echo "Annual income: ". 

using

const showIncome = amount => {
  document.querySelectorAll(".annual").forEach(li => {
    const amt =  li.textContent.trim()
    li.closest(".card").hidden = amt/12 < amount;
  })
};

then you can have

<button type="button" onclick="showIncome(4500)">4500/month or more</button>

CodePudding user response:

$sql = 'SELECT * FROM customers WHERE Monthly_salary>400 ORDER BY Monthly_salary/GREATEST(Existing_loan_amount, 0.01) DESC';
  • Related