Input in txt/html/php file - output in html?

Discussion in 'Programming' started by Dzonny, Jan 24, 2014.

  1. #1
    Hello there all,

    I have a website built for a local business and I need something like this if that's even possible to build.

    So I would need something like ability to add some info (text and links) in some .txt file on server and to be able to display those informations based on lines in that txt files.
    For example, let's say that I have following text file:
    Title example 1
    Description example 1
    Link example 1
    Title example 2
    Description example 2
    link example 2
    Code (markup):
    So I would need to display "Title example 1" on my website through html, so I can apply some style to it like this:
    <Td>
              <Div class="EntryDesc">
                <a href="LINK1" class="blog-article-a"><h3>Title example 1</h3></a>
                <p>Description example 1</p>
              </Div>
            </Td>
    HTML:
    So i need to use it inside html code, based on rows in txt (doesn't have to be txt though) if that's possible.

    Any suggestions please?
    Thanks!
     
    Dzonny, Jan 24, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Well, you've got a LOT of issues there, and I'm having a hard time finding an actual question in your post.

    For what I'm seeing wrong though, I'm wondering what makes those tabular data, why you need the extra DIV if they are tabular data, why you have a block level tag (h3) incorrectly placed inside an inline-level tag (a) even if the HTML5-tards say it's ok now (it isn't, still broken)... and of course if there's a bunch of those TD and DIV what do they need classes for (use the parent as the selector).

    ... so the markup is garbage and should be pitched. Still not sure what you're actually asking though.
     
    deathshadow, Jan 24, 2014 IP
  3. Dzonny

    Dzonny Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Nevermind the html, I'm aware of the mentioned problems there, I didn't wrote it - I just took it as an example from the previously made code on site.
    The actual question is how to display specific line from txt file on server inside html code? For example 5th or 6th line if possible.
     
    Dzonny, Jan 24, 2014 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    I believe this will work, I didn't run the code and I haven't done something like this in PHP in a while not to mention this probably isn't the "best" way to do it but it should work

    
    $items = file_get_contents("textFile.txt");
    $items = explode("\n", $items);
    
    $list = "a";
    
    foreach($items as $item)
    {
    if($list == "a")
    {
    echo "Content Before Title Here";
    echo $item;
    echo "Content After Title Here";
    $list = "b";
    }
    else
    {
    echo "Content Between Title and Description Here";
    echo $item;
    echo "Content After Description Here";
    $list = "a";
    }
    }
    
    PHP:
     
    Pudge1, Jan 24, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Assuming that it is PERFECTLY three lines per item, I'd read it one line at a time with fgets so as to keep memory use down -- file_get_contents and the split array Pudge1 suggested has a lot more overhead.

    if ($txtFile = @fopen('data.txt')) {
    	while ($title = fgets($txtFile, 256)) {
    		if ($description = fgets($txtFile, 4096)) {
    			if ($url = fgets($txtFile, 2048)) { // use IE's max length
    			
    				echo '
    					<h3><a href="', $url, '">', $title, '</a></h3>
    					<p>', $description, '</p>';
    					
    			} else {
    				/* add error handler for premature end of file here */
    			}
    		} else {
    			/* add error handler for premature end of file here */
    		}
    		fclose($txtFile);
    	}
    } else {
    	/* add error handler for being unable to open file here */
    }
    Code (markup):
    really though that file format would be VERY fragile, one extra linefeed and you're screwed up. I'd probably suggest using a non-text rarely used character as a element divider and another as a row divider; you could even use the old ASCII control codes for that.

    NOT that any of this would work on my server since I block fopen, file_get_contents, readfile and a host of other functions as being security holes / things PHP has no business doing.
     
    Last edited: Jan 24, 2014
    deathshadow, Jan 24, 2014 IP
  6. Dzonny

    Dzonny Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    Thanks for your answer guys :)

    I did it by using:
    $lines = file('file.txt');
    echo $lines[0];
    echo $lines[4];
    PHP:
    Looks like it's pretty simple actually, and it did the trick for me! :)

    The only thing left is how to use some specific line, for example line 5, in CSS, like this:
    #blog1 { background: url("LINE 5 HERE") center center;}   
    Code (markup):
    I don't know how to add fifth line there, i tried something like:
    #blog1 { background: url("echo $lines[4]; ") center center;}   
    Code (markup):
    But it won't work?
     
    Dzonny, Jan 25, 2014 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    ...You can't just implement PHP directly into CSS
     
    Pudge1, Jan 25, 2014 IP
  8. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #8
    It looks like you have the answer already. One thing I have is a code that read straight from a CSV file but sometimes things go wrong (for whatever reason) and you end up with a broken site. I found it easy to pull it into sql . This way if the update can't run due to incorrect format and layout, the live site will still work.

    It also gives access to former records, which is great if you want to have history or archive or something on your site.

    So IMHO look into pulling the data into SQL (from the file) and it well be easier to work with
     
    matt_62, Feb 19, 2014 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    If you need to use the info taken from the file directly in CSS, you'll have two options - either parse the CSS-files as PHP (ie, make a CSS-file run via the PHP-parser, then you can use PHP directly in the CSS-file) - however, this is not really a good way to solve the problem, as it creates a bunch of other issues.
    The other way is to either include the CSS in the PHP-file within <style>-tags, or directly on the element, via inline-CSS.
     
    PoPSiCLe, Feb 20, 2014 IP