0

Need help to put some jquery function inside if statement . I want to hide my div when data from database is empty . I've done like this , and nothing happened.

                    <?php if(empty($all_data)){ ?>

                            <script>
                                $(document).ready(function () {
                                    $( '.table-wrapper').css("display","none");
                                });
                            </script>                       <===update

                    <?php }else{ ?>
                        <?php foreach($all_data->result() as $data){ ?>
                            <tr> 
                                <td><?php echo $data->id_history;?></td>
                                <td><?=$data->id_admin;?></td>
                                <td><?=$data->ipc;?></td>
                                <td><?=$data->task_date;?></td>
                                <td><?=$data->task_time;?></td>
                                <?php if ($data->id_task == 1){ ?>
                                    <td>Login Site</td>  
                                <?php }else{ ?>
                                    <td>Logout Site</td>
                                <?php } ?>
                                <td>-</td>
                            </tr>
                        <?php } ?>  
                    <?php } ?>

Is it possible to put some jquery inside php ??

4
  • jQuery is JavaScript which is executed on the client side, PHP is executed on the server side. By definition you cannot put some jquery inside PHP... Commented Mar 4, 2015 at 8:09
  • 1
    Where do you call your function hiding() by the way? Commented Mar 4, 2015 at 8:11
  • 1
    Define "nothing happened". Was there an error? What was the resulting output client-side? It doesn't look like anything ever invokes that hiding() function in the PHP code, so that echo statement within it would never be executed. In order to execute the code within a function, something has to call that function. Commented Mar 4, 2015 at 8:12
  • i've deleted function hiding() and just a jquery inside script. div with class table-wrapper is not hiding. how do i check a value of $all_data ? i tried with echo $all_data; inside if statement , nothing appear a value. Commented Mar 4, 2015 at 8:56

6 Answers 6

2

Try this one

<!--Always generate table-->
<div class='table-wrapper'>
    <table>
       <?php foreach($all_data->result() as $data){ ?>
          <tr> 
         <td><?php echo $data->id_history;?></td>
         <td><?=$data->id_admin;?></td>
         <td><?=$data->ipc;?></td>
         <td><?=$data->task_date;?></td>
         <td><?=$data->task_time;?></td>
         <?php if ($data->id_task == 1){ ?>
            <td>Login Site</td>  
         <?php }else{ ?>
            <td>Logout Site</td>
         <?php } ?>
         <td>-</td>
     </tr>
<?php } ?>  
</table>
</div>

<!-- Hide Wrapper if no data -->
<script>
var div = $(".table-wrapper");
if(div.html() == "<table></table>") {
   div.css("display","none");
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

i have thead and tbody inside table . and looping process inside tbody .. i modified your code into this <script> $(document).ready(function () { var div = $(".data-table > tbody"); var wrapper = $("#log_history"); if(div.html() == '') { wrapper.css("display","none"); }else{ alert('not hiding'); } }): </script>
i have thead and tbody inside table . and looping process inside tbody .. i modified your code into this <script> $(document).ready(function () { var div = $(".data-table > tbody"); var wrapper = $("#log_history"); if(div.html() == '') { wrapper.css("display","none"); }else{ alert('not hiding'); } }): </script> it's still wont be hide
1

Nothing ever calls hiding(). So you conditionally define that function in PHP, but you never actually invoke it. If you want the contents of that function to execute in the if block then don't declare the hiding() function, just execute the code:

<?php if(empty($all_data)){
          echo "<script>
                    $(document).ready(function () {
                        $( '.table-wrapper').hide();
                    });
                </script>";
      }else{ ?>
...

Or maybe remove the echo and just emit the output directly, might look a little cleaner:

<?php if(empty($all_data)){ ?>
<script>
    $(document).ready(function () {
        $( '.table-wrapper').hide();
    });
</script>
<?php }else{ ?>
...

Though, if I'm being honest, hiding on document ready probably isn't the best approach. If .table-wrapper elements should be hidden when the page renders, conditionally style them as hidden (or don't emit them to the page at all if they're not supposed to be visible, depending on the dynamic functionality of the page). Emitting visible output and then hiding it could easily cause a poor user experience. Better to emit it as hidden in the first place or not emit it at all.

3 Comments

i've tried this and div still won't hide .. i've changed with $( '.table-wrapper').css("display","none"); it's still won't hide .. how do i check if my variable is realy empty ??? becaus in db it has not data.
@IbnuHabibie: How specifically is it failing? What is your updated code? What client-side code does it produce? Is there an error in the PHP logs? An error in the JavaScript console? When you debug the JavaScript, where does it fail? What specifically does it do?
updated my code .. i changed my jquery and deleted echo . i got no error in server side or client-side . it's just won't be hide when i check variable $all_data is empty .. is there a way to check a value of my variable ???
0

try it. dont want call any function.

<?php if(empty($all_data)){ ?>
                            <?php 

                                    echo "<script>
                                            $(document).ready(function () {
                                                $( '.table-wrapper').hide();
                                            });
                                          </script>";

                            ?>
                        <?php }?>

Comments

0

just need to call the function if query result is empty:

?php if(empty($all_data)){ ?>
    <?php 
        echo "<script>hiding();</script>";
    ?>
<?php }else{ ?>
    <?php foreach($all_data->result() as $data){ ?>
        <tr> 
            <td><?php echo $data->id_history;?></td>
            <td><?=$data->id_admin;?></td>
            <td><?=$data->ipc;?></td>
            <td><?=$data->task_date;?></td>
            <td><?=$data->task_time;?></td>
            <?php if ($data->id_task == 1){ ?>
                <td>Login Site</td>  
            <?php }else{ ?>
                <td>Logout Site</td>
            <?php } ?>
            <td>-</td>
        </tr>
    <?php } ?>  
<?php } ?>

and then

<script>
       $(document).ready(function () {
        function hiding(){
           $( '.table-wrapper').hide();
        }
       });
</script>

Comments

0

Well,delete the function hiding(){}.It's better to do this with php,like

<?php if(!empty($all_data)){ ?>
            <div class="table-wrapper">
              <?php foreach($all_data->result() as $data){ ?>
                            <tr> 
                                <td><?php echo $data->id_history;?></td>
                                <td><?=$data->id_admin;?></td>
                                <td><?=$data->ipc;?></td>
                                <td><?=$data->task_date;?></td>
                                <td><?=$data->task_time;?></td>
                                <?php if ($data->id_task == 1){ ?>
                                    <td>Login Site</td>  
                                <?php }else{ ?>
                                    <td>Logout Site</td>
                                <?php } ?>
                                <td>-</td>
                            </tr>
            <?php } ?>  
          </div>
<?php } ?>

Comments

0

Try this :

<?php if(empty($all_data)) : ?>

        <script>
            $(document).ready(function () {
                $( '.table-wrapper').css("display","none");
            });
        </script>

<?php else : ?>
    <?php foreach($all_data->result() as $data) : ?>

        <tr> 
            <td><?=$data->id_history;?></td>
            <td><?=$data->id_admin;?></td>
            <td><?=$data->ipc;?></td>
            <td><?=$data->task_date;?></td>
            <td><?=$data->task_time;?></td>

            <?php if ($data->id_task == 1) : ?>
                <td>Login Site</td>  
            <?php else : ?>
                <td>Logout Site</td>
            <?php endif; ?>

            <td>-</td>
        </tr>

    <?php endforeach; ?>  
<?php endif; ?>

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.