Jump to content

chito

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Posts posted by chito

  1. can we see the script that's calling this function? i presume you're iterating through the array of names/emails (as per the other thread) and running this function each time? if so, it's likely that you're replacing the $sent array every time mail_message() is called. the tell-tale sign of this being the issue is that it only ever contains the information of the last person mailed at the end.

     

    Thats correct thats the whole script tho. I'm iterating and sending to the function to do the mailing then I'm trying to keep an array populated of each email that is processed and not blank, then after mailing, echo a list that shows who was emailed. I may be trying to do to much in that function, maybe not the right place to do it. Like I mentioned in the other post, Im just learning and trying to wrap my head around the best practices part of PHP.. any advice on how to structure this better? Am I way off?  :-[

  2. Here's the code for the input form. It's an assignment for my class. Thanks

     

    
        <head>
            <title>Party Invitation Central</title>
        </head>
    <body>
        <h3><b>Invite up to 10 people to your party.</b></h3>
    
        <form method="POST" action="invite_process.php">
    
            <table>
                <tbody>
                    <tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name1" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email1" value="" size="25" /></td>
                    </tr>
                    <tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name2" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email2" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name3" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email3" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name4" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email4" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name5" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email5" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name6" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email6" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name7" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email7" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name8" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email8" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name9" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email9" value="" size="25" /></td>
                    </tr><tr>
                        <td align="right">Name:</td>
                        <td align="left"><input type="text" name="name10" value="" size="25" /></td>
                        <td align="right">Email:</td>
                        <td align="left"><input type="text" name="email10" value="" size="25" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" value="SUBMIT" />
                        </td>
                    </tr>
    
                </tbody>
            </table>
    
    
    
        </form>
    </body>
    
    
    

  3. for the record (and future form design), it would have been far more straightforward to process these values the way you wanted if you used an array key in the name, rather than an iterative variable tacked onto the end of your name:

     

    <td align="right">Name:</td>
    <td align="left"><input type="text" name="name[1]" value="" size="25" /></td>
    <td align="right">Email:</td>
    <td align="left"><input type="text" name="email[1]" value="" size="25" /></td>

     

     

    what this allows you to do is iterate through one of the $_POST arrays, and use the key to identify its partner:

     

    $paired_data = array();
    foreach ($_POST['name'] AS $key => $this_name)
    {
      $this_email = $_POST['email'][$key];
      $paired_data[$key] = array('name' => $this_name, 'email' => $this_email);
    }
    print_r($paired_data);

     

    the php manual has some pretty good information on arrays here, if you're still quite new to them.

     

    This makes sense and I will keep for future reference. Thanks for all your replies Teamatomic and Gizmo as well. I went thru both your recommendations and I can understand the logic of both applications and I will be keeping all this for my references.. I have been referring to the PHP manual a lot but it seems to leave something to be desired when dealing with some particulars.. especially arrays for some reason. Books I've gone through also seem to just touch the surface and not get too in depth, maybe its just my thinking that needs to be adjusted as well as more familiarity over time with the concepts. Thanks for the examples, all of them give me an insight as to how people work with PHP and how its more a matter of personalizing how you get the job done.. Thanks again.

  4. Hi.. My script is processing up to 10 inputs from a form (name, email) and mailing out invite to each email. I need to echo after the emails are processed a list of who was emailed. I seem to be stuck on Return Array from a Function. I've tried also using return array ($array) to no luck as well. I've been able to build the array correctly in the function for each email processed now I need to build the array outside to create a running list of all the emails that were processed. Thanks for any help or advice.

     

    <?php
    
    function mail_message ($name,$email,$template_file) {
        if (empty ($name)) {
            echo "The fields are empty<br/>";
            return ;
        }
    
        else {
    
    
            $sent['name'] = $name;
            $sent['email'] = $email;
    
            #echo "inside $name, $email<br/>";
            $email_message = file_get_contents($template_file);
            $email_message = str_replace("#NAME#", $name, $email_message);
    
            #construct the email headers
            $to = $email;
            $from = "webmaster@viaeterna.com";
            $email_subject = "Your invited to a party!";
    
            #now mail
            mail($to, $email_subject, $email_message, "From: Party List Central ".$from);
    
           #for me to see if its building the array inside the function
            echo '<pre>';
            print_r($sent);
            echo '</pre>';
    
            return $sent;
    
        }
    }
    
    
    $list = array();
    foreach ($_POST as $key => $value) {
        // is this a name?
        if (substr($key, 0, 4) == 'name') {
            $index = (int)substr($key, 4);
            $list[$index]['name'] = $value;
            $name = $value;
            # echo "$name<br/>";
        }
    
        // is this an email
        if (substr($key, 0, 5) == 'email') {
            $index = (int)substr($key, 5);
            $list[$index]['email'] = $value;
            $email = $value;
            # echo "$email<br/>";
            //DOCUMENT_ROOT is the file path leading up to the template name.
            mail_message($name,$email,'./invite_text.txt');
        }
    
    
    
    
    }
    
    $sent=mail_message($sent);
    
    echo '<pre>';
    print_r($sent);
    echo '</pre>';
    
    ?>
    
    

     

  5. Thanks I gave that a try, it still blocked an ip when it found '202' anywhere in the ip.

    I just need to block an ip starting with '202' this seemed to pick it up anywhere in the address.

    $useragent = $_SERVER['HTTP_USER_AGENT'];
    
    
    #$remoteaddr = $_SERVER['REMOTE_ADDR'];
    $remoteaddr = '192.202.0.102';
    
    
    #$deny =  '202';
    
    
    if (preg_match('#202\.\d{1,3}\.\d{1,3}.\d{1,3}#',$remoteaddr)) {  
        echo "<h3>Sorry you can not download the requested file, please feel free to visit our friends and file your grievence <a href='http://www.fbi.gov/'>Click Here</a></h3>";
        exit();
    }
    

     

  6. I have a script exercise that is supposed to deny someone with an IP starting 202.xxx.xx.xxx from accessing a download link as well as checking for browser compatibility. What I have works, but then an IP that happened to have "202" anywhere in the address would be blocked, not just one that began with that. I can't seem to find anything describing the proper way to format an expression to do so. Im starting to think the books I have suck or my googleing skills are slipping. Thanks for the help.

     

    $useragent = $_SERVER['HTTP_USER_AGENT'];
    
    
    $remoteaddr = $_SERVER['REMOTE_ADDR'];
    
    
    $deny =  '202';
    
    
    if (ereg($deny, $remoteaddr)) {  
        echo "<h3>Sorry you can not download the requested file, please feel free to visit our friends and file your grievence <a href='http://www.fbi.gov/'>Click Here</a></h3>";
        exit();
    }
    
    elseif (preg_match('|Windows|', $useragent) && (preg_match('|MSIE|', $useragent))) {
      echo "<h3> Your file is now ready for download,<a href='http://google.com'>Click Here</a> to begin.";
    }
    
    elseif (preg_match('|Macintosh|', $useragent) && (preg_match('|Firefox|', $useragent))) {
        echo "<h3> Your file is now ready for download,<a href='http://google.com'>Click Here</a> to begin.";
    }
    
    else {
        echo "<h3> Sorry but your browser appears to be incompatible with our downloader</h3><br/>";
        echo "<b> If you are using a Windows computer please download the latest version Internet Explorer <a href='http://www.microsoft.com/nz/windows/internet-explorer/default.aspx'>Here</a></b><br/>";
        echo "<b> If you are using a Macintosh computer please download the latest version of Firefox <a href='http://www.mozilla.com/en-US/firefox/u'>Here</a></b><br/>";
        
    }
    
    

     

  7. Hello Im new to PHP and still trying to wrap my head around array's in particular how to iterate through them to get what I need. As an assignment I have to build a party invite  form with up to 10 inputs for a name and email for each individual that will mail each person an invite, and then the admin a list of who was mailed. I have tried a few ways to approach this but keep running into a dead end. Without giving me the complete solution can you help me with the concepts and how to structure and approach this from a newbie standpoint. Mostly the issue I have is how to get the values from each item and build a complete Name & Email for each person from 2 separate elements.

     

    I have an input form with 10 inputs formated like this:

    <form method="POST" action="invite_process.php">
    <tr>
            <td align="right">Name:</td>
            <td align="left"><input type="text" name="name1" value="" size="25" /></td>
            <td align="right">Email:</td>
            <td align="left"><input type="text" name="email1" value="" size="25" /></td>
    </tr>
    
    My $_POST array ends up looking like this:
    
    Array
    (
        [name1] => myname1
        [email1] => email1@email.com
        [name2] => myname2
        [email2] => email2@email.com
        [name3] => myname3
        [email3] => email3@email.com
        [name4] => myname4
        [email4] => email4@email.com
        [name5] => 
        [email5] => 
        [name6] => 
        [email6] => 
        [name7] => 
        [email7] => 
        [name8] => 
        [email8] => 
        [name9] => 
        [email9] => 
        [name10] => 
        [email10] => 
    )
    
    This obviously gives me each element I need:
    
    while(list($key, $value) = each($HTTP_POST_VARS))
    {
    echo "$key = $value<br/>";
    
    }
    
    name1 = myname1
    email1 = email1@email.com
    name2 = myname2
    email2 = email2@email.com
    name3 = myname3
    email3 = email3@email.com
    name4 = myname4
    email4 = email4@email.com
    
    
    also tried a foreach loop:
    
    foreach ($_POST as $key => $value) {
       foreach ($value as $name => $x);
        echo $name."<br/>";
        echo $value;
    }
    
    myname1
    email1@email.com
    myname2
    email2@email.com
    myname3
    email3@email.com
    myname4
    email4@email.com
    

    My problem is stepping thru each element and grabbing the value I need to build the complete name and email address for the email. Any help and pointers would be greatly appreciated.. I REALLY want to get this down!

  8. Thanks all, I guess there are many ways to do the same thing, the way I had it just made sense to me, but I see the other points as well. Made an adjustment to the foreach loop to endup looking like this, I do think the first way looked more efficient tho.

     

    foreach ($products as $product=>$value) {

      foreach ($value as $item=>$x);

      $subtotals += subtotals($value['price'], $value['shipping'], $tax);

      echo "<li>$product: {$value['price']}</li>";

      }

     

    This is a great site for someone like me just learning and grow my knowledge..thanks again.

     

  9. Ok my teacher came back telling me that the array i used with the above response that solved my issue was not to her liking.

     

    $products = array (array ("item"=>"Candle Holder","price"=>"12.95","shipping"=> "0"),

                                array ("item"=>"Coffee Table","price"=>"99.50","shipping"=> "0.10"),

                                array ("item"=>"Lamp","price"=>"42.99","shipping"=> "0.10")

     

    She stated that it was better to format in this way, but without and explanation why..

     

    $products = array ("Candle Holder"=>array("price"=>"12.95","shipping"=> "0"),

                                  "Coffee Table"=>array("price"=>"99.50","shipping"=> "0.10"),

                                              "Lamp"=>array("price"=>"42.99","shipping"=> "0.10")

    );

     

    From all the examples I've seen in most books and online sources I've seen the first example used, any reason why the 2nd would be a better or worse option?

     

    Thanks.

     

  10. Hello, Im just learning and getting started with PHP and I've searched high and low for the answer myself and can't seem to find the right application. My assignment requires pulling the price and shipping cost out of this and sending it to a function that does the calculation for each item. The way I have it below works, but my instructor wants me to have the array be more descriptive as in example 2. For some reason I can't figure out how to manipulate the loop to get the extracted items to the variables as I need them, any advice?

    Thanks for any clarity you can offer..

     

    example 1: It works as I need it to.

    <B>Checkout</B><br>

    Below is a summary of the products you wish to purchase, along with totals:

    <?php

     

    # Enclosed the calculations into a function

    function subtotals($price,$shipping,$tax) {

    $total_price += $price;

    $total_tax += $tax * $price;

    $total_shipping += $shipping * $price;

    $grand_total += ($total_price + $total_tax + $total_shipping);

    return $grand_total;

    }

     

    $products = array ("Candle Holder" => array ("12.95", "0"),

    "Coffee Table" => array ("99.50", "0.10"),

    "Lamp" => array ("42.99", "0.10")

    );

     

     

    #tax rate is constant

    $tax = 0.08;

    $total_price = 0;

    $total_tax = 0;

    $total_shipping = 0;

    $grand_total = 0;

    ?><ul><?

     

    # Foreach loop grabs value from each item and populates variable for calc and echo's subtotal

    foreach ($products as $key =>$value) {

    list($price,$shipping)=$value;

    $subtotals += subtotals($price, $shipping, $tax);

    echo "<li>".$key.": $".$price;

    }

     

    ?>

     

    </ul>

    <hr>

    <br>

    <B>Total (including tax and shipping): $<? echo $subtotals; ?></B>

     

     

    Example 2: Teacher requests I set up array as so:

     

    $products = array (array ("item"=>"Candle Holder","price"=>"12.95","shipping"=> "0"),

                                array ("item"=>"Coffee Table","price"=>"99.50","shipping"=> "0.10"),

                                array ("item"=>"Lamp","price"=>"42.99","shipping"=> "0.10")

    );

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.