Jump to content

php flat-file (txt) semi-colon table echoing one record in the table to a html page


malanno
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi:

I'm a java programmer so a brief answer  is what I'm looking for, or a point in a direction.

 

I want access one record in a flat file semicolon delimited txt file? I'm having problems... Thank you

 

# comments start with pound sign - ignore these and blank lines.
VERSION 1.0
# Allowable keywords:  SCD, CCD, BRD, ACU, WCU
# Possible status indicators are:
#                    SCD/CCD/BRD   CHIP/TARGET
#   G = green        online        in use
#   R = red          offline       marked out
#   Y = yellow       partial       disabled
#   N = no color     not installed idle
 
 
# SCD num scd_status
  SCD 0 ; G ;
 
# RACK 0 ----------------------------------------------------------------------------------------------
# CCD num ccd_status  target_status   temp   details
  CCD 0 ; G         ; GGNNNNNNNRRY  ; 34C  ; sn=NCC023e00350010
# BRD num brd_status chip0  chip1  chip2  chip3  chip4  chip5  chip6  chip7  temp  details
  BRD 0   ; G       ;  G      G      G      N      N      N      N      G    ; 37C ; sn=BRD023d00420000
  BRD 1   ; G       ;  N      N      N      Y      Y      N      N      N    ; 37C ; sn=BRD023d00420001
  BRD 2   ; G       ;  G      N      N      N      R      N      N      N    ; 37C ; sn=BRD023d00420002
  BRD 3   ; G       ;  N      N      N      N      R      Y      Y      Y    ; 37C ; sn=BRD023d00420003
  BRD 4   ; Y       ;  N      N      N      N      R      R      R      R    ; 37C ; sn=BRD023d00420004
  BRD 5   ; R       ;  R      R      R      R      R      R      R      R    ; 37C ; sn=BRD023d00420005
Link to comment
Share on other sites

  • Solution

Using fgetcsv you would use the semi-colon as the delimeter. As you loop over the lines when $data[0] starts with BRD, $data[3] will contain the value for the temperature column. The following code will build an array of temperatures, the board column (BRD 0, BRD 1) etc will be used as the index for the array.

<?php

$temps = array();
if (($handle = fopen("data.txt", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
    {
        if(substr(trim($data[0]), 0, 3) == 'BRD')
        {
            $board = trim($data[0]);
            $temp = $data[3];
            $temps[ $board ] = $temp;
        }
    }
    fclose($handle);
}

Change data.txt to the filename your data is stored in. To show the temperature for board 1 (BRD 1) you would use  echo $temps['BRD 1'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.