views:

190

answers:

5
$i=0;
while ($row=mysql_fetch_assoc()) {
if ($i==0) echo "First"
$i++;
}

Access directly to mysqli pointer? a php class like's Iterator?

Thanks.

A: 

If you're trying to use the first element for something special, then maybe something like:

$row=mysql_fetch_assoc();
//do stuff to first row
do {
    //do stuff to all rows (including the first)
} while ($row=mysql_fetch_assoc());

Otherwise I have no idea what the question is and I'm not a PHP guy...

Cogsy
+1  A: 

I’d use this:

if ($row = mysql_fetch_assoc()) {
    // process first item
    while ($row = mysql_fetch_assoc()) {
        // process following items
    }
}
Gumbo
-1! I wouldn't use this. A while loop only executes while TRUE, which is checked first. Therefore the IF is duplicitous as the loop wouldn't execute anyway if left alone.
The Wicked Flea
@The Wicked Flea: That's wrong. The first $row = mysql_fetch_assoc() will check if there is any data (false if none), the second one will check if there is more than one row in the result set (mysql_fetch_assoc() moves the internal result pointer). So Gumbo is absolutely right.
Stefan Gehrig
@The Wicked Flea: That’s wrong. If the `if` contition is true, `$row` would hold the first record and can be prcessed in “process first item”. And only if there are more than one record, the `while` condition will be true and iterate the rest.
Gumbo
Yet you duplicate what should happen. The boolean flag is a better method, because while always does an IF-check before executing its contents. If there are no rows, the interior is never executed. When a boolean/integer is initialized first and then maintained you can have your exception ... rather than a whole block of code for the first item, and a whole block for the rest.
The Wicked Flea
It depends on how the first record will be processed differently from the rest. He can always adjust it to his needs. He could also use a `do-while` loop instead. It’s just the separation of first and rest that he was interested in.
Gumbo
A: 

Do not have any method for know exactly position in all moment?

for example java iterator implements next or hasNext(), if !hasNext() the item is the last.

Please edit your question and don’t use the answer function. You can implement that in object oriented way with an iterator if you want to.
Gumbo
+1  A: 
$first = true;
while ($row=mysql_fetch_assoc()) {
   if ($first) echo "First"
   $first = false;
}
solomongaby
To alternate rows with this method, just do $first = !$first.
The Wicked Flea
this is the same
True David, true. But it's the only DRY way to do it, there's no other way. DB objects with iterators are either rare or part of a framework.
The Wicked Flea
@The Wicked Flea: Again, you’re wrong. `$first` will just be true for the first iteration. After that, `$first` is always false.
Gumbo
So replace it with the integer. There's no "better" method of tracking the first row that doesn't duplicate a whole block of code.
The Wicked Flea
A: 

well, if i understand your question correctly, you want the first row and then iterate through the others:

$row = mysql_fetch_assoc(); // $row contains first row

while ($row = mysql_fetch_assoc()) { // loop through the rest of the rows

}
Ozzy