Data tables on server side using a for loop in selecting value from database

Data tables on server side using a for loop in selecting value from database

nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0
edited January 2021 in Free community support

Hello everyone,
Without controversy, data tables is the best thing after slice bread in presenting information from the data base.
Now i have a situation at hand and im lost if data tables can help perform the required action.

I can draw data from data base using server. thats fine.
However i need to insert a for loop and an if else, as a condition for placing a particular field from the database in a <td></td> column.

The code, (which i can do on client side, but is very slow because i need to output like 30,000 rows) is something like this

                                        **  <td>
                                            <?php  if ($key == $k){
                                                    echo $volume; 
                                                    } 
                                                    ?>
                                                                                           </td>**

But this isnt possible with server side because one you query your rows, your send all up to json with this....

while( $rows = mysqli_fetch_assoc($resultset) ) {
    
**  $data[] = $rows;
}
$results = array();
    echo json_encode($results);**

is there any idea how with server side i can perform an if else or for loop over a <td></td> element before outputting it into the element and then to json?

Thanks a lot

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Without controversy, data tables is the best thing after slice bread in presenting information from the data base.

    Haha - thank you :).

    So with the issue you are having - if it isn't important that the data is sent to the client, you could use a renderer to perform the conditional check and return an empty string for the display if it isn't met. The downside is that the data is still in the JSON loaded by the client, which might or might not be an issue for you.

    On the server-side, what you'd need to do is build up the $data[] array more explicitly - e.g.:

    $data[] = [
      "name" => $row["name"],
      "volume" => $key == $k ? $row["volume"] : '',
      ...
    ];
    

    Or something like that!

    Allan

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0
    edited January 2021

    Hi allan, thanks so much for tkaing time to answer.

    im a bot new to data tables proper so please bear with my ignorance.
    From your reply, im getting that you mean i can modify this code snippet that sends fetched data into an array...

    **while( $row = mysqli_fetch_row($queryRecords) ) { 
            $data[] = $row;
        }
    **
    

    into this that you wrote in your reply ..

    $data[] = [
      "name" => $row["name"],
      "volume" => $key == $k ? $row["volume"] : '',
      ...
    ];
    

    right?
    so that means the data bles will adjust the data gotten from the server based on the name assigned to the row above e.g "name" => $row["name"] right?

    Thanks in advance

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    That's correct. For volume, if the condition isn't met, the server will just send that empty string so the data will never be readable on the client.

    Colin

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0

    Hi Allan,
    Thanks so much for replying.

    based on your advise, i got an idea and instead manipulated the tow data fetching the results from the server side, then hid some columns and viola,,,got my result :)

    However, can i please get an idea on how to sum total this columns, e.g from col 5 to col 10. at the footer?
    Thanks a bunch

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    If you are doing it all client-side, then this example shows how you can do that.

    Regards,
    Allan

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0
    edited January 2021

    Im doing it at server side, not client side sir

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0
    edited January 2021

    Hello allen, i looked around and got some feedbacks on how to implement the footer sum. Thank you. I just have one final questions please (pardon me)

    How do i implement the index column on data tables? Thank you

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    By index column do you mean a unique number coming from the database?

    Colin

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0

    hi colin, thanks for replying. I mean like, an auto increment that increments by itself for S/N (1, 2, 3 ...)

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    I see. For that you could either do it client-side, like here, otherwise you could do it in the database with a primary key,

    Colin

  • nnamdiosunnamdiosu Posts: 7Questions: 1Answers: 0

    Awesome. thanks so much colin

This discussion has been closed.