Forum Moderators: coopster

Message Too Old, No Replies

Function Calls from within an HTML Table

This is annoying for me, but simple for most...

         

djburg42

3:13 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



Hi again all...

I know this can be done, and I obviously have something simple wrong here, but I am trying to call a function to populate a dropdown box on a form. This is an edit page and the dropdown may already have a value from the DB in some cases. So the function will address that. Problem here is , that for some reason I can't even get the function to call. The table code for this particular area is as follows:

<?php

Some code...

<tr>
<td colspan="2"><label for="favline">Favorite Line:</label></td>
<td colspan="2"><name="favline" id="favline"/> displayvalues(favline,$FavLine,DBLine,Line) </td>
</tr>

...More code...

?>

This is the function being called...

function displayvalues($dropdownname,$dropdownvalue,$tablename,$columnname)
{
db_connect();

$result = mysql_query("Select * from " . $tablename . mysql_error());

if (!$result)
{
die('Invalid formation of select query in displayvalues(): ' . mysql_error());
}

echo "<select name=" . $dropdownname . ">";

while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
if ($row[0] == $dropdownvalue)
{
echo "<option value=" . $row[1] . " SELECTED>" . $row[1] . "</option>";
}
else
{
echo "<option value=" . $row[1] . ">" . $row[1] . "</option>";
}
}

echo "</select>";

mysql_free_result($result);

mysql_close();
}

?>

When this displays on the page, it actually just prints the call to the function...
Favorite Line: displayvalues(favline,$FavLine,CruiseLine,Line)

So, any help or pointers would be greatly appreciated.

RobinC

4:22 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



Try -

<td colspan="2"><name="favline" id="favline"/> <?php displayvalues(favline,$FavLine,DBLine,Line);?> </td>

Robin

djburg42

4:36 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



I thought you had something there for a minute. I noticed I missed the ";" at the end, but that wasn't it either. I didn't need the <?php and?> as the entire section is inside of the php handles.

Back to square one...

encyclo

4:47 pm on Jul 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't just place raw HTML markup inside a PHP code block - you will either need to close the code block before the <tr> and use another block for the function in the HTML, or you need to echo the entire HTML block.

Something like:

<?php 

Some code...

<tr>
echo "<td colspan=\"2\"><label for=\"favline\">Favorite Line:</label></td>\n";
echo "<td colspan=\"2\"><name=\"favline\" id=\"favline\"/>".displayvalues(favline,$FavLine,DBLine,Line)."</td>\n";
echo "</tr>\n";

...More code...

?>

(Note: above sample done fast, not tested!)

djburg42

12:15 am on Jul 27, 2004 (gmt 0)

10+ Year Member



OK, I'm fairly new to this so now I'm a bit confused. Currently, the page has HTML code inside the PHP handles and I currently populate a text box with a variable out of the DB through the query.

<tr>
<td colspan="2"><label for="favline">Favorite Line:</label></td>
<td colspan="2"><input type="text" name="favline" id="favline" size="15" value="' . $row['FavLine'] . '" /></td>
</tr>

This works, but when I remove the "input type" and try to call the function, it fails. I guess my better question is why does it work this way yet the function call doesn't work?

ergophobe

12:29 am on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A little hard to tell, but I'm guessing that you are basically using a big long string that includes html and php. PHP will od some string substitution, so some variables may work, but you can't have a function in a quoted string.

Good:
<?
echo '<html><body><h1>' . get_page_title($page_id) . '</h1></body></html>';
?>

Good:

<html><body><h1>
<? echo get_page_title($page_id);?>
</h1></body></html>

Bad:

<?
'<html><body><h1>' . get_page_title($page_id) . '</h1></body></html>';
?>

Bad:

<?
<html><body><h1>
echo get_page_title($page_id)
</h1></body></html>
?>