PHP/SQL - Two While Statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
unifiedac
Forum Newbie
Posts: 4
Joined: Tue Mar 22, 2011 11:34 am

PHP/SQL - Two While Statements

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP/SQL - Two While Statements

Post 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?
vishal_arora
Forum Newbie
Posts: 8
Joined: Thu Mar 24, 2011 5:08 am

Re: PHP/SQL - Two While Statements

Post by vishal_arora »

Merge two arrays first using array_merge function than used as u want
Post Reply