Hi all,

I am working on a script that displays dynamic rows of questions in a HTML table. Each question can have one of two stats, open (not answered) or closed (answered). If the question is open a form button is display which is clickable and opens another page.

The questions are stored in a data table (ItemCategory), this table can be edited by the user so the number of questions can vary (dynamic).

When the HTML table is loaded is displays row by row the question in ItemCategory and depending on if the question has already been answerd displays the open button or the closed iamge.

[ATTACH]5359[/ATTACH]

Because the question list is dynamic the stored answers data table needs to be queried so I am using the result of a query from the Information Schema to return the column names, and then creates an array of column names, ($SeqIDArray).

$strSQLCol = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
				WHERE TABLE_NAME= '".$_SESSION['NX]."_Active' AND COLUMN_NAME LIKE 'SeqID%'";

$rsCol = mysqli_query($link,$strSQLCol);										

$SeqList = array();

do {
	$SeqList[] = $row;
} while($row = $rsCol->fetch_assoc());


$SeqIDArray = array();
foreach($SeqList as $key => $value) {
	if (1 > $key) { continue; }
	$SeqIDArray[] = 'SeqID' . $key;
}

The array content looks like:

Array ( [0] => SeqID1 [1] => SeqID2 [2] => SeqID3 [3] => SeqID4 [4] => SeqID5 [5] => SeqID6 [6] => SeqID7 [7] => SeqID8 [8] => SeqID9 [9] => SeqID10 [10] => SeqID11 [11] => SeqID12 [12] => SeqID13 [13] => SeqID14 [14] => SeqID15 [15] => SeqID16 [16] => SeqID17 [17] => SeqID18 [18] => SeqID19 [19] => SeqID20 [20] => SeqID21 [21] => SeqID22 [22] => SeqID23 [23] => SeqID24 [24] => SeqID25 [25] => SeqID26 [26] => SeqID27 [27] => SeqID28 [28] => SeqID29 [29] => SeqID30 [30] => SeqID31 [31] => SeqID32 [32] => SeqID33 [33] => SeqID34 [34] => SeqID35 [35] => SeqID36 [36] => SeqID37 [37] => SeqID38 [38] => SeqID39 [39] => SeqID40 [40] => SeqID41 [41] => SeqID42 [42] => SeqID43 [43] => SeqID44 [44] => SeqID45 [45] => SeqID46 [46] => SeqID47 ) 

I then use the array to create a query to return the results from the table which holds the answered and un-answered list.

$SQLActive = "SELECT  " . join(', ', $SeqIDArray) . " FROM ".$_SESSION['NX']."_Active WHERE UniqueID = '".$_SESSION['UniqueID']."'";
$SeqActive = mysqli_query($link,$SQLActive);

while ($row = mysqli_fetch_assoc($SeqActive)) {
	$Seq[] = $row;
}

This result of the query is:

Array ( [0] => Array ( [SeqID1] => 1 [SeqID2] => 1 [SeqID3] => [SeqID4] => [SeqID5] => [SeqID6] => [SeqID7] => [SeqID8] => [SeqID9] => [SeqID10] => [SeqID11] => [SeqID12] => [SeqID13] => [SeqID14] => [SeqID15] => [SeqID16] => [SeqID17] => [SeqID18] => [SeqID19] => [SeqID20] => [SeqID21] => [SeqID22] => [SeqID23] => [SeqID24] => [SeqID25] => [SeqID26] => [SeqID27] => [SeqID28] => [SeqID29] => [SeqID30] => [SeqID31] => [SeqID32] => [SeqID33] => [SeqID34] => [SeqID35] => [SeqID36] => [SeqID37] => [SeqID38] => [SeqID39] => [SeqID40] => [SeqID41] => [SeqID42] => [SeqID43] => [SeqID44] => [SeqID45] => [SeqID46] => [SeqID47] => ) ) 

As you can see if a question has been answered the column contains a "1", as in [SeqID1] => 1 [SeqID2] => 1.

All of the above works, it may not be the right way to do it but it works.

Now this is where I have an issue. In the body of the page I display the list of questions, row by row.

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<? do { ?>
	<? $_SESSION['TableID'] = $row_SeqList['TableID'];?>
	<tr>
	<td width="71%" class="tablelist"><?php print $row_SeqHead['SeqHeader']; ?></td>
	<td width="7%" class="tablelist"><?php print $row_SeqHead['TableID']; ?></td>
	<td width="16%" class="openbutton">

<?php
foreach($Seq as $key => $value) {
	foreach ($Seq as $val) {
		if ($val == 1){ ?>
			<img src="images/completebutton.png" width="72" height="29">
		<? } else { ?>
			<a href="table01.php?TableID=<?php echo $row_SeqHead['TableID']; ?>" target="_self"><img src="images/openbutton.png" width="72" height="29"></a>
		<?php }
	}
}?>
</td>
</tr>
<?php } while ($row_SeqHead = mysqli_fetch_assoc($SeqHead)); ?>
</table>

The problem I have is if the question has been answered the question row is still displaying the open button and not the closed image.

How can I use the content of array $Seq to display the correct open button or closed image.

Or should I be doing this a completely different way. Your help and comments would be great.

Many thanks in advance for your time.

question_list.png

    The problem is the two nested foreach(){} loops. The second loop is looping over the first array, not the nested array.

    You have way over complicated this. If you store the data properly, all you need to accomplish this is one query, to retrieve the data you want in the order that you want it, then just loop over that data and produce the output that you want.

    You have created multiple tables, holding same structure data, and you have created multiple columns in those tables, holding same meaning data. Both of these things are a bad design, that requires a lot of extra code and queries to manage (insert, update, delete, or find and display) any of the data.

    When you dynamically add data, you should be adding rows to a table, not altering the table structure adding columns. Based on what you have shown, you should have TWO database tables. One to hold the questions and a second one to hold the related answers. Whatever the $_SESSION['NX] value represents should be stored in a (one) column in one table, not selecting between multiple tables. Each question should be a row in a table, not a column.

      Hi pbismad,
      Thanks for your reply.

      Each user group has thier own ItemCategory table and the table only contains questions related to thier group.

      The ItemCategory data table can be changed by the user admin. They must be able to create thier own fields containing thier own questions. This is why I am using the Information Schema to return the columns, making sure that any queries are constructed with the correct columns.

      It may look and read complete odd but thats the way I need to construct the scripts.

      With regard to the nested foreach loops can you see any way of constructing them within the do while loop?

      Many thanks for any help you can provide.

        I looked at some of your previous threads, and this is not the first time a forum member has told you to not create a series of columns. You are trying to store the data like it is in a spread-sheet. A spread-sheet is a human convention to let humans see data spread out in front of them. This is not how RDBMS systems are coded. It is inefficient from a data storage standpoint and it requires a ton of unnecessary code and queries.

        Defining a list of questions, just involves storing that list, with one row for each question, in a questions table. To support multiple sets of questions in the single table, there would be a category/id column that would have the same value for all the related questions in each set.

          Hi pbismad, I have taken on board all your comments along with comments from others. I have completely rewritten my tables. queries and scripts to make sure that I am following advice given.
          I am very pleaed to say that the new designs are working perfectly. I would like to thank you for your input.

          Regard.

            7 days later
            Write a Reply...