Jump to content

Retrieve data from text file...stuck...


lindm

Recommended Posts

The function works. Modified it shortly so the $file variable is directly within the function (see below). I have another problem however due to the import function on my page since I switched to php5 last week...my page is built up by the user either pressing a save button or an import button (the function you helped med with). When user presses the save button the form post value is saved and when the import button is pressed the import function executes. This is controlled by a function as follows:

function sieorform($formname,$sievalue){if(isset($_POST["spara"])){return $_POST[$formname];} else if(isset($_POST["SIE"])){return $sievalue;}}

this however returns an error when the user presses save:

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/writeform.php on line 109

Line 109 is the foreach of the import function:

function calculate_range($cat, $bool, $range_min, $range_max)
{
    $contents = file($_FILES['userfile']['name']);
    foreach($contents as $line_value)
    {
        list($_cat, $_bool, $_id, $num) = explode("\t", $line_value);

        $_cat = str_replace('#', '', $_cat);

        $number[$_cat][$_bool][$_id] = trim($num);
    }

    $total = 0;
    for($i = $range_min; $i <= $range_max; $i++)
    {
        if(isset($number[$cat][$bool][$i]))
        {
            $total += $number[$cat][$bool][$i];
        }
    }

    return $total;
}

Here is an example of the whole code:

sieorform(RRintNOcy,calculate_range("RES", 0, 3000, 3799)

 

My guess is that calculate_range still is executed despite the save button (spara) is pressed and can't find a file to import so it returns an error?

 

 

Link to comment
Share on other sites

Try the following:

function sieorform($formname, $sievalue)
{
    if(isset($_POST["spara"]))
    {
        return $_POST[$formname];
    }
    elseif(isset($_POST["SIE"]))
    {
        return eval("calculate_range($sievalue);");
    }
}

sieorform(RRintNOcy, "'RES', 0, 3000, 3799");

 

What I have done instead is pass just the arguments for the calculate_range function as a string to the sieorform function. Then if $_POST["SIE"] isset and and $_POST["spara"] is not it'll run the calculate_range function with the passed parameters.

 

NOTE: You will have to enclose the the parameters in quotes for the second parameter when calling the sieorform function otherwise the code wont work. Example:

Correct

sieorform(RRintNOcy, "'RES', 0, 3000, 3799");

Incorrect

sieorform(RRintNOcy, 'RES', 0, 3000, 3799);

Link to comment
Share on other sites

Not the ultimate solution for me. Been trying around a bit and was thinking that in the function calculate_range I add a condition that it continues to excute if if(isset($_POST["SIE"])). Currently no errors are produced if I press save or import but the save part with getting the info from the post paramater not working:

 

Is this a possible solution?

function calculate_range($cat, $bool, $range_min, $range_max)
{
    
if(isset($_POST["SIE"])){



$contents = file($_FILES['userfile']['name']);
    foreach($contents as $line_value)
    {
        list($_cat, $_bool, $_id, $num) = explode("\t", $line_value);

        $_cat = str_replace('#', '', $_cat);

        $number[$_cat][$_bool][$_id] = trim($num);
    }

    $total = 0;
    for($i = $range_min; $i <= $range_max; $i++)
    {
        if(isset($number[$cat][$bool][$i]))
        {
            $total += $number[$cat][$bool][$i];
        }
    }

    return $total;
}
}

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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