I want to bring two records of my ID and DISTRICT fields but the districts are duplicated I want to bring only one of them as I do?

0
try{
                    require_once('conexion.php');
                    $sql = "SELECT DISTINCT(TRIM(commerce_district)) AS commerce_district, commerce_id FROM 'commerc' ORDER BY 'commerc'.'commerce_district' ASC ";
                    // $sql2  = "SELECT commerce_district FROM 'commerc'";

                    $resultado = $con->query($sql);
                    // $resultado2 = $con->query($sql2);

                }catch(Exception $e){
                    $error = $e->getMessage();
                }

<div class="estado pb-4">
                <div class="list-group" id="navbar-example">
                    <p class="list-group-item list-group-item-action active">Seleccione el estado</p>
                    <div class="scroll">
                        <?php while($distrito = $resultado->fetch_assoc()) { ?>
                        <label for="checkbox" class="list d-flex">
                            <?php echo $distrito['commerce_district'] ?>
                            <input type="checkbox" value="<?php echo $distrito['commerce_id'] ?>" name="estado"
                             id="checkbox"/>
                        </label>
                        <?php } ?>
                    </div>
                    <!--fin scroll -->
                </div>
            </div>
    
asked by ivan fuenmayor 19.10.2018 в 14:59
source

1 answer

0

Try to change your While cycle for a For cycle, I think that this way you ensure that more control over how the results you get from the SQL query are traversed; it would be something like this:

<?php for($i=0;$distrito < $resultado->fetch_assoc();$i++) { ?>
                    <label for="checkbox" class="list d-flex">
                        <?php echo $distrito[$i]['commerce_district'] ?>
                        <input type="checkbox" value="<?php echo $distrito[$i]['commerce_id'] ?>" name="estado"
                         id="checkbox"/>
                    </label>
                    <?php } ?>

If it does not work for you, try to put a conditional (If) within the cycle that you have and that will be in charge of comparing the current result with the previous or next one, so that when they are the same, do not reprint it in screen and continue with the record in the next position of the array.

    
answered by 19.10.2018 в 18:15