PHP and echoing a table

PHP and echoing a table

georagegeorage Posts: 3Questions: 1Answers: 0

I am using php to echo a table of mysql results.

My function returns every row in the database as html wrapped in a table.

How do I make my data tables only show the first 10 results and not all of them? I could make the function only return 10 results, of course, but I want to retain the functionality of the data tables "next" button.

thanks for any assistance.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    My function returns every row in the database as html wrapped in a table.

    Each row is an individual table? Could you link to the page showing the issue (per the forum rules) please? I'd need to see the HTML that is being generated.

    Allan

  • georagegeorage Posts: 3Questions: 1Answers: 0
    edited February 2016

    Thanks for the reply. The page currently does not show anything because I am retooling the php function to return an array of values instead of echoing the entire table.

    But, i generally use a php function to echo any html i need for dynamic divs. here is the php function i was using to create the table and echo it back to jquery in an ajax call.

    after the table is initialized ... $('#overlayTable').DataTable(); ... the table generated by the following function looks fine, but has every row in the database (hundreds of rows) and not just 10 as indicated by the Data Tables dropbox. Sorting does not work either.

    It would be great if I could make my tables on the server side, since that is how I usually do such things, but I am willing to learn a new method.

    thanks again for your time. it seems like the method I am trying to use would be very common, but maybe not.

    ```
    require_once("functions.php");

    function getCrew(){
    $mysqli = connectToDB();

    $query = "SELECT * from crew";
    
    $result = mysqli_query($mysqli, $query) or die('Error -- '.mysqli_error($mysqli));
    
    $html = '<table id="overlayTable" class="display" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>NAME</th>
                        <th>ROLE</th>
                        <th>AGE</th>
                        <th>GENDER</th>
                    </tr>
                </thead>';
    
    
    while($row = mysqli_fetch_assoc($result))
    {
    
    //get info
    $name = $row['fName'].' '.$row['lName'];
    $role = $row['role'];
    $age = $row['age'];
    $gender = $row['gender'];    
    
            $html = $html . '
                <tbody>
                    <tr>
                        <td>'.$name.'</td>
                        <td>'.$role.'</td>
                        <td>'.$age.'</td>
                        <td>'.$gender.'</td>
                    </tr>
                </tbody>';
    }
    

    $html = $html . '</table>';

    echo $html;
    }

    mysqli_close($mysqli);

    <?php > ``` ?>
  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    Your tbody element is in the loop. Move it outside the loop so there is only one tbody element. That should address the issue.

    Allan

  • georagegeorage Posts: 3Questions: 1Answers: 0

    Wow! You are a genius and I am a clown!

    Thanks so much for taking the time to help. It is now functioning as expected.

    http://52.4.178.78/george/www/special/stars.html

    If you click the "crew" button you will see the table you helped fix.

    If we were on reddit I would happily award you GOLD.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    Great to hear that helped :-)

    Allan

This discussion has been closed.