Note that there are some explanatory texts on larger screens.

plurals
  1. POphp clone not working how I expected
    text
    copied!<p>Trying to construct a simple datagrid from a mySQL db.</p> <p>The database class I am using to connect returns the result set as an object. I want to clone this object so that i can use the getNext() method to get the headers of the table without moving the pointer forward to drop the first row returned. I expect this to be simple as cloning the result set so that I now have 2 object that are identical. then returning the headers in one object while leaving the other object untouched. </p> <p>This however has proven to be harder then I thought. Perhaps I am not using clone properly so if you can help please tell me what I am doing wrong. </p> <p>Below is the code:</p> <pre><code> function gethtmlTable($database, $table) { $db = new DB_Connection(); $sql = "SELECT * FROM $table;"; $result = $db-&gt;query($sql,$database); $tabelheader = clone $result; $tablerows = clone $result; if (!$result) die($db-&gt;getError()); if ($result-&gt;getNumRows() == 0) die('No Results'); $count = $tabelheader-&gt;getNumRows(); $html = "&lt;table&gt;&lt;th&gt;Select&lt;/th&gt;"; // echo "&lt;pre&gt;".var_dump($result)."&lt;/pre&gt;"; foreach($tabelheader-&gt;getNext() as $k =&gt; $v){ $html .="&lt;th&gt;".$k."&lt;/th&gt;"; } while($count &gt; 0){ $row = $tablerows-&gt;getNext(); $html .= "&lt;tr&gt;"; $html .= "&lt;td&gt;&lt;input type='checkbox' id='checkbox".$count."' name=checkbox".$count." class ='styled' value='checked'&gt;&lt;/td&gt;"; foreach($row as $k =&gt; $v){ $html .="&lt;td&gt;".$v."&lt;/td&gt;"; } //foreach($result-&gt;getNext() as $k =&gt; $v){ // $html .="&lt;td&gt;".$v."&lt;/td&gt;"; //} $html .="&lt;/tr&gt;"; $count--; } $html .="&lt;/table&gt;"; echo $html; } </code></pre> <p>When I run this function I get back a table with headers and a select box but the first row is always missing. :( </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload