need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
df75douglas
Forum Newbie
Posts: 6
Joined: Thu Nov 10, 2011 1:30 am

need help

Post by df75douglas »

here is my code

Code: Select all

<html><body>
<h4>sorter</h4>
<form action="" method="post"> 

numbers#(seperate by commas): <input name= "data" type="text" />

<input type ="submit"/>

<form/>

<?php
$data = $_POST['data'];

?>

<table border= 1 cellspacing=1 cellpadding=2 align=center>
<tr><td>numbers entered<td/>
<td>
<?php
echo $data;
?>
<td/><tr/>
<tr><td>#'s from highest to lowest<td/>
<td>
<?php
//order numbers
?>
<td/><tr/>
<tr><td>average<td/>
<td>
<?php
//average numbers
?>
<td/><tr/>
<tr><td>sorted sequence<td/>
<td>
<?php
//seq.
?>
<td/><tr/>
<table/>
<body/>
<html/>
I need to manipulate the numbers the user places in the "data" field.
i need to sort them highest to lowest.
I need to find the average.
the user can put in as many numbers as they want separated by commas.

any ideas?
Last edited by Benjamin on Thu Nov 10, 2011 1:42 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: need help

Post by twinedev »

Code: Select all

<?php
    $aryErr = array();
    $aryData = FALSE;

    if (count($_POST)>0) {
        if (!isset($_POST['data']) || !preg_match('/^[0-9]+( ?, ?[0-9]+)*$/',$_POST['data'])) {
            $aryErr['data'] = 'Please only enter numbers separated by commas';
        }
        else {
            // Process it
            $_POST['data'] = str_replace(' ','',$_POST['data']);
            $aryData = explode(',',$_POST['data']);
            sort($aryData);
            $fAverage = array_sum($aryData)/count($aryData);
        }
    }
?><html>
<head>
    <title>Sample Number Handler</title>
</head>
<body>
    <h4>Sorter</h4>

    <?php if(isset($aryErr['data'])): ?>
        <p><strong><em><?php echo $aryErr['data']; ?></em></strong></p>
    <?php endif; ?>

    <form action="" method="post">
        <label for="data">Numbers (separated by commas):</label>
        <input type="text" name="data" id="data" />
        <input type="submit" name="submit" id="submit" value="Process" />
    </form>

    <?php if($aryData!==FALSE): ?>
        <table border="1" cellpadding="2" cellspacing="1">
            <tr>
                <th>Numbered Entered</th>
                <td><?php echo $_POST['data']; ?></td>
            </tr>
            <tr>
                <th>Average of numbers</th>
                <td><?php echo number_format($fAverage,2); ?></td>
            </tr>
            <tr>
                <th>Numbers from lowest to highest</th>
                <td><?php echo implode(',',$aryData); ?></td>
            </tr>
        </table>
    <?php endif; ?>
</body>
</html>
( why do I feel like I just did someones homework? )
df75douglas
Forum Newbie
Posts: 6
Joined: Thu Nov 10, 2011 1:30 am

Re: need help

Post by df75douglas »

thank you. ( and it was 1 of 4 questions I had, but it was after the due date)
If you are bored and want to help a novice php programmer. Please explain some of the syntax and logic. I am going through it but am only 3 weeks into php
and have decent knowledge of html, css, xml.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: need help

Post by twinedev »

Be glad to, I normally explain as well as give a solution, but this week have been swamped big time ;) I will to go through this this weekend for you.

-Greg
Post Reply