mehidy Posted September 29, 2017 Share Posted September 29, 2017 (edited) Hi I am viewing xls table in php using below code. I want to count the no of row of the table below the line. But its not appearing the result. Result is showing O, but it should be 6. Please have a look & advise. echo $table ="<table>"; $x=1; while($x<=$connection->sheets[$sheet]['numRows']) { echo "\t<tr>\n"; $y=1; while($y<=$connection->sheets[$sheet]['numCols']) { $cell = isset($connection->sheets[$sheet]['cells'][$x][$y]) ? $connection->sheets[$sheet]['cells'][$x][$y] : ''; echo "\t\t<td>$cell</td>\n"; $y++; } echo "\t</tr>\n"; $x++; } echo "</table>"; $count = substr_count($table, "<tr>"); echo "Total rows: ".$count; echo "</br>"; Edited September 29, 2017 by mehidy Quote Link to comment https://forums.phpfreaks.com/topic/305154-count-total-no-of-row-in-a-table-in-php/ Share on other sites More sharing options...
phpmillion Posted September 29, 2017 Share Posted September 29, 2017 Aren't 2 answers enough for you? http://www.codingforums.com/php/387653-count-total-no-row-table-php.html Quote Link to comment https://forums.phpfreaks.com/topic/305154-count-total-no-of-row-in-a-table-in-php/#findComment-1552113 Share on other sites More sharing options...
Barand Posted September 29, 2017 Share Posted September 29, 2017 Have you echoed $table to check them manually? Why are you outputting tab characters inside the cells? Won't the number your trying to output be very similar to $connection->sheets[$sheet]['numrows'] ? Quote Link to comment https://forums.phpfreaks.com/topic/305154-count-total-no-of-row-in-a-table-in-php/#findComment-1552116 Share on other sites More sharing options...
Psycho Posted September 29, 2017 Share Posted September 29, 2017 (edited) Well, let's look at how you are getting the count of rows $count = substr_count($table, "<tr>"); Ok, so you are using a function to count the number of occurrences of "<tr>" within the variable $table. Ok, now let's look at where the "<tr>" values are being added to the $table variable. Oh wait, they aren't! For some unexplained reason the output is being echo'd instead of appended to the $table variable. in fact, the only place $table is being defined is in the initial definition echo $table ="<table>"; Why you would echo a value being defined to a variable makes no sense. So, let's start over. Only define/append values to $table (do not echo anything). Then, once ALL of the content has been defined, THEN echo the variable to the page. As for counting the rows, having to use substr_count() after you have defined the content is not how I would suggest doing it. Instead, create a counter before the loop $count = 0;, then increment the counter within the loop $count++;. Barand's suggestion would obviously work for this particular scenario as would counting the "<tr>" instances. But, I suggest adding a loop counter for a couple of reasons. 1) If you were to add a header row or other "row" content to the output, them the number of "<tr>" tags might not be the correct number. 2) When processing an input source (such as this spreadsheet) you may have a need to reject some rows of data for the output. By having a loop counter inside the loop you can ensure rejected rows do not increment the counter. Why are you outputting tab characters inside the cells? I do this as well. I also include line breasks in the output: \n. It makes debugging the generated HTML code much, much simpler. E.g. <table><tr><td>cell1a</td><td>cell2a</td><td>cell3a</td></tr><tr><td>cell1b</td><td>cell2b</td><td>cell3b</td></tr></table> Vs <table> <tr> <td>cell1a</td> <td>cell2a</td> <td>cell3a</td> </tr> <tr> <td>cell1b</td> <td>cell2b</td> <td>cell3b</td> </tr> </table> Edited September 29, 2017 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/305154-count-total-no-of-row-in-a-table-in-php/#findComment-1552119 Share on other sites More sharing options...
mehidy Posted September 29, 2017 Author Share Posted September 29, 2017 Aren't 2 answers enough for you? http://www.codingforums.com/php/387653-count-total-no-row-table-php.html Got the answer. Quote Link to comment https://forums.phpfreaks.com/topic/305154-count-total-no-of-row-in-a-table-in-php/#findComment-1552126 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.