Page 1 of 1

assigning a function to a variable...kinda

Posted: Thu May 19, 2005 12:51 pm
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.

Posted: Thu May 19, 2005 12:58 pm
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);

Posted: Thu May 19, 2005 1:00 pm
by shiznatix
rewriting the function is not a option. sorry i should have said that at first.

Posted: Thu May 19, 2005 1:09 pm
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.

Posted: Thu May 19, 2005 1:15 pm
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?

Posted: Thu May 19, 2005 2:01 pm
by Skara
this might be what you're looking for: http://php.net/ob_start

Posted: Thu May 19, 2005 2:06 pm
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 :)

Posted: Thu May 19, 2005 2:32 pm
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?

Posted: Thu May 19, 2005 3:56 pm
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?

Posted: Thu May 19, 2005 7:12 pm
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?

Posted: Thu May 19, 2005 7:35 pm
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)

Posted: Fri May 20, 2005 6:01 am
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. :|

Posted: Fri May 20, 2005 6:26 am
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?

Posted: Fri May 20, 2005 7:04 am
by shiznatix
cause im super lazy but it seams like i dont have much of a choice so i gotta rewrite it

Posted: Fri May 20, 2005 8:49 am
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