Page 1 of 1

Preventing functions from executing at the end

Posted: Wed May 25, 2005 7:52 am
by hwttdz
My problem is that I'm using a function and when the php is converted to html the html generated by the function appears an extra time at the end where I gave the function definition. How can I prevent this from happening?

Posted: Wed May 25, 2005 7:56 am
by hawleyjr
Please post your code.

Posted: Wed May 25, 2005 7:56 am
by shiznatix
post your code.

what i think you are trying to say is that what the function returns is beging outputted 2 times instead of 1? maybe you are echoing it out in the function then echoing out the fuction which in turn out echo the information out twice?

Posted: Wed May 25, 2005 8:06 am
by hwttdz

Code: Select all

<?php
$link = @mysql_connect('localhost', 'root', 'password') or 
    die("Could not connect to MySQL");

$db = @mysql_select_db('temp1',$link) or 
    die("Could not select database");

$query = "SELECT * FROM table1"; 
$result = @mysql_query($query,$link) or 
    die("Could not submit query");
$numrows = @mysql_num_rows($result);

for ($i=0; $i<$numrows; $i++) {
    $jones = mysql_fetch_array($result);
    echo ("Location: "."$jones[location]");
    dropmenu();
    ?><?php
}


function dropmenu()
{
?>
<form action="#" method="post">
<SELECT NAME="State" SIZE="1">
  <OPTION VALUE="1">ACTIVE</OPTION>
  <OPTION VALUE="2">BROKEN</OPTION>
  <OPTION VALUE="3"">PLANNED</OPTION>
</SELECT>
<input type="submit" value="crazy stuff"/>
</form>
<?php
}
dropmenu();

?>

Posted: Wed May 25, 2005 8:08 am
by hwttdz
The result I'm getting has one more drop down menu as generated by the dropmenu function not after the output from the database.

Posted: Wed May 25, 2005 8:08 am
by JayBird
why even have it in a function?

If you do need it in a function for some reason

Code: Select all

<?php
$link = @mysql_connect('localhost', 'root', 'password') or 
    die("Could not connect to MySQL");

$db = @mysql_select_db('temp1',$link) or 
    die("Could not select database");

$query = "SELECT * FROM table1"; 
$result = @mysql_query($query,$link) or 
    die("Could not submit query");
$numrows = @mysql_num_rows($result);

for ($i=0; $i<$numrows; $i++) {
    $jones = mysql_fetch_array($result);
    echo ("Location: "."$jones[location]");
    dropmenu();
    ?><?php
}


function dropmenu()
{
echo '
<form action="#" method="post">
<SELECT NAME="State" SIZE="1">
  <OPTION VALUE="1">ACTIVE</OPTION>
  <OPTION VALUE="2">BROKEN</OPTION>
  <OPTION VALUE="3"">PLANNED</OPTION>
</SELECT>
<input type="submit" value="crazy stuff"/>
</form>';
}
dropmenu();
?>

Posted: Wed May 25, 2005 8:13 am
by hwttdz
I'm sorry I'm new, can you explain what you've changed and why. It looks like only dropmenu() has changed. It's still there. What in c++ I think I would say that my function definition is executing.

Posted: Wed May 25, 2005 12:07 pm
by Skara
function definitions don't exist in php. When you put dropmenu(); there, it's executing it. ;)

Posted: Wed May 25, 2005 1:00 pm
by phpScott
you did call dropDown twice line 16 and line 34.