Rex Rex - 4 months ago 6
HTML Question

Get HTML Table option value through PHP

I am trying to get a table option value, specifically the value of "number-per-page" below.

<table class="centerTable" number-per-page="3" current-page="0">


I need to get the number-per-page option value using php. Is this possible? I am using in-line php if that helps.

Answer

As mentioned by an answer to a similar question, you can use:

$str   = <table class='centerTable' number-per-page='3' current-page="0">
$doc   = new DOMDocument();
$d     = $doc->loadHtml($str);
$tbl   = $doc->getElementsByTagName('table');    # select table
$attr  = $tbl->getAttribute('number-per-page');  # get attribute value

Alternately, if you add an id to your table, you can also use this to select it:

$tbl   = $doc->getElementById('your-table-id');

For more information, refer PHP DOM documentation.