Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
sergei
Forum Commoner
Posts: 33 Joined: Mon Oct 06, 2003 4:17 am
Post
by sergei » Tue Nov 04, 2003 1:44 am
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
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Nov 04, 2003 2:56 am
Can we see the bit of code where you call the functions to build the form?
Mac
sergei
Forum Commoner
Posts: 33 Joined: Mon Oct 06, 2003 4:17 am
Post
by sergei » Tue Nov 04, 2003 3:54 am
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> </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>
");
}
?>
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Nov 04, 2003 4:27 am
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> </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
sergei
Forum Commoner
Posts: 33 Joined: Mon Oct 06, 2003 4:17 am
Post
by sergei » Tue Nov 04, 2003 4:40 am
Code: Select all
Parse error: parse error, unexpected T_SL, expecting ',' or ';' on line 72
I'm getting this error. It refers to
What does END do ?
sergei
Forum Commoner
Posts: 33 Joined: Mon Oct 06, 2003 4:17 am
Post
by sergei » Tue Nov 04, 2003 5:05 am
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.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Nov 04, 2003 5:18 am
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
sergei
Forum Commoner
Posts: 33 Joined: Mon Oct 06, 2003 4:17 am
Post
by sergei » Tue Nov 04, 2003 5:39 am
Thanks. I finally got it working. Checked the whitespace.
Enjoy the rest of your day.
Thanks again !!!