Re: Check variable value if change inside the Loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/19/2011 12:32 PM, ®0L¥ wrote:
> Hello World,
> 
> I have a question regarding loops, How I can check inside a loop if a
> variable chage a value, for example
> 
> $sql = mysql_query("SELECT model FROM table")
> 
> while($row=mysql_fetch_array){
>     $model = $row['model'];
>     echo "First model: ".$model."<br />";
>    * // if $model chage its model value*
> *   // echo "Second Model: ".$model."<br />"*
> }
> 
> Thank you world !!!!!
> 
> 

You have two ways of solving this issue:

1) Do it in Mysql with a GROUP BY option

$results = mysql_query("SELECT model FROM table GROUP BY model")

2) Within PHP, do something like this.

$results = mysql_query("SELECT model FROM table")

# This assumes none of the values stored in DB column model are type=NULL
$model = null;
while( $row = mysql_fetch_array($results) ) {
  if ( is_null($model) || $model !== $row['model'] ) {
    $model = $row['model'];
    echo $model . '<br />';
  }
  ...
}

Either one should give you the same results.

-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

C - (541) 408-5189
O - (541) 323-9113
H - (541) 323-4219

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux