On Friday 22 Aug 2003 10:26 pm, John Taylor-Johnston wrote: > This is my favourite question. Can I clean up my 'if then' statements a > tad? Must be a cleaner way? > Still learning, still having fun :) > Thanks, > John > > $news = mysql_query($sql) or die(print > "document.write(\"".mysql_error()."\");"); > > $found = 0; > while ($mydata = mysql_fetch_object($news)) > { > if($getaddr == $mydata->IPAddress) > { > $found = 1; > } > } > > if ($found > 0) > { > echo "document.write(\"$getaddr already visited. \");"; > }else{ > echo "document.write(\"insert $getaddr into $table. \");"; > }
Not sure at exactly what level you're looking to improve it! Could you improve the query so you didn't have to search through all the records? i. e. add in '..where IPAddress = $getaddr" That way you could do away with the while loop altogether. If you can't then why not break out of the while as soon as you've found the IPaddress you're looking for - i.e. if ( $found = $getaddr == $mydata->IPAddress ) break; And finally, if you were really having a downer on if statements you could use the ternary operator form as in: document.write( ( $found ) ? "...already vistied" : "insert..." ); Whatever you use, I think Robert's layout not only shows elegance and panache but also makes it much easier to read. HTH Nick PS: IP addresses are a notoriously bad way of checking to see if someone's visited your site. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php