0

In my page, I have a list of shops with some information. For each shop, I have a button that opens a modal where I want to show all the information about the shop.

I put my modal inside the while that generates the list and the first result is correctly shown into the modal, but if I click the second, third etc... shop in the list, I see the information about the first shop (now I tried only with the name of the shop). I don't understand why.

Is it possible for each store to show its information? Is it mandatory to use ajax?

I check this question How can i show data into bootstrap panel dynamically by javascript? but it didn't helped me.

This is the code with the php that generates my list, the button that opens the modal, and the modal:

<?php
    echo'<section class="col-xs-12 col-sm-6 col-md-12">';
    $sql = "SELECT nome_L, tipocucina_TC, wifi_L, tipolocale_TL, descrizione_L, indirizzo_L, fasciaprezzo_L, convenzione_L, image_thumb_L  FROM locale l 
            JOIN tipocucina c On l.TipoCucina_L = c.IDtipocucina_TC
            JOIN tipolocale t On l.TipoLocale_L = t.IDTipolocale_TL
            WHERE TRUE";

    $result = mysqli_query($conn,$sql) or die(mysqli_error($conn));

    while($row = mysqli_fetch_array($result)) 
    {
        $nome_Local = $row['nome_L'];
        $descrizione_Local = $row['descrizione_L'];
        $indirizzo_Local = $row['indirizzo_L'];
        $tipocucina_Local = $row['tipocucina_TC'];
        $tipolocale_Local = $row['tipolocale_TL'];
        $immagine_Local = $row['image_thumb_L'];

        //START MODAL
        echo'
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">'.$nome_Local.'</h4>
                        </div>
                        <div class="modal-body">
                        ...
                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>
        ';
        //END MODAL

        echo'<article class="search-result row">
                <div class="col-xs-12 col-sm-12 col-md-3">
                    <a href="#" title="Lorem ipsum" class="thumbnail"><img src="images/locals/'.$immagine_Local.'"/></a>
                </div>
                <div class="col-xs-12 col-sm-12 col-md-2" style="width: 18%;">
                    <ul class="meta-search">
                        <li><i class="fa fa-cutlery fa-lg"></i> <span>'.$tipocucina_Local.'</span></li>
                        <li><i class="fa fa-coffee fa-lg"></i> <span>'.$tipolocale_Local.'</span></li>
                    </ul>
                </div>

                <div class="col-xs-12 col-sm-12 col-md-7 excerpet" style="width: 55%;">';
                    echo"<h3><a>".$nome_Local."</a></h3>";
                    echo'<i class="fa fa-compass"> </i>'.$indirizzo_Local.'</i>';
                    echo"<br>";
                    echo'<p class="local-description">'.$descrizione_Local.'</p>';   

                    //BUTTON THAT OPEN MY MODAL
                    echo'<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Show more </button>'; 
                    echo'
                </div>
                <span class="clearfix borda"></span>
            </article>'; 
    }              
    $conn->close(); 
?>

Hopefully, I have described the problem correctly.

4
  • every modal has the same id=myModal. This is why bootstrap will always open the same. Give them unique ids and adjust the opening button accordingly to the same ids (in data-target) Commented Jul 22, 2017 at 11:32
  • I can use the id of the shop from the database? Commented Jul 22, 2017 at 11:38
  • yes, that would be best. Commented Jul 22, 2017 at 11:39
  • thank you so much for the answer! Commented Jul 22, 2017 at 13:08

1 Answer 1

1

As someone has commented, you are giving every modal the same ID, and every button the same target. You need to give them unique ones. Declare a counter just before your while loop, say $i = 0;

$i = 0;
while ( ... 

Change:

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

to:

 <div class="modal fade" id="myModal_<? echo $i; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

and change the button, from:

echo'<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Show more </button>'; 

to:

echo'<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal_' .$i .'"> Show more </button>'; 

and increment $i before your while loop closes, using $i++:

    ...
    $i++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.