Array help

Discussion in 'PHP' started by bquast, Feb 16, 2008.

  1. #1
    Ok so basically I am pulling a bunch of urls from a file, each on their own new line, now I have come to something I want to do.

    <?php
    $newestdisplay = 25;
    //count the entrys
    while ($counter < $newestdisplay) {
    $counter=$counter+1;
    //echos out the 25 recently added urls / listings
    echo "<a href='goto.php?id=...?...' target='_blank'>".$file[$counter] . "</a><br/>";
    }
    ?>
    PHP:
    Basically this echos out the 25 recent entries to the file..
    But I want to get the line number of them and add it to the ?id=...?... if anyone can help I would love it.

    thanks,
    Brett
     
    bquast, Feb 16, 2008 IP
  2. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #2
    aaron_nimocks, Feb 16, 2008 IP
    bquast likes this.
  3. bquast

    bquast Active Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #3
    Omg I feel like a retard... wow... o wow..stupid me

    i feel stupid, that was very easy to much time looking at script.. gotta start making some coffee.
     
    bquast, Feb 16, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Issue resolved and all that but just here to teach you something new (maybe): you can do $counter++; instead of $counter=$counter+1;

    Shorter and does the same thing.

    Also.. since you didn't define $counter at first, it's obviously starting at 0, and you're adding 1 to it before it gets to its first line, which means you're skipping the very first element of the array (since arrays start with element 0 and not 1). If you actually have something in the element 0 of the array, you should do the $counter increase after the echo.
     
    zerxer, Feb 16, 2008 IP
  5. bquast

    bquast Active Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #5
    nvm got it.
     
    bquast, Feb 16, 2008 IP
  6. bquast

    bquast Active Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #6
    Sorry if this is called bumping but I do have another problem, so maybe a new post would suit, so I will call this a new post.

    so what if i wanted to select a specific entry, say line number 5, how would I go about this, I have been looking at this code for to long already I know it :(

    
    $counter= 0;
     foreach($file as $row) {
       $line = explode("|", $row);
       echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>';
       $counter++;
    }
    PHP:
     
    bquast, Feb 17, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Try:
    
    <?php
    $want = 5;
    
    $counter= 0;
     foreach($file as $row) {
       if ($counter == $want){
           $line = explode("|", $row);
           echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>';
           break;
       }
       $counter++;
    }
    ?>
    
    PHP:
    Edit: There's a better way if you want it. I thought it would be better this way so you can see how your code could be modified.

    Jay
     
    jayshah, Feb 17, 2008 IP
    bquast likes this.
  8. bquast

    bquast Active Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #8
    Exactly what I was looking for, I was almost there, already had the if statement going just couldn't piece the rest together, thanks Jay
     
    bquast, Feb 17, 2008 IP
  9. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Very welcome :)

    The other method is a lot shorter, and will execute quicker (by a few microseconds, anyway):

    
    <?php
    $want = 5;
    
    $line = explode("|", $row[$want]);
    echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>';
    ?>
    
    PHP:
    That should give you the general idea.

    Jay
     
    jayshah, Feb 17, 2008 IP