Functions being show at the top of the page.

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
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Functions being show at the top of the page.

Post by Domsore »

So this is the code I have:

Code: Select all

<?php
/**
 * Description of registerClass
 *
 * @author Dominic Sore
 */
class registerClass {

    public function getPlatform(){
	$result = mysql_query('SELECT * FROM sys_platform') OR die(mysql_error());
	$template = mysql_fetch_array($result) OR die(mysql_error());
    }

    public function incCountries(){
	include_once(TPATH_BASE.DS.'components'.DS.'componentControllers'.DS.'register'.DS.'registerCountries.php');
    }

    public function dobFunc(){
	$Tyear = date(Y);
	$year = $Tyear - 18;

	echo '<select id="user_dob" name="user_dob">';

	for($i=0; $i<=80; $i++){
	    echo '<option value="'.$year.'">'.$year.'</option>';
	    $year = $year - 1;
	}

	echo '</select>';

    }

    public function startForm(){
	echo '<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="post" enctype="multipart/form-data" name="register">';
    }

    public function endForm(){
	echo '</form>';
    }

    public function createPersonal(){
	echo '<div id="inner_box">
<div id="inner_header">Personal Info</div>
<div id="inner_content">
<table width="300" border="0">
  <tr>
    <td width="121">First Name:</td>
    <td width="195">
      <input type="text" name="user_fname" id="user_fname" />
   	</td>
  </tr>
  <tr>
    <td>Last Name:</td>
    <td><input type="text" name="user_lname" id="user_lname" /></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" name="user_email" id="user_email" /></td>
  </tr>
  <tr>
    <td>Confirm Email:</td>
    <td><input type="text" name="user_cemail" id="user_cemail" /></td>
  </tr>
  <tr>
    <td>Gender:</td>
    <td>
      <select name="user_gender" id="user_gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
      </select>
 	</td>
  </tr>
  <tr>
    <td>Birth Year:</td>
    <td>'.$this->dobFunc().'</td>
  </tr>
  <tr>
    <td>Street Address:</td>
    <td><input type="text" name="user_street" id="user_street" /></td>
  </tr>
  <tr>
    <td>City:</td>
    <td><input type="text" name="user_city" id="user_city" /></td>
  </tr>
  <tr>
    <td>Country:</td>
    <td>'.$this->incCountries().'</td>
  </tr>
  <tr>
    <td>Post Code:</td>
    <td><input type="text" name="user_pcode" id="user_pcode" /></td>
  </tr>
</table>

</div>
</div>';
    }
}
?>
But this is what is happening?
Image
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Functions being show at the top of the page.

Post by anantha »

Code: Select all

<td>'.$this->dobFunc().'</td>
change to

Code: Select all

<td>';echo $this->dobFunc(); echo '</td>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Functions being show at the top of the page.

Post by John Cartwright »

That won't work, because it's being output already.

What you want to do is change your dobFunc() to return the values, instead of echo'ing it.

Code: Select all

public function dobFunc(){
        $Tyear = date(Y);
        $year = $Tyear - 18;
        
        $html = '<select id="user_dob" name="user_dob">';

        for($i=0; $i<=80; $i++){
            $html .= '<option value="'.$year.'">'.$year.'</option>' . PHP_EOL;
            $year = $year - 1;
        }

        $html .= '</select>';
        
        return $html;
    }
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Functions being show at the top of the page.

Post by Domsore »

John... you know so much. This worked great. Praise John :)

Cheers again.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Functions being show at the top of the page.

Post by John Cartwright »

Domsore wrote:Praise John
Some things you just can't hear enough of :D Joking aside, no praise necessary. Just use what you've learned today for good and not evil.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Functions being show at the top of the page.

Post by anantha »

if u remove the echo for the $this->dobFunc() it should work....

like this

Code: Select all

<td>';$this->dobFunc(); echo '</td>

it should work..i should have not put echo there....instead of changing all your echo to return...u can just do this...
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Functions being show at the top of the page.

Post by Domsore »

Thanks for you reply, however I have already changed it to return and it worked perfectly.

Cheers though,

Dom
Post Reply