如何处理告诉朋友数据? [英] How to process tell-a-friend data?

查看:147
本文介绍了如何处理告诉朋友数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基本上具有告诉朋友功能的脚本,但我试图找出捕获和处理数据的最佳方法。



例如:


  1. 我是否显示5对要求姓名和电子邮件的字段,每对是$ friend_name1 / $ friend_email1,$ friend_name2 / $ friend_email2等,它允许您一次给5个朋友发邮件?

  2. 我是否显示一对并使用JavaScript来允许用户使用变量命名约定作为#1继续添加更多的朋友吗?
  3. 是否按#1或#2中的建议显示它们,但随后提交一个数组,例如$ friend_email [] / $ friend_name []等。




  4. 捕获数据的最佳方式是什么?如果你有一个数组,例如#3中的数组, ,然后你循环每个$ friend_name $ _POST?你把它存储在另一个数组中吗?如何确保正确的姓名/电子邮件组合保持在一起,例如,如果用户没有为第三个朋友添加姓名?



    大多数人都这么做,因此在拍摄过程中他们可以保持灵活性,并在加工过程中保持精确?我真的很想在这里寻找逻辑,尽管示例代码非常感谢。



    我跟踪的其中一件事是谁指的是谁。例如,如果A指的是B和B买东西,A就会受益。所以准确性和安全性对我来说非常重要。



    谢谢,
    Ryan

    解决方案

    我会推荐这个数组。你可以做JavaScript或其他,但确保处理结束可以看到有多少推介。为确保正确的电子邮件和名称保持在一起,请尝试类似于

     < input type =textname = friend_name [0]/> 
    < input type =textname =friend_email [0]/>
    < input type =textname =friend_name [1]/>
    < input type =textname =friend_email [1]/>

    等等。从那里,你的PHP脚本将简单地匹配$ _POST var中的数组索引。例如,如果这个表单是用一些任意数据提交的(如上所述),它的格式就是这样(这是print_r($ _ POST); data)

    <$ p $数组

    [friend_name] =>数组

    [0] => test_name
    [1] => ; test_name2


    [friend_email] => Array

    [0] => email@something.com
    [1] = > email@two.com



    循环浏览这些内容的想法(如果您实现了具有无限引用数的潜在可能性),则可以使用count();功能。实现将如下所示:

    pre $ for($ i = 0; $ i< count($ _ POST ['friend_name']] ); ++ $ i)
    {
    if($ _ POST ['friend_email'] [$ i] == NULL)continue; //忽略NULL电子邮件
    //处理数据等额
    }

    或者,如果这些名字的输入顺序不正确,PHP的foreach()特性非常好,并且可以完成相同的事情(可能对于这种类型的错误检查更有效)

    <$ ($ _ POST ['friend_email'] [$ key] $ p $ foreach($ _ POST ['friend_name'] as $ key => $ val)
    {
    if == NULL)继续; //忽略NULL电子邮件
    //处理$ val和$ _POST ['friend_email'] [$ key]
    }

    希望这有助于!祝你好运!



    Dennis M。


    I'm working on a script that essentially has tell-a-friend functionality, but I'm trying to figure out the best way to capture and process the data.

    For example:

    1. Do I display 5 pairs of fields asking for name and email, each pair being $friend_name1/$friend_email1, $friend_name2/$friend_email2, etc, which allows you to email 5 friends at a time?

    2. Do I display one pair and use JavaScript to allow the user to keep adding more friends using the variable naming convention as #1?

    3. Do I display them as suggested in #1 or #2, but then submit an array e.g. $friend_email[]/$friend_name[], etc.

    What's the best way to capture the data?

    And then what's the best way to process the data?

    If you get an array, such as in #3, do you then loop through each $friend_name $_POST? Do you store it in another array? How do you ensure that the correct name/email combination stays together, if for example, the user didn't add a "name" for the third friend?

    How do most people do it so they can remain flexible during the capturing and precise during the processing? I'm really looking for logic here, although sample code is much appreciated.

    One of the things I'm tracking is who is referring who. For example, if A refers B and B buys something, A will benefit. So accuracy and security are very important to me.

    Thanks, Ryan

    解决方案

    I would recommend the array. You could do javascript or otherwise, but make sure that the processing end can see how many referrals there are. To ensure the correct e-mail and name stay together, try something like

    <input type="text" name="friend_name[0]" />
    <input type="text" name="friend_email[0]" />
    <input type="text" name="friend_name[1]" />
    <input type="text" name="friend_email[1]" />
    

    and so on. From there, your PHP script will simply match the array indices in the $_POST var. For instance, if this form was submitted (as above) with some arbitrary data, it would format like this (this is the print_r($_POST); data)

    Array
    (
        [friend_name] => Array
            (
                [0] => test_name
                [1] => test_name2
            )
    
        [friend_email] => Array
            (
                [0] => email@something.com
                [1] => email@two.com
            )
    
    )
    

    An idea on looping through these (if you implemented javascript with a potential for an "infinite" number of referrals) would be to use the count(); function. Implementation would look something like this:

    for($i=0;$i<count($_POST['friend_name']);++$i)
    {
        if($_POST['friend_email'][$i] == NULL) continue; // Ignore NULL e-mails
        // Process data blah
    }
    

    Or, if those names where entered out of order, PHP's foreach() feature is very nice and can accomplish the same thing (probably more efficiently for this type of error checking)

    foreach($_POST['friend_name'] as $key => $val)
    {
        if($_POST['friend_email'][$key] == NULL) continue; // Ignore NULL e-mails
        // Process $val and $_POST['friend_email'][$key]
    }
    

    Hope this helps! Good luck!

    Dennis M.

    这篇关于如何处理告诉朋友数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆