I have problem :(
[19-Feb-2016 22:46:28 Europe/Berlin] PHP Notice: Undefined offset: 3 in D:\Xampp\htdocs\Fix\index.php on line 30

[19-Feb-2016 22:46:28 Europe/Berlin] PHP Notice: Undefined index: in D:\Xampp\htdocs\Fix\index.php on line 30

[20-Feb-2016 00:39:13 Europe/Berlin] PHP Notice: Undefined offset: 1 in D:\Xampp\htdocs\Fix\index.php on line 28

[20-Feb-2016 00:39:13 Europe/Berlin] PHP Notice: Undefined offset: 2 in D:\Xampp\htdocs\Fix\index.php on line 29

[20-Feb-2016 00:39:13 Europe/Berlin] PHP Notice: Undefined offset: 3 in D:\Xampp\htdocs\Fix\index.php on line 30

[20-Feb-2016 00:39:13 Europe/Berlin] PHP Notice: Undefined index: in D:\Xampp\htdocs\Fix\index.php on line 30

<?php 

$pageTitle='Списък';

include 'header.php';

$groups=array(1=>'Храна',2=>'Транспорт',3=>'Дрехи',4=>'Други' );



?> <a href="form.php">Добави разход</a> <table style="border: 1px solid red;"> <tr> <td>Дата</td> <td>Име</td> <td>Сума</td> <td>Вид</td> </tr> <?php 

    if (file_exists('data.txt')) {



        $result= file('data.txt');

        foreach ($result as $value) {

            $columns=explode('!', $value);

            echo '<tr> <td>'.$columns[0].'</td>  ----> line 27

            <td>'.$columns[1].'</td>  ----> line 28

            <td>'.$columns[2].'</td> ----> line 29

            <td>'.$groups[trim($columns[3])].'</td> ----> line 30



            </tr>';



        }







    }





    ?> </table> <?php 

include 'footer.php';



?>
Help me find my mistake beginner 'm in PHP.

Recommended Answers

Content of the file?

Jump to Post

I was asking about data.txt

Jump to Post

You have blank lines (line 6) this may be an issue. Anyhow, just replace your loop with this for now and see what's going on:

    $lines= file('data.txt');
    echo "<pre>";
    $x=1;
    foreach ($lines as $line) {
        echo "\nLINE $x: $line \n&nbsp;&nbsp;&nbsp;&nbsp;";
        $m = explode("!",$line);
        print_r($m);
        $x++;
    } …
Jump to Post

All 9 Replies

Member Avatar

Content of the file?

<?php
    mb_internal_encoding('UTF-8');
    $pageTitle = 'Form';
    include 'includes/header.php';

    if($_POST){
        $username=trim($_POST['username']);
        $username=  str_replace('!', '', $username);
        $sum=(float)$_POST['phone'];
        $selectedGroup=(int)$_POST['group'];
        $error=false;
        if(mb_strlen($username)<4){
            echo '<p>Името е прекалено късо</p>';
            $error=true;
        }

        if($sum<0){
            echo '<p>невалиден телефон</p>';
            $error=true;
        }    
        if(!array_key_exists($selectedGroup, $groups)){
            echo '<p>невалидна група</p>';
            $error=true;
        }

        if(!$error){
            $result=$username.'!'.$phone.'!'.$selectedGroup."\n";
            if(file_put_contents('data.txt', $result,FILE_APPEND))
            {
                echo 'Записа е успешен';
            }
        }


    }




    ?> <a href="index.php"></a> <form method="POST"> <div>Име:<input type="text" name="username" /></div> <div>Сума:<input type="text" name="sum" /></div> <div> <select name="group"> <?php
                foreach ($groups as $key=>$value) {
                    echo '<option value="'.$key.'">' . $value .
                            '</option>';
                }
                ?> </select> </div> <div><input type="submit" value="Add" /></div> </form> <?php
    include 'includes/footer.php';
    ?>

I hope you have that in mind The content of the file.
Member Avatar

I was asking about data.txt

woda!2.00!1
чорапи!3.24!3
чорапи!3.24!3
захар!4.50!1
woda!3.50!1

eeeeeeeeeeeeeeeee!56666!3
ererr!345!4
19.02.2016!olio!2.34!1
wwwwwwwww!4.56!3
eeeeeeee!4.56!1
19.02.2016!ssssssss!456!2
19.02.2016!wwww!456!4
weeee!444!3
wwww!344!3
eee!5555!3
aaaa!444!3
20.02.2016!aaaa!4444!2
It is data.txt :)thank you for your patience  :)
Member Avatar

You have blank lines (line 6) this may be an issue. Anyhow, just replace your loop with this for now and see what's going on:

    $lines= file('data.txt');
    echo "<pre>";
    $x=1;
    foreach ($lines as $line) {
        echo "\nLINE $x: $line \n&nbsp;&nbsp;&nbsp;&nbsp;";
        $m = explode("!",$line);
        print_r($m);
        $x++;
    }
    echo "</pre>";

Thank you very much , the error disappeared .How to put the results of an array table ?
How do I add the first column the current date + data array.
I think this is a date (), but do not know how to realize his idea ; (
Your help is invaluable for beginners like me.

Member Avatar

You could try array_pad() to fill out missing values to avoid the unrecognized index issue.

Thanks for the advice. I issue resolved, greetings from Bulgaria

Member Avatar

Give my love to Borovets and Sofia :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.