Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a table containing 2 loops 1

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
ZA
I need to create an html table that conatains the data from 2 loops. The first column is the questions from the database and the second column is the answers to those relative questions. As the are 1-n questions, it can't be hardcoded.

Problem is I either get the questions 10 times over to every answer(that's how many there are in this case) or I get the answers 10 times over to every question.

Code:
<?php

session_start();
?>
<html>
<link rel="stylesheet" href="styles.css" type="text/css">
<body bgcolor="#ffffff">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr align="center">
    <td valign="top"><div align="center"><img src="images/logo_order.gif" alt="Order Page"></div></td>
  </tr>
</table>
<p>
<?php
require_once('../../config.php');
$song=$_GET['song_id'];

//this is where you select the songs details via the song id
$sql = mysql_query("SELECT * FROM song where song_id = $song"); 

// go through each row in the result set and display data from the song table
while ($row = mysql_fetch_array($sql)) {
	// give a name to the fields
	$questions = $row['song_questions'];
	$song_cat = $row['song_category_id'];
	$song_name = $row['song_title'];
	

//we explode the data that is kept in the blob file into a series of seperate arrays
$quest = explode(",", $questions);


}
//This is to see the category of the song
$sql3 = mysql_query("SELECT * FROM category where category_id = '$song_cat'"); 
while ($row = mysql_fetch_array($sql3)) {
	// give a name to the fields
	$category = $row['category_title'];
	}
print "<center><span class=\"lightbluebig\">Welcome to our Shopping Cart</span><p>
<span class=\"font10\">You have chosen the following song:</span><br>
<font class=\"redbig\"><b>$category - $song_name</b></font><br>
<span class=\"font10\">To purchase this item<br>
Please enter the following information:<br></span><p>

<form method=\"post\" action=\"checkoutb.php\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"10\">

<input type=\"hidden\" value=\"$song\" name=\"songid\">

";

//we determine starting row colour here
$row_col = 1;
$questname = 1;

//we loop through the seperate arrays and perform the following sql statement
foreach($quest as $quest2){

//sql statement selects all the fields from the question table where the question id equals the seperate arrays
$sql2 = mysql_query("SELECT * FROM question where question_id=$quest2"); 	



//executes the sql2 statement 
while ($row2 = mysql_fetch_array($sql2)) {

// give a name to the fields
$_SESSION[quest3] = $row2['question_question'];
$quest4 = $row2['question_type'];
$quest5 = $row2['question_selections'];
$_SESSION[questamount] = count($row2);


//this is where we alternate row colours
if ($row_col == 1){
	$color = ffffff;
	$row_col = 2;
}elseif ($row_col == 2){
	$color = eeeeee;
	$row_col = 1;
}

// Build your formatted results here. 
    echo" <tr bgcolor=\"$color\"><td width=\"250\" class=\"font11\">$_SESSION[quest3]</td>";
	if ($quest4 == 0){
		print "<td><input type=\"text\" name=\"question[$questname]\"></td>";
	}elseif ($quest4 == 1){
		$select = explode(",", $quest5);
		
		print "<td><select name=\"question[$questname]\">";
		
		foreach($select as $select2){
		echo" <option value=\"$select2\">$select2</option>
		";
		
	}
	print "</select></td>";
		
		
		
	}elseif ($quest4 == 2){
		print "<td><input type=\"text\" name=\"question[$questname]\"></td>";
	}
	print "</tr>
	
	";
	
 
}$questname++;
}
print "<tr><td colspan=\"2\" align=\"center\"><input type=\"Submit\" name=\"submit\" value=\"Proceed\"></table></form></center><br>";
?> 
</body>
</html>

and the confirmation page with the answers relevant to the questions in the table...
Code:
<?php

session_start();
?>

<?


require_once('../../config.php');
$song=$_GET['song_id'];

//this is where you select the songs details via the song id
$sql = mysql_query("SELECT * FROM song where song_id = '$_POST[songid]'"); 

// go through each row in the result set and display data from the song table
while ($row = mysql_fetch_array($sql)) {
	// give a name to the fields
	$questions = $row['song_questions'];
	$song_cat = $row['song_category_id'];
	$song_name = $row['song_title'];
	

//we explode the data that is kept in the blob file into a series of seperate arrays
$quest = explode(",", $questions);


}
//This is to see the category of the song
$sql3 = mysql_query("SELECT * FROM category where category_id = '$song_cat'"); 
while ($row = mysql_fetch_array($sql3)) {
	// give a name to the fields
	$category = $row['category_title'];
	}
print "<center><span class=\"lightbluebig\">Welcome to our Shopping Cart</span><p>
<span class=\"font10\">You have chosen the following song:</span><br>
<font class=\"redbig\"><b>$category - $song_name</b></font><br>
<span class=\"font10\">To purchase this item<br>
Please enter the following information:<br></span><p>

<form method=\"post\" action=\"checkoutb.php\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"10\">

<input type=\"hidden\" value=\"$song\" name=\"songid\">

";

//we determine starting row colour here
$row_col = 1;
$questname = 1;

//we loop through the seperate arrays and perform the following sql statement
foreach($_POST[question] as $value){
	$confirm = "<td>$value</td></tr>";
}
foreach($quest as $quest2){

	
//sql statement selects all the fields from the question table where the question id equals the seperate arrays
$sql2 = mysql_query("SELECT * FROM question where question_id=$quest2"); 	

//executes the sql2 statement 
while ($row2 = mysql_fetch_array($sql2)) {

	
	
// give a name to the fields
$_SESSION[quest3] = $row2['question_question'];
$quest4 = $row2['question_type'];
$quest5 = $row2['question_selections'];
$_SESSION[questamount] = count($row2);


//this is where we alternate row colours
if ($row_col == 1){
	$color = ffffff;
	$row_col = 2;
}elseif ($row_col == 2){
	$color = eeeeee;
	$row_col = 1;
}

// Build your formatted results here. 
    echo" <tr bgcolor=\"$color\"><td width=\"250\" class=\"font11\">$_SESSION[quest3]</td>$confirm";
	
 }
}$questname++;

print "<tr><td colspan=\"2\" align=\"center\"><input type=\"Submit\" name=\"submit\" value=\"Proceed\"></table></form></center><br>";
?>

Reality is built on a foundation of dreams.
 
okay, made the code a bit clearer but still get the repeated values.

This is the form:
Code:
<?php

session_start();
?>
<html>
<link rel="stylesheet" href="styles.css" type="text/css">
<body bgcolor="#ffffff">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr align="center">
    <td valign="top"><div align="center"><img src="images/logo_order.gif" alt="Order Page"></div></td>
  </tr>
</table>
<p>
<?php
require_once('../../config.php');
$song=$_GET['song_id'];

//this is where you select the songs details via the song id
$sql = mysql_query("SELECT * FROM song where song_id = $song"); 

// go through each row in the result set and display data from the song table
while ($row = mysql_fetch_array($sql)) {
	// give a name to the fields
	$questions = $row['song_questions'];
	$song_cat = $row['song_category_id'];
	$song_name = $row['song_title'];
	

//we explode the data that is kept in the blob file into a series of seperate arrays
$quest = explode(",", $questions);


}
//This is to see the category of the song
$sql3 = mysql_query("SELECT * FROM category where category_id = '$song_cat'"); 
while ($row = mysql_fetch_array($sql3)) {
	// give a name to the fields
	$category = $row['category_title'];
	}
print "<center><span class=\"lightbluebig\">Welcome to our Shopping Cart</span><p>
<span class=\"font10\">You have chosen the following song:</span><br>
<font class=\"redbig\"><b>$category - $song_name</b></font><br>
<span class=\"font10\">To purchase this item<br>
Please enter the following information:<br></span><p>

<form method=\"post\" action=\"checkoutc.php\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"10\">

<input type=\"hidden\" value=\"$song\" name=\"songid\">

";

//we determine starting row colour here
$row_col = 1;
$questname = 1;

//we loop through the seperate arrays and perform the following sql statement
foreach($quest as $quest2){

//sql statement selects all the fields from the question table where the question id equals the seperate arrays
$sql2 = mysql_query("SELECT * FROM question where question_id=$quest2"); 	



//executes the sql2 statement 
while ($row2 = mysql_fetch_array($sql2)) {

// give a name to the fields
$quest3 = $row2['question_question'];
$quest4 = $row2['question_type'];
$quest5 = $row2['question_selections'];
$_SESSION[questamount] = count($row2);


//this is where we alternate row colours
if ($row_col == 1){
	$color = ffffff;
	$row_col = 2;
}elseif ($row_col == 2){
	$color = eeeeee;
	$row_col = 1;
}

// Build your formatted results here. 
    echo" <tr bgcolor=\"$color\"><td width=\"250\" class=\"font11\">$quest3<input type=\"hidden\" name=\"q[$questname]\" value=\"$quest3\"></td>";
	if ($quest4 == 0){
		print "<td><input type=\"text\" name=\"question[$questname]\"></td>";
	}elseif ($quest4 == 1){
		$select = explode(",", $quest5);
		
		print "<td><select name=\"question[$questname]\">";
		
		foreach($select as $select2){
		echo" <option value=\"$select2\">$select2</option>
		";
		
	}
	print "</select></td>";
		
		
		
	}elseif ($quest4 == 2){
		print "<td><input type=\"text\" name=\"question[$questname]\"></td>";
	}
	print "</tr>
	
	";
	
 
}$questname++;
}
print "<tr><td colspan=\"2\" align=\"center\"><input type=\"Submit\" name=\"submit\" value=\"Proceed\"></table></form></center><br>";
?> 
</body>
</html>

and this is supposed to be the confirmation page:
Code:
<?php

session_start();
?>

<?


require_once('../../config.php');
Print "<table>";

foreach($_POST[q] as $questionconf){
	foreach($_POST[question] as $answerconf){
		print "<td>$answerconf</td></tr>";
	}
	print "<tr><td>$questionconf</td>";
}

	
print "</table>";
	
?>

Reality is built on a foundation of dreams.
 
This is my take on what you need to do.
Code:
<?
	$HTML = "<table>";
	$query = mysql_query("SELECT question FROM questiontable");
	while( $option = mysql_fetch_array( $query )){
		$question = $option['question'];
		$query = mysql_query("SELECT answer FROM answertable WHERE question = '$question'");
		while( $option = mysql_fetch_array( $query )){
			$answer = $option['answer'];
		}		
		$qahtml .= "
			<tr>
				<td>$question</td>
				<td>$answer</td>
			</tr>
		";
	}
	$HTML .= $qathtml . "</table><br><br>";
	echo $HTML;
?>

Sera
I often believe that...
My computer is possessed!
 
fantastic - thank-you

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top