How to use with a while

How to use with a while

TiruveTiruve Posts: 4Questions: 1Answers: 0

Hi everyone,
I want to use DataTables with a table that is in a while which take informations from my database.

So, when I try to use DataTables, It only find one entry, and I see all my entries, but I can't search, or click on one of my tbody to sort out.

Please help me ^^
(Sorry for my bad english, I'm french ahah)

This question has accepted answers - jump to:

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    Post sample code please.

  • allanallan Posts: 63,259Questions: 1Answers: 10,421 Site admin

    Bonjour,

    If you could link to the page you are having problems with please we'll take a look and see what is going wrong. Unfortunately we really can't offer any help without a full description of the problem and being able to debug it.

    Allan

  • TiruveTiruve Posts: 4Questions: 1Answers: 0
    edited September 2015

    Hello,
    Sorry for the delay,

    so, I work on a test table, which is on my testing website, but I can't give you the link because it's protected by a htaccess with password.

    There is my code :

    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript" src="datatables.js"></script> 
    <link rel="stylesheet" type="text/css" href="datatables.css"/>
    
    <table id="example" border="1" cellspacing="0" style="width:100%" align="center" charset="UTF-8">
            <thead>
                <tr>
                     <th style="background-color:#FEFEFE;">TEST</th>
                </tr>
            </thead>
     
           <?php
            // on se connecte à MySQL 
            $db = mysql_connect( 'myserver', 'dassonviyjtest', 'mypasswd' ); 
    
            // on sélectionne la base 
            mysql_select_db('dassonviyjtest',$db); 
            mysql_set_charset('utf8');
     
            // on crée la requête SQL
            $sql = 'SELECT id_order FROM ps_order_detail 
            WHERE product_id IN ( 52 , 53 , 271 , 272 , 273 , 274 , 275 , 477 , 605 , 653 , 952 , 955 , 956 , 1274 , 1276 , 1277 , 1280 , 1585 , 1586 , 1593 , 1870 , 1871 , 2089 , 2091 , 2092 , 1256 , 1257 , 1258 , 1425 , 1426 , 1450 , 1875 , 276 , 277 , 609 , 965 , 966 , 1335 , 1336 , 1352 , 1354 , 1396 , 1462 , 1663 , 1838 , 1874 , 1880 , 1938 , 47 , 2089 , 2091 , 2092 , 1625 , 1595 , 1614 , 1615 , 1623 , 1636 , 1912 , 1913 , 1914 , 1915 , 2407 )';
            
            // on envoie la requête 
            $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
    
            // on fait une boucle qui va faire un tour pour chaque enregistrement 
            while($data = mysql_fetch_assoc($req)) {  
                echo '<tbody>';
                echo '    <tr>';
                echo '        <td>'.$data['id_order'].'</td>';
                echo '    </tr>';   
            }
        ?>
            </tbody>
        </table>
        
        <script>
        $(document).ready(function() {
            $('#example').DataTable( {
                stateSave: true
            } );    
        } );</script>
    
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    Invalid HTML. Your opening <tbody> tag should be outside your loop.

    Also, you require a <thead></thead>.

  • TiruveTiruve Posts: 4Questions: 1Answers: 0

    Oh, thank's, it work perfectly!

    I'm so bad...

  • allanallan Posts: 63,259Questions: 1Answers: 10,421 Site admin
    edited September 2015 Answer ✓

    Move the echo '<tbody>'; out of the while loop. At the moment your code is creating invalid HTMl.

    Allan

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    There's a funny echo in here....

  • allanallan Posts: 63,259Questions: 1Answers: 10,421 Site admin

    Haha - sorry. I completely missed your reply! I tend to open a mass of tabs with questions to reply to and work my way through them - I must have had it open while you were fixing it :-)

    Allan

  • TiruveTiruve Posts: 4Questions: 1Answers: 0

    Thank you both!

This discussion has been closed.