Page 1 of 1

Help !!! Populating drop-downs

Posted: Tue Nov 04, 2003 1:44 am
by sergei
I have the following function.

Code: Select all

<?
 function DynamicMenu() {
		$selectstatement = "SELECT Category_ID, Category_Type FROM category";
		$selectresult = mysql_query($selectstatement) or die(mysql_error());			
		echo("<select name="Category_ID">");
		while ($row = mysql_fetch_array($selectresult)) { 
			$Category_Type = $row['Category_Type'];			
			$id = $row['Category_ID'];
			if ($id == 1) {
				echo("<option SELECTED value="$id">".$row['Category_Type']."</option>"); 
			} else {
				echo("<option value="$id">".$row['Category_Type']."</option>"); 
			}
		} 
		echo("</select>");
	}	
?>
When I call this function inside a form, it doesn't display. I'm guessing that this is because the form gets processed before the function.

BTW, The form is handed inside a function as well, because I need to easily display and change it on various pages.

How can I have these drop downs process and display when the form loads ?

Thanks

Posted: Tue Nov 04, 2003 2:56 am
by twigletmac
Can we see the bit of code where you call the functions to build the form?

Mac

Posted: Tue Nov 04, 2003 3:54 am
by sergei

Code: Select all

<?

function AddTables() {
		
		echo("<h5 class="style1"><a name="add"></a>Add Articles </h5>
			<form name="addarticles" method="post" action="addarticle.php">
			<table width="95%" border="0">
			  <tr>
				<td width="31%">Article Name </td>
				<td width="69%"><input name="Article_Name" type="text" id="Article_Name"></td>
				</tr>
			  <tr>
				<td>Article Author </td>
				<td><input name="Article_Author" type="text" id="Article_Author"></td>
				</tr>
			  <tr>
				<td>Issue Number </td>
				<td><input name="Issue_Number" type="text" id="Issue_Number"></td>
				</tr>
			  <tr>
				<td>Expiry Date </td>
				<td><input name="Expiry_Date" type="text" id="Expiry_Date"></td>
				</tr>
			  <tr>
				<td>Reference Number </td>
				<td><input name="Reference_number" type="text" id="Reference_number"></td>
				</tr>
			  <tr>
				<td>Category ID </td>
				<td><?php 
						
					DynamicMenu();
				
					?></td>
				</tr>
			  <tr>
				<td>Year</td>
				<td><input name="Year" type="text" id="Year"></td>
			  </tr>
			  <tr>
				<td>Short Description Text </td>
				<td><textarea name="Intro" cols="40" rows="8" wrap="VIRTUAL" id="Intro"></textarea></td>
			  </tr>
			  <tr>
				<td>Full Story Text </td>
				<td><textarea name="Content" cols="40" rows="8" id="Content"></textarea></td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
				<td><input name="submit" type="submit" id="submit" value="Add Article">
				  <input name="reset" type="reset" id="reset" value="Reset"></td>
			  </tr>
			</table>
			</form>   
			");
	}


?>

Posted: Tue Nov 04, 2003 4:27 am
by twigletmac
Basically you can't put a call to a function within an echo like that, try instead something like this for AddTables():

Code: Select all

function AddTables() {

	$menu = DynamicMenu();

	echo <<<END

<h5 class="style1"><a name="add">Add Articles</a></h5>

<form name="addarticles" method="post" action="addarticle.php">
<table width="95%" border="0">
<tr>
	<td width="31%">Article Name</td>
	<td width="69%"><input name="Article_Name" type="text" id="Article_Name" /></td>
</tr>
<tr>
	<td>Article Author </td>
	<td><input name="Article_Author" type="text" id="Article_Author" /></td>
</tr>
<tr>
	<td>Issue Number</td>
	<td><input name="Issue_Number" type="text" id="Issue_Number" /></td>
</tr>
<tr>
	<td>Expiry Date </td>
	<td><input name="Expiry_Date" type="text" id="Expiry_Date" /></td>
</tr>
<tr>
	<td>Reference Number </td>
	<td><input name="Reference_number" type="text" id="Reference_number" /></td>
</tr>
<tr>
	<td>Category ID</td>
	<td>$menu</td>
</tr>
<tr>
	<td>Year</td>
	<td><input name="Year" type="text" id="Year" /></td>
</tr>
<tr>
	<td>Short Description Text</td>
	<td><textarea name="Intro" cols="40" rows="8" wrap="VIRTUAL" id="Intro"></textarea></td>
</tr>
<tr>
	<td>Full Story Text</td>
	<td><textarea name="Content" cols="40" rows="8" id="Content"></textarea></td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td>
		<input name="submit" type="submit" id="submit" value="Add Article" />
		<input name="reset" type="reset" id="reset" value="Reset" />
	</td>
</tr>
</table>
</form>
END;
   }
Read here about heredoc (<<<END END; format) here:
http://php.net/manual/en/language.types ... ax.heredoc

and for DynamicMenu() try something like this:

Code: Select all

function DynamicMenu() {
	$selectstatement = "SELECT Category_ID, Category_Type FROM category";
	$selectresult    = mysql_query($selectstatement) or die(mysql_error());

	$html = '<select name="Category_ID">';

	while ($row = mysql_fetch_array($selectresult)) {
		$Category_Type = $row['Category_Type'];         
		$id            = $row['Category_ID'];

		if ($id == 1) {
			$html .= '<option SELECTED value="'.$id.'">'.$row['Category_Type'].'</option>';
		} else {
			$html .= '<option value="'.$id.'">'.$row['Category_Type'].'</option>';
		}
	}

	$html .= '</select>';

	return $html;
}
Mac

Posted: Tue Nov 04, 2003 4:40 am
by sergei

Code: Select all

Parse error: parse error, unexpected T_SL, expecting ',' or ';' on line 72
I'm getting this error. It refers to

Code: Select all

echo <<<END
What does END do ?

Posted: Tue Nov 04, 2003 4:44 am
by twigletmac
Please read the info provided here:
http://php.net/manual/en/language.types ... ax.heredoc

Could you show the couple of lines above where you're getting the parse error, as the code I posted works fine (no parse errors) for me.

Mac

Posted: Tue Nov 04, 2003 5:05 am
by sergei
I took what you had, made sure that the END; was in line with the first column and just called the function.

Nothing else was added or deleted.

Posted: Tue Nov 04, 2003 5:18 am
by twigletmac
That's really strange because I cut and paste the code out of the forum to test it and it works fine (except for the fact that it can't connect to your database).

Did you check to make sure there weren't any spaces after <<<END as that would cause the error you're getting.

Mac

Posted: Tue Nov 04, 2003 5:39 am
by sergei
Thanks. I finally got it working. Checked the whitespace.

Enjoy the rest of your day.

Thanks again !!! :lol: