Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:55:24+00:00 2026-05-27T11:55:24+00:00

Php noob. I have this text file (names.txt) with comma separated names and codes.

  • 0

Php noob.

I have this text file (names.txt) with comma separated names and codes. Looks similar to this:

John doe, 123456, 876543
Mary Ann, 456878
Ben Anderson, 987554, 097532, 873445

As you can see each name can have a different amount of codes ranging from one up to 10.
What I want to do is to output this information as a table

I tried this:

<table>
<?php
$file_handle = fopen("names.txt", "rb");

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);

echo "<tr>";
echo "<td><strong>" . $parts[0] ."</strong></td>";
echo "<td>" . $parts[1] . "</td>";
echo "<td>" . $parts[2] . "</td>";
echo "<td>" . $parts[3] . "</td>";
echo "<td>" . $parts[4] . "</td>";
echo "<td>" . $parts[5] . "</td>";
echo "<td>" . $parts[6] . "</td>";
echo "<td>" . $parts[7] . "</td>";
echo "<td>" . $parts[8] . "</td>";
echo "<td>" . $parts[9] . "</td>";
echo "<td>" . $parts[10] . "</td>";
echo "</tr>";
}

fclose($file_handle);

?>
</table>

This does not work because sometimes $parts[] will be empty and there is nothing to output and I get an error. Undefined offset.
How would I go about doing this and only “echo” the $parts[] when it has a value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:55:25+00:00Added an answer on May 27, 2026 at 11:55 am

    Instead of echo’ing all the parts like you do know (the 10 lines), you could replace it by something like this:

    for($i = 1; $i <= 10; $i++) {
        echo '<td>' . (!empty($parts[$i]) ? $parts[$i] : '') . '</td>';
    }
    

    What does it do: loop 10 times and echo always an . Inside the it checks if $parts[] for that iteration is not empty, and if not echos it. Else it echos just nothing.

    You still want all ‘s to be there, else your table would get ruined (would look strange to the end user).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP, also a complete noob at it. So I have this URL
I'm a PHP noob. I have a database that I will update weekly with
I apologize in advance, I am a PHP noob! I have form with some
This might be a noob question, but can't find an answer anywhere. I have
sorry for the (probably) noob question, but I', new at this stuff. i have
I have a multi part question for a php script file. I am creating
I am new to URL rewriting and I have an .htaccess file that looks
I have a list similar to this (with an arbitrary amount of li's): <ul>
I'm a real noob with PHP, so here' goes. I have a script to
I am a totally noob at CakePHP and have been dealing with this site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.