Member Avatar for osirelkhatim

I would like to create a table based on exploded content of a text file.

My text file contains information about users stored like this:

u0001:Omi Wan Kenomi:123456-12-1234:female:engaged:private sector:ddsfsfsdfsd:603-78860409:6015-2343444:omi.beckett@gmail.com:omiwan:dingo
u0002:Jason:334323-32-2323:male:married:private sector:dfsdfsdfsdf:603-42342342:6015-2321312:jason@gmail.com:jasonlim:dingo1234
u0003:Jina:443342-34-4434:female:engaged:government sector:BLAH BLAH BLAH:603-34334353:6012-7564938:jinadesus@yahoo.com:jina1234:dingo1234
u0004:Gail:432424-34-3242:male:single:private sector:Hell's Kitchen:603-32423423:6015-2432423:gail@gmail.com:gail1234:dingo1234
u0005:Deana Zafir:434435-35-3564:female:single:student:Melbourne Uni:603-44442345:6015-5356343:deanazafir@hotmail.com:Deana1234:dingo1234

To create a table within echo in php, I tried this code, but it wont work, the data displayed in the table is simply:

UserID : .Array[0].
Name : .Array[1]. IC : .Array[2]. Name : .Array[]. //as you can see, it's in some kind of format but not table.

Here's the code:

$facename= $_SESSION["facename"];

            $UserInfo = file('datatext/users.txt');
            echo "<table>";

        foreach($UserInfo as $tag => $info)
        {
    $user[$tag] = explode(":",$info);
        }

            for($i=0;$i<=count($info);$i++)
            {

             if($user[$i][10] == $facename)

            {

             echo "<tr>";
        echo "<td> UserID </td>";
        echo "<td> : </td>";
        echo "<td> .$user[$i][0].</td>";
        echo "</tr>";
        echo "</table>";


             echo "<tr>";
        echo "<td> Name </td>";
        echo "<td> : </td>";
        echo "<td> .$user[$i][1].</td>";
        echo "</tr>";
        echo "</table>";


             echo "<tr>";
        echo "<td> IC </td>";
        echo "<td> : </td>";
        echo "<td> .$user[$i][2].</td>";
        echo "</tr>";
        echo "</table>";


             echo "<tr>";
        echo "<td> Name </td>";
        echo "<td> : </td>";
        echo "<td> .$user[$i][].</td>";
        echo "</tr>";
        echo "</table>";


            }

}
echo "</table>";
?>              

However, when i simply echo the exploded array like this:

            echo $user[$i][0]; 
            echo $user[$i][1];
            echo $user[$i][2];
            echo $user[$i][3];
            echo $user[$i][4];
            echo $user[$i][5];
            echo $user[$i][6];
            echo $user[$i][7];
            echo $user[$i][8];
            echo $user[$i][9];
            echo $user[$i][10];
            echo $user[$i][11];

it works just fine, but the data is printed out in one long string.

I would like to know what I'm doing wrong, thank you :)

You are aware of the fact that you have

for($i = 0;$i <= count($info);$i++)
{

which loops through the info of a user, not through the array of users, and that in your echo's you are trying to access a user using the counter of the user info loop instead of the users loop? :)

I'm not sure if this will work, but I think it should:

$facename = $_SESSION["facename"];

// The file() function will just put every line in your file in a new array record. Therefore
// I've named your var $lines instead of $UserInfo.
$lines = file('datatext/users.txt', FILE_IGNORE_NEW_LINES);

echo "<table>";

// Check if $lines exists, as a foreach() loop will generate an error if it does not.
if($lines)
{
    // No special keys are present, therefore I've replaced the "$key => $value" part by just $value.
    foreach($lines as $line)
    {
        // Convert the line to a user.
        $users[] = explode(':', $line);
    }
}

// Uncomment if you would like to check the value of $users here.
// echo '<pre>'; print_r($users); echo '</pre>';

for($i = 0; $i < count($users); $i++)
{
    $user = $users[$i];

    if($user[10] == $facename)
    {
        //* Apparently we've found a user you need?

        // Echo some stuff.
        echo "<tr>";
        echo "<td> UserID </td>";
        echo "<td> : </td>";
        echo "<td> .$user[0].</td>";
        echo "</tr>";
        echo "</table>";
        echo "<tr>";
        echo "<td> Name </td>";
        echo "<td> : </td>";
        echo "<td> .$user[1].</td>";
        echo "</tr>";
        echo "</table>";
        echo "<tr>";
        echo "<td> IC </td>";
        echo "<td> : </td>";
        echo "<td> .$user[2].</td>";
        echo "</tr>";
        echo "</table>";
        echo "<tr>";
        echo "<td> Name </td>";
        echo "<td> : </td>";
        echo "<td> .$user[].</td>";
        echo "</tr>";
        echo "</table>";
    }
}

echo "</table>";

P.S. Here's a link to the file() function, in case you would like to recheck the manual :).

Member Avatar for osirelkhatim

Minitauros, thanks man, this worked well :)

Great! Good to hear :).

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.