Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a list in php/mysql

Status
Not open for further replies.

tranquillo

Technical User
Jan 6, 2003
22
SE
Hi... I need some help with a list that I wanna do in php.

I want two lists of events with dates that are devided in coming and previous events. so I'd need a php-tagg that lists all events in a table where date<now or date>now. and I'd like the background to be different on every second row... gray-white-gray-white. and so on...

I'm kind of new at php. and I wanna know what's wrong with this code...

<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);
mysql_select_db(&quot;seko&quot;,$db);

$sql = &quot;SELECT * FROM arbetsgivare&quot;;

$result = mysql_query($sql);

while ( $row = mysql_fetch_array ($result)){
echo ($var%2) ? &quot;#999999&quot; : &quot;#FFFFFF&quot;;
echo $row[0];
}

?>
 
ok... so now I know whats wrong...

how do I increment $var

and what's error-handling... the access code works fine on a lot of other things I've done...

thanks.
 
You need to explicitly set $var to zero. I know that PHP will instantiate the variable with that value, but it's good programming practice in terms of readability. Do this before your while loop.

Then inside the while loop, after you have performed the modulo check against it, add one to it. If the value doesn't change between a value that is evenly divisible by two and one that isn't, the background is never going to cycle. You can either perform &quot;$var = $var + 1;&quot; or &quot;$var++;&quot;.



I strongly recommend that you check the return of functions for error conditions. Not just MySQL functions, but all functions. That way, your scripts can react gracefully to problems.

Even if you don't need it now, sooner or later you will write a script that user input can break. Without graceful reaction to errors, you code just fails. With it, you have a chance of either recovering from the error or providing better diagnostic information for later troubleshooting. I strongly recommend that you get into this programming habit now.

In fact, when I'm programming I write the error-handling code first. That way, I know I have caught a majority of them before I clutter up my screen with the working code. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top