Re: [PHP] Check variable value if change inside the Loop

2011-10-19 Thread Jim Lucas
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."";
>* // if $model chage its model value*
> *   // echo "Second Model: ".$model.""*
> }
> 
> 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 . '';
  }
  ...
}

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



[PHP] Check variable value if change inside the Loop

2011-10-19 Thread ®0L¥
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."";
   * // if $model chage its model value*
*   // echo "Second Model: ".$model.""*
}

Thank you world !


-- 
salu2 ®0L¥