Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Parse txt (or xml) - output to var & call later in page

    Hi, Im stuck parsing a document into lines and then echo'ing them out via a variable to certain lines of html code.

    IE:

    I have in the header:

    <?PHP
    $textfile= "comments.txt";
    $handle = fopen($textfile, "r");
    $contents = fread($handle, filesize($textfile));
    fclose($handle);
    $array01 = explode("&", $contents);
    echo $array01[1];
    ?>

    the txt file "comments" contains (for this example):

    &ONE
    &TWO
    &THREE

    .. so far I can simply echo out either line one two or three via the array denoted by "&" .. great. Now I need to set this so I can call this echo in certain places of my html code. The text will be pulled in and placed by choice for ALT (im using overlib) text on mousover. So far I have:

    <a href="javascript:sendEvent('playitem',0)" onmouseover="return overlib('<?php echo $array01[1];?>', CAPTION, '(click to listen)', STATUS, 'click to listen', FGCOLOR, '#d8e0e3', BGCOLOR, '#175b7b', BORDER, 3, CAPTIONFONT, 'Verdana', TEXTFONT, 'Verdana', TEXTSIZE, 1, ol_width=400, ol_offsetx=12, ol_offsety=17);" onmouseout="return nd();">

    (the rest of the line continues using overlib commands)

    this in my very basic PHP mind should show "ONE" in my mouseover, but it just causes a JS error in the browser "unterminated string constant".

    I know this is basic .. im new to PHP so I may be causing a red herring.

    I simply want a text file or xml than can be parsed, split into lines that I can ID and echo out into the html line you see above so each line of text on mouseover via overlib is the text from the document per line.

    I see examples where its echoed out to a table etc but I need it to be called/echoed within my specific html line using a variable, which is where I get stuck.

    all help welcomed, example code and description would be great
    Last edited by Barriestard; 03-27-2007 at 07:32 PM.

  • #2
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Barriestard, I'm no guru so don't quote me on this but maybe you should try using
    PHP Code:
    <?print_r($array01[1]);?>
    im not so sure that yuo can just echo arrays that way. but i could be wrong. Let me know

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    this in my very basic PHP mind should show "ONE" in my mouseover, but it just causes a JS error in the browser "unterminated string constant".
    Load the PHP file in your browser, right click the page and choose View Source.

    Copy the JavaScript line and post it on here. Perhaps you are getting a warning.

    im not so sure that yuo can just echo arrays that way. but i could be wrong. Let me know
    The print_r() function is only useful for debugging purposes. And it only works good on arrays. So using print_r() on a single element from a 1 dimensional array wouldn't prove useful. It is perfectly fine to echo the single element with echo or print.

    Your code appears to be correct (at a glance). I do recommend naming your variables to something more logical than array01.

  • #4
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The print_r() function is only useful for debugging purposes. And it only works good on arrays. So using print_r() on a single element from a 1 dimensional array wouldn't prove useful. It is perfectly fine to echo the single element with echo or print.
    ohh i never knew it was only useful in debugging. The only times i've needed to use it so far is debugging so I hadn't found out that it doesn't work for the uses I was talking about...
    Thanks

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well if you send print_r() a string or an integer or a float, then yeah it acts just like print or echo, so I guess it's ok to use... it's just more "correct" to use echo or print. print_r as aedrin mentioned gives you extra information about an array or an object which wouldn't be of any interest to your end-user but is very useful to us as the developer.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •