assigning a function to a variable...kinda

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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

assigning a function to a variable...kinda

Post by shiznatix »

ok i have a huge function that displays a large amount of data, but when i call the function it echos out all of the information which is what i need SOMTIMES. but i now need to email the results of the function to someone so i tried

Code: Select all

$message = show_cv($user_id, '2');
mail ($to, $subject, $message);
but that just sends a blank page. and i know its because in the function show_cv i echo out all the information so when i assign it to a variable it auto echos out everything instead of saving a copy of it per say. so my question is:

how to i copy all of the information echoed out by my function in a variable that i can then email to a user.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

to assign the var you need to "return" something:

instead of echoing everythign in the function set a var to it:

Code: Select all

function bob(){
  $thisVar = "this is my first line";
  $thisVar .= "this is my second line";
  $thisVar .= "this is my third line";
  return $thisVar;
}

$larry = bob();

mail($to,$subject,$larry);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

rewriting the function is not a option. sorry i should have said that at first.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

hmm...dunno then, seems like there's got be a way to capture what is outputted, but I don't know it.

Sorry, hope I didn't waste this thread for you.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Burrito wrote:hmm...dunno then, seems like there's got be a way to capture what is outputted, but I don't know it.
exactlly, i dont see a reason why that cant be done but i just dont know of how to do it but no, you didnt waste my time you just helped me realize i need to think my code through more :wink: . but who here does know how to do that?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

this might be what you're looking for: http://php.net/ob_start
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

negative.

heres what happens

Code: Select all

$message = show_cv($some_id);
//automatically since i even wrote show_cv() with a real id in it is echos out the cv i entered. so right there it makes the mail function not send the cv info because when i do...
mail($to, $subject, echo'the cv info');
//well that obviously is incorrect and thats what basically being entered into the mail function
and thats whats going on so i need to capture what is outputted fromthe show_cv function into a variable and send that with mail(). and i know ob_start() dosnt work cause i have that at the top of my page right now because of headers :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

heres my code... i think i have explained my probelem enough but maybe this might help

Code: Select all

//****************************************//
    // display the users personal information //
    //****************************************//
    ?>
    <table border="0">
      <tr>
        <td width="125">
    <? $lang->DoLcV('NAME'); ?>
        </td>
        <td>
    <? $personal_info[first_name] = sanitize_html_string($personal_info[first_name]); ?>
    <? echo $personal_info[first_name]; ?>
    &nbsp
    <? $personal_info[last_name] = sanitize_html_string($personal_info[last_name]); ?>
    <? echo $personal_info[last_name]; ?>
        </td>
      </tr>
    <?
    if ($personal_info[birthday] != '0000-00-00')
    {
    ?>
          <tr>
            <td>
        <? $lang->DoLcV('FORM_BIRTHDAY'); ?>
            </td>
            <td>
        <? $personal_info[birthday] = sanitize_html_string($personal_info[birthday]); ?>
        <? echo $personal_info[birthday]; ?>
            </td>
          </tr>
    <?
    }
    if ($personal_info[address] != '')
    {
    ?>
          <tr>
            <td>
        <? $lang->DoLcV('ADDRESS'); ?>
            </td>
            <td>
        <? $personal_info[address] = sanitize_html_string($personal_info[address]); ?>
        <? echo $personal_info[address]; ?>
        <br>
        <?
        if ($personal_info[fk_dd_city] > 0)
        {
            ShowItem('city', $personal_info[fk_dd_city]);
        }
        ?>
        , &nbsp
        <? echo $personal_info[county]; ?>
        , &nbsp
        <?
        if ($personal_info[fk_dd_country] > 0)
        {
            ShowItem('country', $personal_info[fk_dd_country]);
        }
        ?>
        <br>
        <? $personal_info[postal] = sanitize_html_string($personal_info[postal]); ?>
        <? echo $personal_info[postal]; ?>
            </td>
          </tr>
    <?
    }
    if ($personal_info[phone_contact] != '')
    {
    ?>
          <tr>
            <td>
        <? $lang->DoLcV('PHONE'); ?>
            </td>
            <td>
        <? $personal_info[phone_contact] = sanitize_html_string($personal_info[phone_contact]); ?>
        <? echo $personal_info[phone_contact]; ?>
            </td>
          </tr>
    <?
    }
    if ($personal_info[email] != '')
    {
    ?>
          <tr>
            <td>
        <? $lang->DoLcV('FORM_EMAIL'); ?>
            </td>
            <td>
        <? $personal_info[email] = sanitize_html_string($personal_info[email]); ?>
        <a href="mailto:<? echo $personal_info[email]; ?>"><? echo $personal_info[email]; ?></a>
            </td>
          </tr>
          <tr height="5">
            <td colspan="2">
        <? //leave this blank ?>
            </td>
          </tr>
    <?
    }
    //*********************************************//
    //end displaying the users personal information//
    //*********************************************//
that is just a excert from the function but you get the idea. and for the admin to mail it i have

Code: Select all

if ($_POST[email_selected])
{
    echo '<form action="?op=do_email" method="post">';
    foreach($_POST[checked] as $key => $value)
    {
        $query = '
            SELECT
                first_name, last_name
            FROM
                mdl_cv_personal_info
            WHERE
                fk_user_id="'.$value.'"
        ';

        $do_query = mysql_query($query) or die(mysql_error().__LINE__);

        $names = mysql_fetch_assoc($do_query);

        $query = '
            SELECT
                email
            FROM
                mdl_usr_users
            WHERE
                user_id="'.$value.'"
        ';

        $do_query = mysql_query($query) or die(mysql_error().__LINE__);

        $info = mysql_fetch_assoc($do_query);

        echo $names[first_name].'&nbsp'.$names[last_name].'&nbsp &nbsp';
        $lang->DoL('FORM_EMAIL');
        echo '<input type="text" name="email['.$value.']" value="'.$info[email].'">';
        echo '<input type="hidden" name="checked[]">';
        echo '<br>';
    }
    echo '<input type="submit" name="submit" value="';
    $lang->DoL('FORM_SUBMIT');
    echo '"';
    echo '</form>';
    die();
}
maybe that helps?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

i couldn't think of any options other than the most common ones:
- return something from a function
- use global variable
- use buffer

I was wondering why you didn't want to return a value?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i didnt return a value becuase

a) i didnt think my code through enough
b) it is supra much easier to echo out the data as i go

but if anyone else has a idea or is this impossible?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Don't be so lazy and write the code properly :wink:. Just goes to show planning before coding is often recommended while keeping in mind things like flexibility.

Another option.. to capture your output is, as mentioned before ob_start() and ob_get_contents()
(http://ca.php.net/manual/en/function.ob ... ntents.php)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you can replace all the echo's with a var that will contain all the output and just return that in end. :|
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

shiznatix wrote:rewriting the function is not a option. sorry i should have said that at first.
your code is obviously badly design so why exactly cant you rewrite it?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

cause im super lazy but it seams like i dont have much of a choice so i gotta rewrite it
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well ok i lied i didnt rewrite the function but heres what i did do that worked quite well

Code: Select all

ob_start();
show_cv($user_id, '2');
$cvs = ob_get_contents();
$cvs = strip_tags($cvs);
mail($email, 'Your CV', $cvs);
ob_end_flush();
that is inside a loop so show_cv will get called then i get the contents of the page with ob_get_contents() then i mail it out then i flush the page then start over with the new ob_start then get contents. worked quite well
Post Reply