Very ambitious While loop

Discussion in 'PHP' started by JasonGreen, Oct 3, 2009.

  1. #1
    I have a php file with lines of data.

    In order they are 1)pc serial number, 2)month purchased, 3)day of month purchased, 4)number of days serviced(taken away from users), and 4)original cost

    each of these values are separated by a space

    Is it possible to include 'pcinfo.php' into another php file and use strtok (I'm sure with rtrim) to retrieve all of the data one line at a time (or 4 values at a time) using a While Loop instead of print for each line?

    I've been experimenting for days, but am getting very little. Thanks
     
    JasonGreen, Oct 3, 2009 IP
  2. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #2
    why do you need it retrieved one line at a time?

    try this:
    Your entire list will be in $this_array, one line per element. You can proceed to count them or explode them into another array or sub-elements so that you can work with them individually.

    If you have other requirements please share them :)
     
    goliath, Oct 3, 2009 IP
  3. JasonGreen

    JasonGreen Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the response. It doesn't seems like an array would be optimal, unless I'm doing it wrong. This original php file of data is in php, not text. Which is why I was attempting to use strtok, my goal is to use as little code as possible to display the data.

    Thanks
     
    JasonGreen, Oct 3, 2009 IP
  4. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #4
    OK man,

    But I think you're confused, maybe a C programmer? Why tokenize or trim if the format is as you've described?

    
    <?php
        $this_array = file_get_contents ("pcinfo.php");
    
        foreach ($this_array as $line)
        {
             if (trim $line) != "")
             {
                  $data_array[] = explode (" ", $line);
             }
        }
    
        foreach ($data_array as $pc_info)
        {
             echo "Serial = ".$data_array[0]."<br>\n";
            echo "Month = ".$data_array[1]."<br>\n";
            echo "Date = ".$data_array[2]."<br>\n";
            echo "Service = ".$data_array[3]."<br>\n";
           echo "Cost = ".$data_array[4]."<br>\n";
    
         }
     ?>
    
    Code (markup):
    Sorry for the edits I'm having trouble posting this.

    If your description of the format was accurate, that code (less than 500 chars including output formatting and whitespace) should print the contents of your pcinfo file to the screen with each value labeled. That's not what you're looking for?

    Your not going to get output with much less code than that. And this code also checks for blank lines in the file, bonus feature!

    If you use a while loop, even while(not EOF) then manually read the file line by line, triming and tokenizing each string/substring along the way, and then do your output, you're going to be looking at loads more code than this, I think.
     
    Last edited: Oct 3, 2009
    goliath, Oct 3, 2009 IP
  5. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Hey and I didn't mean any offense to C programmers :)

    I said that because it sounds like the OP was thinking that he would have to write a load of array handling code, or making a lower-level language assumption.

    Load file -> parse file -> display data is what PHP was written for. There are loads of higher level tools for these sorts of tasks that you wouldn't expect to find in a lower level language.

    That's all.
     
    goliath, Oct 3, 2009 IP
  6. JasonGreen

    JasonGreen Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry for the confusion. I understand and you make a valid point. Let me show you a bit of what the file looks like

    <?php
    $serialpurchase =
    "GXC1233 5 9 19 1124.95
    GXB1F65 3 7 5 795.13
    GXF1977 9 4 2 1229.
    FXG4986 9 17 5 497.35
    etc.


    this is why I'd like an easy way to tug it out. Thanks again. Perhaps I'm trying to make it too easy?
     
    JasonGreen, Oct 4, 2009 IP
  7. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #7
    So, they're all jammed into one giant PHP string?

    Is that generated or a static file?

    Is there any reason for that?

    Not that I need answers, heh, just rehetoric.

    I would say the easiest way to solve your problem is to delete that PHP wrapper and process the data itself. However, I don't know your requirements, so let's assume you HAVE to work with that format.

    
    
    <?php
        include ("pcinfo.php");
    
        $this_array = explode ("\n", $serialpurchase);
    
         foreach ($this_array as $line)
        {
             if (trim $line) != "")
             {
                  $data_array[] = explode (" ", $line);
             }
        }
    
        foreach ($data_array as $pc_info)
        {
             echo "Serial = ".$data_array[0]."<br>\n";
            echo "Month = ".$data_array[1]."<br>\n";
            echo "Date = ".$data_array[2]."<br>\n";
            echo "Service = ".$data_array[3]."<br>\n";
           echo "Cost = ".$data_array[4]."<br>\n";
    
         }
     ?>
    
    
    Code (markup):
    That changes the way $this_array is generated and then proceeds into the same parse. Please note that depending on what generates your file you may have to adjust the "explode" statement to include the appropriate line separator.
     
    goliath, Oct 4, 2009 IP