Development Learning PHP - any advice appreciated

Discussion in 'Software' started by Zoon, 26 Mar 2009.

  1. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    Well I say "learning", but I've been writing basic-ish PHP apps for a while now. The problem I'm getting is that I'm doing something I've never done in PHP and although I've spent a day on Google no-one seems to answer my question.

    With other programming languages, when reading from a database, there's usually an exposed property indicating which is the first row, and which is the last row.

    I can't seem to find this in PHP, any help?

    I've noticed that I could use a for( x to y) loop and check the row number this way but I'm struggling with working out the syntax for this as everything I've read insists on using a while() loop (which is of course the only way I've ever done it before myself).

    Thanks :wallbash:
     
  2. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    I've now got a workaround I'm kinda happy with in place but if anyone does know if there's a built in isfirstrow or islastrow for arrays I'd really like to know! :)
     
  3. Red Eye

    Red Eye Minimodder

    Joined:
    16 Apr 2008
    Posts:
    235
    Likes Received:
    14
    So what was your solution Zoon?

    In perl, $#array represents the length of the array @array. So as you iterate through the array you can check if your counter equals the last element of the array, hence $#array.

    perl and php are similar so you may be able to use this.
     
  4. hitman012

    hitman012 Minimodder

    Joined:
    6 May 2005
    Posts:
    4,877
    Likes Received:
    19
    PHP:
    if ($k == count($array))
    where $k is your loop variable. You could use a structure like:

    PHP:
    while ($k count($array)) {
       
    // blah blah
       
    $k++; 
    }
    or its for equivalent.
     
  5. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    I ended up using this:

    Code:
    $rownum = 1;
    
    while ($row = mysql_fetch_assoc($results))
    {
    
    do stuff
    
    $rownum++
    }
    The most important bit was knowing if its the first row.

    I can work out the last by comparing $rownum and mysql_num_rows($results) before doing $rownum++.

    Thanks for the suggestion Red Eye but that doesn't seem to be exposed. You can count() your array but you still need to iterate a number through it to know what the current row number is, with the code I'm using.

    I'm finding C# and .Net easier for this project but I want people to actually use this stuff and who likes hosting on IIS???
     
  6. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    It sounds like you're used to getting stuff from a db in an some kind of object with stuff like .movenext or similar?

    You can build your own encapsulation, something like the following (thrown together, assuming you're using php4, not tried or tested in any way! it also assumes you give it a proper array, not a hash table):

    PHP:
    <?php

        
    class Rowset
        
    {
            var 
    $rows;
        
            function 
    Rowset($rows)
            {
                
    $this->rows $rows;
            }
            
            function 
    moveNext()
            {
                return 
    next($this->rows);
            }
            
            function 
    movePrev()
            {
                return 
    prev($this->rows);
            }
            
            function 
    getRow()
            {
                return 
    current($this->rows);
            }
            
            function 
    reset()
            {
                
    reset($this->rows);
            }
            
            function 
    isLast()
            {
                return 
    key($this->rows) == sizeof($this->rows)-1;
            }
            
            function 
    isFirst()
            {
                return 
    key($this->rows) == 0;
            }
        }
        
        
        
    $rows = array('one''two''three''four''five');
        
        
    $res = new Rowset($rows);
        

        do
        {
                    if (
    $res->isFirst()) echo "(we are on the first row!)";
                    if (
    $res->isLast())  echo "(we are on the last row!)";
            echo 
    $res->getRow(), "\n";
        }
        while (
    $res->moveNext());
    That'll output:

    Code:
    (we are on the first row!) one
    two
    three
    four
    (we are on the last row!) five
    
    You'd need to swap:

    PHP:
    $rows = array('one''two''three''four''five');
    with

    PHP:
    // [your other code here]
    while($row mysql_fetch_assoc($results))
    {
     
    $rows[] = $row;
    }
    to use this in your code
     
    Last edited: 26 Mar 2009
  7. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    That's really useful thanks RTT :D

    Its a shame php just doesn't have an evaluator which would tell you if its the first row or last row.

    And yeah that's sort of where I'm coming from. Back in the days of asp you could do recordset.eof :)

    Its been a while since I did any serious development and its always a brain ache dredging up your memories of how things work!
     
  8. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Thought so :)
     
  9. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    In honesty this problem only exists because I'm hacking an open source product we use at work to do something the original writer never intended.

    I am however writing a hopefully superior one myself, so I will design it to avoid this pitfall from the ground up :)
     

Share This Page