• 0

[PHP] Read File Lines


Go to solution Solved by SinsOfCube,

Question

xScence

Good night to all.
I have this problem:
 

I have this function > 

<?php
function read_items($data_to_read){
if(is_file($data_to_read)){
$file = fopen ($data_to_read, 'r');
while ($current_data_item = fgets($file) !== FALSE){
$item_data_info = fscanf($file, "%d %d %[^\n]s"); 
list($item_primary_key, $item_id, $item_name) = $item_data_info;
if(!empty($item_name) && !empty($item_id)){
echo '<option value="'.$item_id.'">'.$item_id.'</option>';
}
}
fclose($file);
}
}
?>

And i use it like this

<?php read_items('items.txt'); ?>

In Items.txt i have this string lines:

 

1 2621441 Dagger
2 2621442 Scout Dagger
3 2621443 Dirk 
4 2621444 Brave Dirk
5 2621445 Wedge Blade
6 2621446 River Stiletto
7 2621447 Venom Stinger
8 2621448 Onyx Eye Stinger
9 2621449 Blook Dirk
10 2621450 Crystal Gouge

 

And the output is this

http://picbg.net/img.php?file=2664edc757590d52.png

Its shows me 5 items not all (10) -.- dunno why.
Any suggestions... about how to get all lines it skips the 2621441, 2621443, 2621445 etc..

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
SinsOfCube

the fgets and fscanf function both grab the next line from the filepointer, so fscanf only reads every other line you need to change it to:

 

<?php
function read_items($data_to_read){
    if(is_file($data_to_read)){
        $file = fopen ($data_to_read, 'r');
        while ($item_data_info = fscanf($file, "%d %d %[^\n]s"); !== FALSE){
            list($item_primary_key, $item_id, $item_name) = $item_data_info;
            if(!empty($item_name) && !empty($item_id)){
                echo '<option value="'.$item_id.'">'.$item_id.'</option>';
            }
        }
        fclose($file);
    }
}
?>
  • Like 1
Link to post
Share on other sites
  • 0
xScence

 

the fgets and fscanf function both grab the next line from the filepointer, so fscanf only reads every other line you need to change it to:

<?php
function read_items($data_to_read){
    if(is_file($data_to_read)){
        $file = fopen ($data_to_read, 'r');
        while ($item_data_info = fscanf($file, "%d %d %[^\n]s"); !== FALSE){
            list($item_primary_key, $item_id, $item_name) = $item_data_info;
            if(!empty($item_name) && !empty($item_id)){
                echo '<option value="'.$item_id.'">'.$item_id.'</option>';
            }
        }
        fclose($file);
    }
}
?>

Thank you, you helped me alot. Regards 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By D!ABOL!C
      I'm not sure if this should be in the server section, but here it goes.
       
      This is the second time I've had this issue occur. It appears that someone is getting a piece of malware that is infecting the public share on the server. I've been able to restore the data from backup, so we are OK on that front, but obviously that is not a solution to the problem if this keeps happening.
       
      I just wanted to get opinions on how to tackle this. I talked with Trend Micro as they are the antivirus we are using and they said the ways these work is that they release the payload and then then it auto deletes itself form the infected machine. So if we try to do a malware scan, we won't find any traces of it.
       
      My thoughts are first, to remove Admin rights from all the machines. I guess my second thought would be to see if any of the users have any local files that are encrypted as well.
       
      Any thoughts would appreciated.
       
      This is an all windows environment, Windows 7 Pro workstatations with SBS2011 as the only server.
    • By lutherfchen
      Hello guys, I'm wondering if i can connect my users with text files.
      My text file looks like this:
      [data]
      Password = asd
      Email = asd@asd
      .
      .
      .
      ...
      [end]
       
      So I have my code like this:
      $username = $_GET['username'];
      $password = $_GET['password'];
      bla. bla. bla
       
      But I don't know how to do it to check if the password inputted is the same as the one in the text file.
       
      Can someone help me with this code?
    • By Turk.
      By Amit Agarwal Apr 23, 2014
      A school teacher wants to have a public drop box (not Dropbox) where students can upload homework assignments. A recruiter wants to have an online form where job applicants can upload their resumes. A news organization may need a public drop box where people can upload files anonymously.


      Google Forms would have been a perfect solution here but unfortunately you cannot upload files to Google Drive through Forms.

      The other option is to have a shared folder inside Google Drive but there are limitations. One, you need a Gmail Account and should be invited by the folder owner to upload files in a shared folder. Second, all collaborators can view and even remove files that have been uploaded in a shared folder on Google Drive.

      Receive Files in Google Drive with Forms

      What you can do is create a regular web form (written in HTML and CSS) and then use  Google Apps Script  to post the content of this form into Google Drive.

      Before diving into the implementation, take a look at this sample form. When you upload a file, it will show up in a specific folder on my Google Drive. You don?t even need to have a Google Account to upload files and the Google Script based form even works on mobile devices.
      ?Click here to make a copy of the Google Script into your Google Drive.
      ?This is vanilla form with a text field, a file input field and a form submit button. You can open the form.html file to apply your own CSS styles or add more input and textarea fields.
      ?From the Run menu, choose doGet and authorize the script. The script needs these permissions since the form will be uploading files to your Google Drive.
      ?Next choose Deploy as Web App from the Publish menu. Click Save New Version, choose Anyone, even Anonymous from the drop-down and click the Deploy button.

      The Google Script will now offer you a form URL. Anyone can now use this form to upload files to your Google Drive.

      This is a bare-bones version of the form but it can be further enhanced through Apps Script. For instance, you may choose to save the form details in a Google Spreadsheet along with the Drive URLs of the uploaded files. The script can even email you the uploaded files as attachments similar to Mail Merge.
      If this looks a bit too technical, you can ask guests to send you files as email attachments and then use the Gmail to Google Drive program to automatically save these file attachments in your Drive.

      [**] If you aren?t on Gmail or Google Apps, you can still receive files in Dropbox from anyone anonymously.
      Source
    • By Turk.
      By A-V Comparatives 18 April 2014
       
      Factsheet Real-World Protection Test March 2014
      http://www.av-comparatives.org/factsheet-real-world-protection-test-march-2014
      File Detection Test March 2014
      http://www.av-comparatives.org/file-detection-test-march-2014
       
      March 2014 File Detection Awards List, by Vendors and by Tests
      http://chart.av-comparatives.org/awards.php?year=2014

    • By Brian Miller
      Can someone help to find the layered PSD version of this generic Mac file icon?
      I would like to make some new icons based on this image, with my image appearing underneath the folded portion of the document.
      I found lots of "PSD" versions of the file, but they were all flattened versions; they might as well have been a PNG file!

      Thanks.