array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

array

Post by Anant »

I am using the while loop to step through array -

Code: Select all

while ($row = mysql_fetch_array($result)) { }
Now i want to put some conditions in only first row of the array in particular column. What would be the best way to do that ?

I know you can access column using

Code: Select all

$row['name']; 
but i want to place condition in only first row.

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array

Post by AbraCadaver »

I'm not sure I understand, but something like this?

Code: Select all

while($row = mysql_fetch_array($result)) {
   $rows[] = $row;
}

$rows[0]['name'] = 'something';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: array

Post by McInfo »

or

Code: Select all

if ($row = mysql_fetch_array($result)) {
    /* Process first row here */

    while ($row = mysql_fetch_array($result)) {
        /* Process additional rows here */

    }
}
or

Code: Select all

$i = 0;
while ($row = mysql_fetch_array($result)) {
    if ($i == 0) {
        /* Do something different for first row here */
    }
    $i++;
}
or

Code: Select all

$isFirstRow = true;
while ($row = mysql_fetch_array($result)) {
    if ($isFirstRow) {
        /* Do something different for first row here */
        $isFirstRow = false;
    }
}
Post Reply