Jump to content

Arrays and Loops


traymonds

Recommended Posts

Hi:


I'm new to PHP and struggling to figure out how to perform the last 2 bullets.... 


Use POST function to retrieve inputs from 3 HTML text boxes - containing values within the range 0.0–10.0.


Place the values in an array, and then do a loop to search the array and validate the input.


- Display results  back to the browser confirming whether the input is valid.


Can anyone help? 


Link to comment
https://forums.phpfreaks.com/topic/295922-arrays-and-loops/
Share on other sites

With respect to requinix replay;

 

can this help to you?

 

<?php
        $arr = array();
        if (isset($_POST['submit'])) {
            foreach($_POST as $key=>$value) {
                if ($value != 'submit') {
                    if ($value >= 0.0 && $value <= 10.0) {
                        $arr[$key] = $value;
                    }
                }
            }
            foreach($arr as $key=>$val) {
                echo $key." Value inserted by User: <strong>".$val."</strong>\n";
            }
        }
       ?>
Link to comment
https://forums.phpfreaks.com/topic/295922-arrays-and-loops/#findComment-1510486
Share on other sites

 POST variables are returned as String afaik, so they needs to be converted into integer for integer comparison

 

So this should do the job:

  if ((int)$value >= 0.0 && (int)$value <= 10.0) {

Except you probably don't want to cast floats as integers, or 5.5 will be the same as 5.2 (both would be just 5)

Link to comment
https://forums.phpfreaks.com/topic/295922-arrays-and-loops/#findComment-1510513
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.