hi can anyone see this code and tell me if there's any other better way to improve this? I feel like im not being efficient with this code and there are many times i've encountered this kind of situations where I have to output lots of html tags inside php due to conditions.... here's the sample code thanks in advance for the tips

<?php
if (empty($id)) {
echo "
Search Customer : <br />
<select name = 'search_type'>
<option value = 'default'>-Select A Catergory-</option>
<option value = 'first_name'>First Name</option>
<option value = 'last_name'>Last Name</option>
<option value = 'address'>Address</option>
</select>
<input type = 'text' name = 'search_customer' />
<input type = 'submit' name = 'search' value = 'search' />
";
}

    One option is to keep the HTML separate from the code:

    <?php
    if (empty($id)) {
       idForm();
    }
    else {
       // do something else
    }
    
    // ... then at the end of the script where it does not disrupt the code flow:
    
    function idForm()
    {
    ?>
    Search Customer : <br />
    <select name = 'search_type'>
    <option value = 'default'>-Select A Catergory-</option>
    <option value = 'first_name'>First Name</option>
    <option value = 'last_name'>Last Name</option>
    <option value = 'address'>Address</option>
    </select>
    <input type = 'text' name = 'search_customer' />
    <input type = 'submit' name = 'search' value = 'search' />
    <?php
    }
    ?>
    

    Alternatively, all the HTML output functions could be in a separate include file, which you would require/include_once() at the start of your script, then call the desired output functions when and where they are needed.

      Write a Reply...