dse84825 2017-02-23 10:31
浏览 13
已采纳

使用PHP根据来宾数发送特定URL

I've written a little code that allows me to send a specific link in an automated response email based on a form submission. In other words, I have a contact form wich will be filled out by users and depending on the number of guests they put in the field, they'll receive one link or another. What I got so far is working fine but I want to create a rule in case they enter zero as value in the input field. What I want is that when they put zero, the code can tell them that 0 guests is not a valid number. Hope i'm being clear and thanks for your help in advance.

<?php

    // This value will be grabbed from a form.
    $guests = 21; // Enter some numeric value here.

    // This is the Brochure's URL in 3 different options, depending on the Nº of guests.
    $brochure = array ( '0' => 'http://www.yahoo.com', '1' => 'http://www.google.co.uk', '2' => 'http://www.kazzabe.com' );

    // Start asking number of guests.
    if ( $guests <= 10 ) {
        // Between 1 and 10.
        $brochure_link = $brochure[0]; 
    } else if ( $guests >= 11 && $guests <= 20 ) {
        // Between 11 and 20.
        $brochure_link = $brochure[1]; 
    } else ( $guests >= 21 ) {
        // More than 20.
        $brochure_link = $brochure[2] 
    };

    echo 'As there will be <b>'.$guests.'</b> guests at your wedding, we proceed to send you the brochure you have requested. In order to download it, please follow this <a href="'.$brochure_link.'" target="_blank">link.</a>';
  • 写回答

1条回答 默认 最新

  • doukao2180 2017-02-23 10:39
    关注

    So make the first thing you do a test for zero guests, throw an error and exit.

    <?php
    
        // This value will be grabbed from a form.
        $guests = 21; // Enter some numeric value here.
    
        // This is the Brochure's URL in 3 different options, depending on the Nº of guests.
        $brochure = array ( '0' => 'http://www.yahoo.com', '1' => 'http://www.google.co.uk', '2' => 'http://www.kazzabe.com' );
    
        // Start asking number of guests.
        if ( $guests == 0 ) {
            echo 'Zero (0) is not a valid Nº of guests. Please try adding a number major than zero.';
            exit;
        } else if ( $guests <= 10 ) {
            // Between 1 and 10.
            $brochure_link = $brochure[0]; 
        } else if ( $guests >= 11 && $guests <= 20 ) {
            // Between 11 and 20.
            $brochure_link = $brochure[1]; 
        } else {
                if ( $guests >= 21 ) {
                    // More than 20.
                    $brochure_link = $brochure[2];
                }
        }
    
        echo 'As there will be <b>'.$guests.'</b> guests at your wedding, we proceed to send you the brochure you have requested. In order to download it, please follow this <a href="'.$brochure_link.'" target="_blank">link.</a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题