Page 1 of 1

PHP/SQL - Two While Statements

Posted: Wed Mar 23, 2011 5:34 pm
by unifiedac
Hello,

I have two while statements that pull in two comma separated lists. One of the lists is actual html code that generates form fields. The other list are the labels for the form fields, customized by user input. Since both arrays are looping separately, I am unable to place the relevant label next to its form field.

The form fields and labels will always match. Here is the code so far. I don't expect to get an answer straight away, but would like to start the dialogue. Thanks!

Code: Select all

	//Retrieve storyID from home page hyperlink
	$storyID = $_GET['storyID'];

	//Database details

	//Get form field IDs from the_story table, based on storyID
	$formFields = mysql_query("
		SELECT story_fields 
		FROM the_story
		WHERE ID = $storyID
		");
	
	while($rowField = mysql_fetch_array($formFields))
  	{
  		//$myFields returns comma separated form field IDs (1,2,3,4,etc.)
		$myFields = $rowField['story_fields'];
  	}

	//Parse form fields into separate values and associate with actual HTML from form_fields table
	$arrField = explode(",",$myFields);  
	foreach($arrField as $f)	
	{  
		$singleField = trim($f);
		$placeField = mysql_query("
			SELECT field_html 
			FROM form_fields
			WHERE ID = $singleField
			");
		
		while($rowField = mysql_fetch_array($placeField))
		{
			//echo individual form field html
			echo $rowField['field_html'];
			echo "</br>";
		}
	}	
	//Get form field labels based on storyID
	$formLabels = mysql_query("
		SELECT field_labels 
		FROM the_story
		WHERE ID = $storyID
		");
	while($rowLabel = mysql_fetch_array($formLabels))
  	{
  		$myLabels = $rowLabel['field_labels'];
  	}
	//Parse form field labels
	$labels = $myLabels;
	$arrLabel = explode(",",$labels);  
	foreach($arrLabel as $l)
	{  
		$newLabel = trim($l);
		echo $newLabel;		
		echo "</br>";
	}
Here is how the current code displays and below it is how I would like it to appear:

http://www.brendonbarnett.com/temp/MyStory/test.html

Re: PHP/SQL - Two While Statements

Posted: Wed Mar 23, 2011 6:13 pm
by Jonah Bron
Not sure, but are you asking how to merge the two loops so that the labels and fields can be echoed together, in sync?

Re: PHP/SQL - Two While Statements

Posted: Thu Mar 24, 2011 5:39 am
by vishal_arora
Merge two arrays first using array_merge function than used as u want