开发者

Complex Server-Side Validation

开发者 https://www.devze.com 2023-04-01 17:16 出处:网络
Hello fellow Stack Overflowites: I have a complex system/form here and I have completed the entire front-end validation (with jQuery)....

Hello fellow Stack Overflowites:

I have a complex system/form here and I have completed the entire front-end validation (with jQuery)....

I need help validating (on the server-side with PHP)... I was hoping this could be something we work at together (because my brain is almost fried now)... I think at this point I just need some help with the logic...

You can look at my form here: (View HTMLjQuery/source code here because it is rather long) http://www.thesportinghub.com/lms/make-my-picks

As you can see, it is pretty intense with lots of things going on... here are my basic validation requests:

1.) You can only pick the same team once through all 17 weeks... 2.) You cannot pick a team for a respective week after the time of that game has been passed...

All of the information revolving around this schedule is in an XML document set up with the following format (This is just a snippet of the BIGGER XML code):

<week id="1">
        <matchup id="1" date="08/29/11" time="1:53 PM">
            <away city="New Orleans">Saints</away>
            <home city="Green Bay">Packers</home>
            <finalscore>
                <away>6</away>
                <home>0</home>
            </finalscore>
        </matchup>
        <matchup id="2" date="09/11/11" time="1:00 PM">
            <away city="Atlanta">Falcons</away>
            <home city="Chicago">Bears</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
</week>
    <week id="2">
        <matchup id="1" date="09/18/11" time="1:00 PM">
            <away city="Oakland">Raiders</away>
            <home city="Buffalo">Bills</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
        <matchup id="2" date="09/18/11" time="1:00 PM">
            <away city="Kansas City">Chiefs</away>
            <home city="Detroit">Lions</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
        <matchup id="3" date="09/18/11" time="1:00 PM">
            <away city="Baltimore">Ravens</away>
            <home city="Tennessee">Titans</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
</week>

So, tell me if my logic is right here... Or what you would do in this scenario...

When the form submits, I will need to create a multidimensional array that stores the data submitted (before it ever goes to my database)... This array will contain the teams selected, for whichever week they were selected for, and the time they were selected.

I will then have to load my XML (with SimpleXML and PHP) and somehow? week-by-week make sure there were no selections past the time of the game.

Honestly, my head is literally broken right now. I doubt any of you are following me?

Could you atleast lead me in the right direction? This is an intense script encompassing so much stuff. All the front-end validation is done. I just need to compare the submitted information with the XML and ensure all my loopholes are covered.

UPDATE:

Here is my PHP code thus far, but it is not really working... I am just showing it to help paint the picture of what I am working with.

  <?php 
    if( isset($_POST['submit']) ) {
    $schedule = "schedule.xml";

$xml = simplexml_load_file($schedule) or die ("Unable to load XML file!");
    date_default_timezone_set('US/Eastern');
    $time = date("h:i:s", time());
    $week1 = $_POST['Week_1'];
    $week2 = $_POST['Week_2'];
    $week3 = $_POST['Week_3'];
    $week4 = $_POST['Week_4'];
    $week5 = $_POST['Week_5'];
    $week6 = $_POST['Week_6'];
    $week7 = $_POST['Week_7'];
    $week8 = $_POST['Week_8'];
    $week9 = $_POST['Week_9'];
    $week10 = $_POST['Week_10'];
    $week11 = $_POST['Week_11'];
    $week12 = $_POST['Week_12'];
    $week13 = $_POST['Week_13'];
    $week14 = $_POST['Week_14'];
    $week15 = $_POST['Week_15'];
    $week16 = $_POST['Week_16'];
    $week17 = $_POST['Week_17'];

    foreach($xml->week as $week)
    {
    $week_number = $week['id'];

    foreach($week->matchup as $matchup)
    {
    $week_name = "Week_" . $week_number;
    $away_city = $matchup->away['city'];
    $home_city = $matchup->home['city'];
    $away_teamname = $matchup->away;
    $home_teamname = $matchup->home;
    $game_time = $matchup['time'];
    $game_date = $matchup['date'];
    $away_full = "{$away_city} {$away_teamname}";
    $home_full = "{$home_city} {$home_teamname}";
    $home_score = $matchup->finalscore->home;
    $away_score = $matchup->finalscore->away;

    date_default_timezone_set('US/Eastern');
    $game = "{$game_date} {$game_time}";

    ?>
    <div class="savedbox">
    <?php
    if (strtotime($game) <= time()) {
    ?>
    <strong>You cannot pick the <?php echo $away_full ?> or <?php echo $home_full ?>. Those teams have been locked for the respective week.</strong>
    <?php
    }
    }
    }
    ?>
    <?php

    if (count(array_unique($_POST))  ===  count($_POST)) {
    ?>
    <strong>Your picks have been saved!</strong><br/><br/>

    <strong>Week 1 Pick:</strong> <?php echo $week1 ?><br/>
    <strong>Week 2 Pick:</strong> <?php echo $week2 ?><br/>
    <strong>Week 3 Pick:</strong> <?php echo $week3 ?><br/>
    <strong>Week 4 Pick:</strong> <?php echo $week4 ?><br/>
    <strong>Week 5 Pick:</strong> <?php echo $week5 ?><br/>
    <strong>Week 6 Pick:</strong> <?php echo $week6 ?><br/>
    <strong>Week 7 Pick:</strong> <?php echo $week7 ?><br/>
    <strong>Week 8 Pick:</strong> <?php echo $week8 ?><br/>
    <strong>Week 9 Pick:</strong> <?php echo $week9 ?><br/>
    <strong>Week 10 Pick:</strong> <?php echo $week10 ?><br/>
    <strong>Week 11 Pick:</strong> <?php echo $week11 ?><br/>
    <strong>Week 1开发者_运维技巧2 Pick:</strong> <?php echo $week12 ?><br/>
    <strong>Week 13 Pick:</strong> <?php echo $week13 ?><br/>
    <strong>Week 14 Pick:</strong> <?php echo $week14 ?><br/>
    <strong>Week 15 Pick:</strong> <?php echo $week15 ?><br/>
    <strong>Week 16 Pick:</strong> <?php echo $week16 ?><br/>
    <strong>Week 17 Pick:</strong> <?php echo $week17 ?><br/>

    <?php
    } else {
    ?>
    <strong>Trying to pull a <em>fast one</em>? We don't think so. You can only pick the same team once. If you need more clarification of the rules, please visit <a href="how-to-play">How To Play</a>.</strong>
    <?php
    }
    ?>
    </div><br/>
    <?php
    }
    ?>

I should also mention, this will all tie into a mySQL database table as well. I will need to take all of the information people submit and plug it into the database table I have set up. It will only go to the database if they pass all of the validation I have outlined above.

Any and all help would be appreciated. Even if you are just giving me a few basic pointers on how to get started.

Thanks, Chris


<?php 

if( isset($_POST['submit']) )
{
    $required_weeks = 17;
    $schedule = "schedule.xml";
    $xml = simplexml_load_file($schedule) or die ("Unable to load XML file!");
    date_default_timezone_set('US/Eastern');
    $time = date("h:i:s", time());

    foreach($xml->week as $week)
    {
        foreach($week->matchup as $matchup)
        {
            $away_city = $matchup->away['city'];
            $home_city = $matchup->home['city'];
            $away_teamname = $matchup->away;
            $home_teamname = $matchup->home;
            $game_time = $matchup['time'];
            $game_date = $matchup['date'];
            $away_full = $away_city . ' ' . $away_teamname;
            $home_full = $home_city . ' ' . $home_teamname;
            $home_score = $matchup->finalscore->home;
            $away_score = $matchup->finalscore->away;

            date_default_timezone_set('US/Eastern');
            $game = $game_date . ' ' . $game_time;

?>
<div class="savedbox">
<?php

            if (strtotime($game) <= time())
            {

?>
<strong>You cannot pick the <?php echo $away_full ?> or <?php echo $home_full ?>. Those teams have been locked for the respective week.</strong>
<?php

            }
        }
    }

    //if (count(array_unique($_POST))  ===  count($_POST))
    if (count(array_unique($_POST)) === $required_weeks)
    {

?>
<strong>Your picks have been saved!</strong><br/><br/>
<?php

        for($a = 1; $a <= $required_weeks; $a++)
        {

?>
<strong>Week <?php print $a; ?> Pick:</strong> <?php echo $_POST['Week_' . $a] ?><br/>
<?php

        }
    }
    else
    {

?>
<strong>Trying to pull a <em>fast one</em>? We don't think so. You can only pick the same team once. If you need more clarification of the rules, please visit <a href="how-to-play">How To Play</a>.</strong>
<?php

    }

?>
</div><br/>
<?php

}

?>

I've tidied your code up a bit (no need to use vars when you can just use $_POST and a more flexible total weeks) but your code looks good.

Other than checking for 17 (in your example) unique selections and making sure all are in the future, do you need to check / do anything else?

0

精彩评论

暂无评论...
验证码 换一张
取 消