Preventing functions from executing at the end

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
hwttdz
Forum Newbie
Posts: 7
Joined: Wed May 25, 2005 7:48 am

Preventing functions from executing at the end

Post 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?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Please post your code.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
hwttdz
Forum Newbie
Posts: 7
Joined: Wed May 25, 2005 7:48 am

Post 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();

?>
hwttdz
Forum Newbie
Posts: 7
Joined: Wed May 25, 2005 7:48 am

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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();
?>
hwttdz
Forum Newbie
Posts: 7
Joined: Wed May 25, 2005 7:48 am

Post 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.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

function definitions don't exist in php. When you put dropmenu(); there, it's executing it. ;)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

you did call dropDown twice line 16 and line 34.
Post Reply