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 11 of 11

Thread: PHP FlatFile

  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post

    PHP FlatFile

    I've got this clip of php code which doesn't work... I can't figure out why.

    It is supposed to get info from file, and echo it. It should be something like this
    <a href="mvid.php?url=emri">ID</a>

    my info.txt
    Code:
    emri||ID
    emri||ID
    emri||ID
    emri||ID
    emri||ID
    emri||ID
    my php
    Code:
    $dir = 'Muzik/' . $_GET['artist'];
    $Lines = file($dir."/info.txt");
    foreach($Lines as $Key => $Val) 
    {
       $video[$Key] = explode("||", $Val);
    }
    Video Klipi 1: <a href=\"mvid.php?url=$video[0][1]\">$video[0][0]</a><br />
    Video Klipi 2: <a href=\"mvid.php?url=$video[1][1]\">$video[1][0]</a><br />
    Video Klipi 3: <a href=\"mvid.php?url=$video[2][1]\">$video[2][0]</a><br />
    Video Klipi 4: <a href=\"mvid.php?url=$video[3][1]\">$video[3][0]</a><br />
    Video Klipi 5: <a href=\"mvid.php?url=$video[4][1]\">$video[4][0]</a><br />
    Video Klipi 6: <a href=\"mvid.php?url=$video[5][1]\">$video[5][0]</a><br />
    the $video doesn't work. GIves me soething like Array[0] as the link display and Array[1] as the url.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    That looks like it would give you the correct link, what do you get when you do:
    echo '<pre>';
    print_r($video);
    echo '</pre>';

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    I get nothing but when looked as html (view source) I get:
    "<pre></pre>".

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Is the filename and path correct?

    Try adding this right before the foreach:
    echo '<pre>';
    print_r($Lines);
    echo '</pre>';

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    I think my problem is on the foreach because I tried:
    $video = explode("||", $Lines[0]);

    and it worked fine, for the first item.

    anyway, print_r($Line) gave me:
    Array
    (
    [0] => emri1||ID1

    [1] => emri2||ID2

    [2] => emri3||ID3

    [3] => emri4||ID4

    [4] => emri5||ID5

    [5] => emri6||ID6
    )

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Try declaring $video before the foreach, maybe we have a scope issue:
    $video = array();

    good luck;

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    You are probably echoing these in double-quotes? To get array elements with brackets [...] to be parsed correctly, you need to enclose the array variable in curly-brackets {}. The following will fix this -
    PHP Code:
    echo "
    Video Klipi 1: <a href=\"mvid.php?url={$video[0][1]}\">{$video[0][0]}</a><br />
    Video Klipi 2: <a href=\"mvid.php?url={$video[1][1]}\">{$video[1][0]}</a><br />
    Video Klipi 3: <a href=\"mvid.php?url={$video[2][1]}\">{$video[2][0]}</a><br />
    Video Klipi 4: <a href=\"mvid.php?url={$video[3][1]}\">{$video[3][0]}</a><br />
    Video Klipi 5: <a href=\"mvid.php?url={$video[4][1]}\">{$video[4][0]}</a><br />
    Video Klipi 6: <a href=\"mvid.php?url={$video[5][1]}\">{$video[5][0]}</a><br />"

    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    I fixed it. Stupid curly brackets. I had mixed in info from other arrays as well. slipped out.

    By the way, I have a loop to echo all the info. I just typed it out to see if it would work this way and if I had a problem in the array itself or myloops.

    Thanks CFMABI

    Thanks Brandoe for trying.
    Last edited by Erindesign; 10-27-2006 at 11:37 PM.

  • #9
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    You are probably echoing these in double-quotes? To get array elements with brackets [...] to be parsed correctly, you need to enclose the array variable in curly-brackets {}. The following will fix this -
    PHP Code:
    echo "
    Video Klipi 1: <a href=\"mvid.php?url={$video[0][1]}\">{$video[0][0]}</a><br />
    Video Klipi 2: <a href=\"mvid.php?url={$video[1][1]}\">{$video[1][0]}</a><br />
    Video Klipi 3: <a href=\"mvid.php?url={$video[2][1]}\">{$video[2][0]}</a><br />
    Video Klipi 4: <a href=\"mvid.php?url={$video[3][1]}\">{$video[3][0]}</a><br />
    Video Klipi 5: <a href=\"mvid.php?url={$video[4][1]}\">{$video[4][0]}</a><br />
    Video Klipi 6: <a href=\"mvid.php?url={$video[5][1]}\">{$video[5][0]}</a><br />"

    aye, but what gets me is print_r($video) is empty...

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    I posted additional info above, probably while you were typing your last post.

    Edit: We don't know at what point in the code he put the print_r($video)... Also it could have contained a typo.. and printed a non-existant variable...
    Last edited by CFMaBiSmAd; 10-27-2006 at 11:36 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    I put the loop in the same loop I use to echo information. But I got it,thanks


  •  

    Posting Permissions

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