-1

I have a table like this:

<table>
<tr>
<td>100</td>
</tr><br>
<tr>
<td>200</td>
</tr><br>
<tr>
<td>300</td>
</tr><br>
<tr>
<td>400</td>
</tr><br>
<tr>
<td>500</td>
</tr><br>
<tr>
<td>600</td>
</tr><br><br>
<tr>
<td>Total: </td>
<td>2100</td>
</tr>
</table>

I want to count and display the total of the column as shown above using PHP but I am not sure how to make this possible. Please help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shubham Jha
  • 31
  • 1
  • 8
  • From where the data is coming from? is it coming from any table? – RK12 Mar 10 '16 at 08:52
  • You want a crawler or sth else? – Peyman.H Mar 10 '16 at 08:53
  • You either do it directly with the data which is used to fill that table (preferred way, that data probably is comming from your server) or you use parse the html doc to find the values and them sum them (less efficent way) – Nadir Mar 10 '16 at 08:54
  • 1
    If your table is coming from a php loop you can build a variable $total, set it to 0 before getting into the loop and echo it after the loop has printed the last row. If you are writing it in html directly you need JavaScript and not php. Anyway please post your relevant code please! – Lelio Faieta Mar 10 '16 at 08:54
  • This might help you http://stackoverflow.com/questions/1149958/jquery-count-number-of-rows-in-a-table – freezingfire Mar 10 '16 at 08:59

2 Answers2

1

You could easily do this with javascript document.getElementById("idHere").value and stock the sum of them in a single variable.

The bad part is that you should give every td an Id.

In PHP you should give them name attribute, add a submit button and get the values from the global-variable $_POST["nameOfTD"];

smobx
  • 67
  • 9
  • var count = (int)document.getElementById("firstTD").innerText + (int)document.getElementById("secondTD").innerText + .....; (This is the way if you want to count the Sum of all td's. Do not count the last td as it will have the TOTAL itslef. Assign the count using: document.getElementById("totalCountTD").innerHTML = "" + count + ""; @ShubhamJha – Asif Mehmood Mar 10 '16 at 09:19
-1

You can use buffer to read the content of the page then parse html:

<?php
    ob_start();
?><table>
<tr>
<td class="foo">100</td>
</tr><br>
<tr>
<td class="foo">200</td>
</tr><br>
<tr>
<td>300</td>
</tr><br>
<tr>
<td>400</td>
</tr><br>
<tr>
<td>500</td>
</tr><br>
<tr>
<td>600</td>
</tr><br><br>
<tr>
<td>Total: </td>
<?php
    $DOM = new DOMDocument;
    $DOM->loadHTML(ob_get_contents());

    $items = $DOM->getElementsByTagName('td');
    function getClass($item) {
        foreach ($item->attributes as $name => $attrNode) {
            if ($name == 'class') {
                return $attrNode->nodeValue;
            }
        }
    }
    $sum = 0;
    for ($i = 0; $i < $items->length; $i++) {
        $item = $items->item($i);
        if (getClass($item) == 'foo') {
            $sum += intval($item->nodeValue);
        }
    }
    echo "<td>" . $sum . "</td>";
?>
</tr>
</table>
<?php
ob_end_flush();
?>
jcubic
  • 61,973
  • 54
  • 229
  • 402