Why does this code snippet keep getting Undefined offset: 2, Undefined offset: 7, Undefined offset: 11, respectively? I'm not sure where I'm going wrong,

for($i = 0; $i < $user_length; $i++) // CHECKS IF THE USERNAME, FIRST NAME, IC NUMBER AND CONTACT NUMBER IS ALREADY BEEN REGISTERED AS A MEMBER
                    {
                        $userdetail = explode(":", $user[$i]);
                        if ($_POST['ic'] == $userdetail[2])
                        {
                            echo "<h2>Sorry!</h2><p>The ic no is already in use!</p>";
                            $error = 1;
                            $i = $user_length;
                        }
                        else if ($_POST['phone'] == $userdetail[7])
                        {
                            echo "<h2>Sorry!</h2><p>phone no is already in use!</p>";
                            $error = 1;
                            $i = $user_length;
                        }
                        else if ($_POST['facename'] == $userdetail[10])
                        {   
                            echo "<h2>Sorry!</h2><p>The Username you provided is already in use!</p>";
                            $error = 1;
                            $i = $user_length;
                        }

                    }

Here's the text file format the explode is reading from:

u0003:omied:123456-12-1234:male:married:privatesector:dfsfsdf:603-32323242:6016-2343432:omied@gmail.com:omeid123:dingo1234

as you can see, ic, phone, and facename are defined as 2, 7 and 10 following the format of the loop and array.

Recommended Answers

All 7 Replies

Need a little more snippet.. e.g. where did $user_length come from ?
is u0003:omied:123456-12-1234:male:married:privatesector:dfsfsdf:603-32323242:6016-2343432:omied@gmail.com:omeid123:dingo1234 associated with a variable?

Here's my first few lines of code:
User_length was basically used to read the number of users in the txt file. `

$error = 0;
// $no = 1;
// $no = sprintf('%04d',$no);   // SETS THE NUMBER TO BE A 4-DIGIT NUMBER WITH LEADING ZEROS

$user = file("datatext/users.txt");
$user_length = count($user);

if($user_length > 0)
{
    $lastRegId = explode(":", $user[$user_length-1]);
}
else
{
    $lastRegId = 0;
}

$newRegId = sprintf('%04d', (ltrim($lastRegId[0], "u") + 1 ));


    // WRITE ALL THE INFORMATION INTO A SINGLE 'STRINGDATA'
$output= "u".$newRegId.":".$_POST['name'].":".$_POST['ic'].":".$_POST['gender'].":".$_POST['marital'].":".$_POST['work'].":".$_POST['address'].":".$_POST['phone'].":".$_POST['mobile'].":".$_POST['email'].":".$_POST['facename'].":".$_POST['password'];

?>`

I learn something everyday :-) file()

You should use foreach not for to iterate through your user records.

Here's my first few lines of code:
User_length was basically used to read the number of users in the txt file. `

$error = 0;
// $no = 1;
// $no = sprintf('%04d',$no);   // SETS THE NUMBER TO BE A 4-DIGIT NUMBER WITH LEADING ZEROS

$user = file("datatext/users.txt");
$user_length = count($user);

if($user_length > 0)
{
    $lastRegId = explode(":", $user[$user_length-1]);
}
else
{
    $lastRegId = 0;
}

$newRegId = sprintf('%04d', (ltrim($lastRegId[0], "u") + 1 ));


    // WRITE ALL THE INFORMATION INTO A SINGLE 'STRINGDATA'
$output= "u".$newRegId.":".$_POST['name'].":".$_POST['ic'].":".$_POST['gender'].":".$_POST['marital'].":".$_POST['work'].":".$_POST['address'].":".$_POST['phone'].":".$_POST['mobile'].":".$_POST['email'].":".$_POST['facename'].":".$_POST['password'];

?>`

no, foreach isn't going to work when commencing a loop.

Of course it is. From the PHP help file:-

<?php
// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
Member Avatar for diafol

Just an observation for the getting of the last id: if this file is going to be large, then you may wish to use fseek() using the SEEK_END to go to the end of the file and work backwards. This way, you don't end up loading the whole file into RAM.

However, if it's just a list of names, I doubt it will be that large. Example:

function readLastLine ($file) {
    if(!file_exists($file)) die("No file found"); //testing
    //FROM THE PHP MANUAL
    $fp = @fopen($file, "r");
    $pos = -1;
    $t = " ";
    while ($t != "\n") {
        if (!fseek($fp, $pos, SEEK_END)) { 
            $t = fgetc($fp);
            $pos = $pos - 1;
        } else {
            rewind($fp);
            break;
        }
    }
    $t = fgets($fp);
    fclose($fp);
    return $t;
}

function getLastID($file, $glue=":") {
    $t = readLastLine($file);   
    if(empty($t))die('Empty File'); //testing
    list($id) = explode($glue, $t);
    return $id;
}


echo getLastID("me.csv",":");

I realise that's not the question you asked, but I thought I'd throw it in.

In addition, you may want to clean/escape (especially ' " ' characters) input in your $_POST variables before inserting them into a file.

Be a part of the DaniWeb community

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