Xtremer360 Posted January 12, 2011 Share Posted January 12, 2011 This script works however I'm wanting create an if statement with jquery where if there are 0 rows then removes the whole table. It does remove the row but if there are 0 rows left in the db then it removes the whole table and the else part would to me be the just remove the row. <?php // Include the database page include ('../inc/dbconfig.php'); $query = "SELECT CONCAT_WS(' ', firstname, lastname) AS name, DATE_FORMAT(templates.datecreated, '%M %d, %Y') AS datecreated, templates.id, templates.templatename FROM templates INNER JOIN handlers ON templates.creator_id = handlers.id WHERE templates.enabled = '0'"; $result = mysqli_query ( $dbc, $query ); // Run The Query $rows = mysqli_num_rows($result); $itemsPerPage = 10; $pages = ceil( $rows / $itemsPerPage ); ?> <script> $(document).ready(function() { $('a', $('div#addform')).click(function() { $('#innerContent').load('forms/' + $(this).attr('id') + '.php?case=addnew'); }); // Add the table sorter and table paginator plugins $('#templatesPageList').tablesorter() .tablesorterPager({ container: $( '#templatesPageList .paginate' ), cssPageLinks: 'a.pageLink' }); $('.edit').click(function(){ var templateID = $(this).attr('rel'); $('#innerContent').load('forms/template.php?id=' + templateID + '&case=edit'); }); $('.delete').click(function(){ var templateID = $(this).attr('rel'); var dataString = 'templateID=' + templateID + '&deletetemplate=True'; $.ajax({ type: "POST", url: "processes/template.php", data: dataString, }); $(this).parents("tr").eq(0).hide(); }); }); </script> <!-- Title --> <div id="title" class="b2"> <h2>Templates</h2> <!-- TitleActions --> <div id="titleActions"> <!-- ListSearch --> <div class="listSearch actionBlock"> <div class="search"> <label for="search">Recherche</label> <input type="text" name="search" id="search" class="text" /> </div> <div class="submit"> <button type="submit" id="search-button" class="button"><strong><img src="img/icons/search_48.png" alt="comments" class="icon "/></strong></button> </div> </div> <!-- /ListSearch --> <!-- newPost --> <div id="addform" class="newPost actionBlock"> <a href="#" id="template" class="button"><strong>Add New Template<img src="img/icons/add_48.png" alt="new post" class="icon "/></strong></a> </div> <!-- /newPost --> </div> <!-- /TitleActions --> </div> <!-- Title --> <!-- Inner Content --> <div id="innerContent"> <!-- ListHeader --> <div id="listHeader"> <p class="listInfos"> You have <?php echo $rows; ?> templates. </p> </div> <!-- /ListHeader --> <!-- ListTable --> <?php if ($rows > 0) { ?> <table cellspacing="0" class="listTable" id="templatesPageList"> <!-- Thead --> <thead> <tr> <th><a href="#" title="Template Name">Template Name</a></th> <th><a href="#" title="Creator">Creator</a></th> <th><a href="#" title="Date Created">Date Created</a></th> <th class="last">Edit/Delete</th> </tr> </thead> <!-- /Thead --> <!-- Tfoot --> <tfoot> <tr> <td colspan="4"> <div class="inner"> <div class="paginate"> <?php if( $pages > 1 ) { for( $i = 1; $i <= $pages; $i++ ) { echo '<span style="padding-left: 5px; padding-right: 5px;"><a class="pageLink" href="#">' . $i . '</a></span>'; } } ?> </div> </div> </td> </tr> </tfoot> <!-- /Tfoot --> <!-- Tbody --> <tbody> <?php while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { echo ' <tr> <td>' . $row['templatename'] . '</td> <td>' . $row['name'] . '</td> <td>' . $row['datecreated'] . '</td> <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['id'] . '"/><img src="img/Delete-icon.png" class="delete" rel="' . $row['id'] . '"/></td> </tr>'; } ?> </tbody> <!-- /Tbody --> </table> <?php } ?> <!-- /ListTable --> </div> <!-- /Inner Content --> Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/ Share on other sites More sharing options...
requinix Posted January 12, 2011 Share Posted January 12, 2011 I was going to mention this on DS, but didn't: assuming you don't want to unhide deleted rows, actually delete them. That would mean you could do var table = $(this).closest("table"); if (table.find("tr").length == 1) { table.remove(); } else { $(this).closest("tr").remove(); } Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/#findComment-1158591 Share on other sites More sharing options...
Xtremer360 Posted January 12, 2011 Author Share Posted January 12, 2011 I like the if statement but its still showing the table. Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/#findComment-1158604 Share on other sites More sharing options...
requinix Posted January 12, 2011 Share Posted January 12, 2011 Probably because you use a table header row. Try ==2. Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/#findComment-1158634 Share on other sites More sharing options...
Xtremer360 Posted January 12, 2011 Author Share Posted January 12, 2011 Dang didn't work here's my full time maybe seeing it might tell you why it hasn't yet. <!-- ListTable --> <?php if ($rows > 0) { ?> <table cellspacing="0" class="listTable" id="templatesPageList"> <!-- Thead --> <thead> <tr> <th><a href="#" title="Template Name">Template Name</a></th> <th><a href="#" title="Creator">Creator</a></th> <th><a href="#" title="Date Created">Date Created</a></th> <th class="last">Edit/Delete</th> </tr> </thead> <!-- /Thead --> <!-- Tfoot --> <tfoot> <tr> <td colspan="4"> <div class="inner"> <div class="paginate"> <?php if( $pages > 1 ) { for( $i = 1; $i <= $pages; $i++ ) { echo '<span style="padding-left: 5px; padding-right: 5px;"><a class="pageLink" href="#">' . $i . '</a></span>'; } } ?> </div> </div> </td> </tr> </tfoot> <!-- /Tfoot --> <!-- Tbody --> <tbody> <?php while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { echo ' <tr> <td>' . $row['templatename'] . '</td> <td>' . $row['name'] . '</td> <td>' . $row['datecreated'] . '</td> <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['id'] . '"/><img src="img/Delete-icon.png" class="delete" rel="' . $row['id'] . '"/></td> </tr>'; } ?> </tbody> <!-- /Tbody --> </table> <?php } ?> <!-- /ListTable --> Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/#findComment-1158640 Share on other sites More sharing options...
Xtremer360 Posted January 13, 2011 Author Share Posted January 13, 2011 I've been working with other stuff today but I would like to try and see if anyone is available in addition to requinix that can also maybe point something out. Just to remind everyone what I'm trying to do is when the user clicks the red x, which is a image, it will remove the table row, which it is doing currently, however when there are no rows I would like to to just remove the whole table if possible. Updated FULL CODE <?php // Include the database page include ('../inc/dbconfig.php'); $query = "SELECT CONCAT_WS(' ', handlers.firstname, handlers.lastname) AS name, DATE_FORMAT(templates.datecreated, '%M %d, %Y') AS datecreated, templates.id, templates.templatename FROM templates INNER JOIN handlers ON templates.creator_id = handlers.id WHERE templates.enabled = '0'"; $result = mysqli_query ( $dbc, $query ); // Run The Query $rows = mysqli_num_rows($result); $itemsPerPage = 10; $pages = ceil( $rows / $itemsPerPage ); ?> <script> $(document).ready(function() { $('a', $('div#addform')).click(function() { $('#innerContent').load('forms/' + $(this).attr('id') + '.php?case=addnew'); }); // Add the table sorter and table paginator plugins $('#templatesPageList').tablesorter() .tablesorterPager({ container: $( '#templatesPageList .paginate' ), cssPageLinks: 'a.pageLink' }); $('.edit').click(function(){ var templateID = $(this).attr('rel'); $('#innerContent').load('forms/template.php?id=' + templateID + '&case=edit'); }); $('.delete').click(function(){ var templateID = $(this).attr('rel'); var dataString = 'templateID=' + templateID + '&deletetemplate=True'; $.ajax({ type: "POST", url: "processes/template.php", data: dataString, }); var table = $(this).closest("table"); if (table.find("tr").length == 2) { table.remove(); } else { $(this).closest("tr").remove(); } }); }); </script> <!-- Title --> <div id="title" class="b2"> <h2>Templates</h2> <!-- TitleActions --> <div id="titleActions"> <!-- ListSearch --> <div class="listSearch actionBlock"> <div class="search"> <label for="search">Recherche</label> <input type="text" name="search" id="search" class="text" /> </div> <div class="submit"> <button type="submit" id="search-button" class="button"><strong><img src="img/icons/search_48.png" alt="comments" class="icon "/></strong></button> </div> </div> <!-- /ListSearch --> <!-- newPost --> <div id="addform" class="newPost actionBlock"> <a href="#" id="template" class="button"><strong>Add New Template<img src="img/icons/add_48.png" alt="new post" class="icon "/></strong></a> </div> <!-- /newPost --> </div> <!-- /TitleActions --> </div> <!-- Title --> <!-- Inner Content --> <div id="innerContent"> <!-- ListHeader --> <div id="listHeader"> <p class="listInfos"> You have <?php echo $rows; ?> templates. </p> </div> <!-- /ListHeader --> <!-- ListTable --> <?php if ($rows > 0) { ?> <table cellspacing="0" class="listTable" id="templatesPageList"> <!-- Thead --> <thead> <tr> <th><a href="#" title="Template Name">Template Name</a></th> <th><a href="#" title="Creator">Creator</a></th> <th><a href="#" title="Date Created">Date Created</a></th> <th class="last">Edit/Delete</th> </tr> </thead> <!-- /Thead --> <!-- Tfoot --> <tfoot> <tr> <td colspan="4"> <div class="inner"> <div class="paginate"> <?php if( $pages > 1 ) { for( $i = 1; $i <= $pages; $i++ ) { echo '<span style="padding-left: 5px; padding-right: 5px;"><a class="pageLink" href="#">' . $i . '</a></span>'; } } ?> </div> </div> </td> </tr> </tfoot> <!-- /Tfoot --> <!-- Tbody --> <tbody> <?php while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { echo ' <tr> <td>' . $row['templatename'] . '</td> <td>' . $row['name'] . '</td> <td>' . $row['datecreated'] . '</td> <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['id'] . '"/><img src="img/Delete-icon.png" class="delete" rel="' . $row['id'] . '"/></td> </tr>'; } ?> </tbody> <!-- /Tbody --> </table> <?php } ?> <!-- /ListTable --> </div> <!-- /Inner Content --> Quote Link to comment https://forums.phpfreaks.com/topic/224228-removing-table-if-statement/#findComment-1158791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.