Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating html table with database data through user selection in php
    primarykey
    data
    text
    <p>So, I have a checkbox that allows the user to select the columns they want to view. So, if the user wants for example to select only 2 columns (say, TicketID and Category) it will only show the data of the ID and the category of the ticket. Here's what I have so far:</p> <p>form:</p> <pre><code> &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt; &lt;ul&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="TicketID" &gt;Ticket ID&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="Category" /&gt;Category&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="Priority" /&gt;Priority&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="Status" /&gt;Status&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="InitialDescription" /&gt;Initial Description&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="SubmittedDate" /&gt;Submitted Date&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="filter[]" value="Description" /&gt;Status Description&lt;/li&gt; &lt;input type="submit" value="Refresh Filters"&gt; &lt;/ul&gt; &lt;/form&gt; &lt;/td&gt; &lt;td&gt; &lt;?php viewTicketTable($_SESSION['userID'],$_POST['filter']); ?&gt; </code></pre> <p>viewticketTable function:</p> <pre><code>function viewTicketTable($userID,$columns) { /* Accepts $userID which will identify the tickets related with the user and $columns which will filter only the columns that are required and outputs the table with the columns passed through. */ /* $columns_array = explode(',', $columns); foreach($array as $array){ echo $array; } */ foreach($columns as $filter) { $filter = $filter . ',' ; } $filter = substr($filter, 0, -1); $query = mysql_query(" SELECT $filter FROM Ticket LEFT JOIN TicketHistory ON Ticket.TicketID = TicketHistory.TicketID WHERE CustomerID = $userID; "); /* Creation of the table */ echo ' &lt;table border="1"&gt; &lt;thead&gt; &lt;tr&gt;'; foreach($columns as $tableHeader) { $tableHeader = '&lt;th scope="col"&gt;' . $tableHeader . '&lt;/th&gt;' ; echo $tableHeader; } echo ' &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; '; /*Looping through the script to print out all the information.*/ while ($row = mysql_fetch_array($query) or die(mysql_error())) { echo ' &lt;tr&gt;'; foreach($columns as $field) { echo '&lt;td&gt;' . $row[$field] . '&lt;/td&gt;'; } echo '&lt;/tr&gt; '; } echo ' &lt;/tbody&gt; &lt;/table&gt;'; </code></pre> <p>}</p> <p>The problem resides in the while loop. row[$field] is only populating the last column instead of populating all of them. Any help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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