suttercain Posted February 23, 2007 Share Posted February 23, 2007 Hi everyone, I am attempting to get alternating row colors using the PHPFreaks tutorial: "Alternating Row Colors With MySQL Results" http://www.phpfreaks.com/tutorials/5/0.php When I run it I get a parse error: Parse error: parse error, unexpected T_LNUMBER, expecting ',' or ';' in C:\wamp\www\ARB\pio\vlist_2007.php on line 14 Here is the code, altered slightly from the tutorial for my needs: <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Begin your table outside of the array echo "<table width="100%" border="0" cellpadding="4" cellspacing="0"> <tr> <td width="110"><b>DIVISION</b></td> </tr>"; // Define your colors for the alternating rows $color1 = "#CCFFCC"; $color2 = "#BFD8BC"; $row_count = 0; // Perform an statndard SQL query: $sql_events = mysql_query("SELECT DIV FROM vlist_1997 ORDER BY DIV ASC") or die (mysql_error()); // We are going to use the "$row" method for this query. This is just my preference. while ($row = mysql_fetch_array($sql_events)) { $div = $row["div"]; /* Now we do this small line which is basically going to tell PHP to alternate the colors between the two colors we defined above. */ $row_color = ($row_count % 2) ? $color1 : $color2; // Echo your table row and table data that you want to be looped over and over here. echo "<tr> <td width="110" bgcolor="$row_color" nowrap> $div</td> </tr>"; // Add 1 to the row count $row_count++; } // Close out your table. echo "</table>"; ?> I also noticed that when you get to the third page of the tutorial the table is displayed but no alternating colors... Any help would be appreciated. Thanks. Shannon Quote Link to comment Share on other sites More sharing options...
EagerWolf Posted February 23, 2007 Share Posted February 23, 2007 I've solved this with CSS: $css = array('css_style_1', 'css_style_2'); $i = 1; foreach($myarray as $key => $value) { $vrstica = (1-pow(-1,$i))/2; print "YOUR DATA HERE"; $i++; } I had data in array ... but it is the same if you get data from MySQL. Hope this'll help U.... Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Alright I changed the double quotes within the echos statements which took care of the parsing error I listed above. Now I am running into error when I run the code: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DIV FROM vlist_1997 ORDER BY DIV ASC' at line 1 DIVISION Don't know what is going on... Quote Link to comment Share on other sites More sharing options...
EagerWolf Posted February 23, 2007 Share Posted February 23, 2007 I forgot ... To your row ... or whatever it is ... add: <td class = "$css[$vrstica]"> ... This will call CSS style from array! Don't forget to include css file in index... Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 Wow... that all seems way too complex. Try something more simple: CSS (change the actual colors to what you want): .even { background-color: #f2f3f4; } .odd { background-color: #ffffff; } PHP: <?php // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_array($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; // Print row cells here with data echo "</tr>\n"; } ?> That's all there is to it. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Okay so I converted some code to css and added it as an include. Here is the code I am now working with: <?php //Connect to the Database via the Include File! require ('get_connected.inc'); include ('rows.css'); // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='4' cellspacing='0'> <tr> <td width='110'><b>DIVISION</b></td> </tr>"; // Perform an statndard SQL query: $sql_events = mysql_query("SELECT DIV FROM vlist_1997 ORDER BY DIV ASC") or die (mysql_error()); // We are going to use the "$row" method for this query. This is just my preference. while ($row = mysql_fetch_array($sql_events)) { $res = $row["div"]; // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_array($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; // Print row cells here with data echo "</tr>\n"; } ?> When I ran the script I got the following error: http://localhost/ARB/pio/vlist_2007.php I checked and the syntax looks alright. Where am I going wrong? Thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 CSS sheets can't be simply included that way. You actually have to include them as link tags in your header: <head> <title>My Page</title> <link type="text/css" rel="stylesheet" href="mystyle.css" /> </head> Obviously, there is other info (ie, Doctype) that I'm leaving out for the sake of simplicity, but you get the idea. Besides that, we can't see the error since it's a "localhost" link. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 My bad. I didn't realize it made a link, I thought I copied the text. This is the error: Parse error: parse error, unexpected $end in C:\wamp\www\ARB\pio\vlist_2007.php on line 39 I have now have the following code: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Page</title> <link type="text/css" rel="stylesheet" href="rows.css" /> </head> <body> <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='4' cellspacing='0'> <tr> <td width='110'><b>DIVISION</b></td> </tr>"; // Perform an statndard SQL query: $sql_events = mysql_query("SELECT DIV FROM vlist_1997 ORDER BY 'DIV' ASC") or die (mysql_error()); // We are going to use the "$row" method for this query. This is just my preference. while ($row = mysql_fetch_array($sql_events)) { $res = $row["div"]; // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_array($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; // Print row cells here with data echo "</tr>\n"; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 23, 2007 Share Posted February 23, 2007 You have two while statement, where you should only have one: <?php // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $res = $row["div"]; $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$res</td>\n"; echo "</tr>\n"; } Ken Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Thanks, I removed the first while statement and now I am getting the error: Connected! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DIV FROM vlist_1997 ORDER BY 'DIV' ASC' at line 1 DIVISION That was the same error I had earlier. I am going crazy Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 Don't put DIV in quotes... Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 The error is with your actual SQL statement. You don't need your quotes around the column name in your ORDER BY clause. *EDIT* - beat me to it Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Hi Jesi, I took the quotes off the DIV, this is the code I am running: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Headache</title> <link type="text/css" rel="stylesheet" href="rows.css" /> </head> <body> <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='4' cellspacing='0'> <tr> <td width='110'><b>DIVISION</b></td> </tr>"; // Perform an statndard SQL query: $sql_events = mysql_query("SELECT DIV FROM vlist_1997 ORDER BY DIV ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $res = $row["div"]; $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$res</td>\n"; echo "</tr>\n"; } ?> </body> </html> Unfortunately I am still getting the following error: Connected! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DIV FROM vlist_1997 ORDER BY DIV ASC' at line 1 DIVISION Sniffle... Sniffle... :'( Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 Not positive, but is "DIV" reserved in MySQL? Just for fun, put tics around your column names (not quotes): SELECT `DIV` FROM vlist_1997 ORDER BY `DIV` ASC Just did some checking, and "DIV" is used as an alternate to the arithmetic division operator, so you must have it with the tics around it. Hope that fixes it for you. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Worth a shot... I changed it to: <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='4' cellspacing='0'> <tr> <td width='110'><b>DIVISION</b></td> </tr>"; // Perform an statndard SQL query: $sql_events = mysql_query("SELECT `DIV` FROM vlist_1997 ORDER BY `DIV` ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $res = $row["div"]; $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$res</td>\n"; echo "</tr>\n"; } ?> And got the following error: Connected!Connected to database! Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\ARB\pio\vlist_1997.php on line 24 DIVISION Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 That's because, in my code, I'm assuming that your results are housed in a variable called "$res," not in "$sql_events" as you have it. You either need to change your query variable to "$res" or your mysql_fetch_assoc() variable to "$sql_events" Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 I made the change: $res = mysql_query("SELECT `DIV` FROM vlist_1997 ORDER BY `DIV` ASC") or die (mysql_error()); But I still am getting the same error. Connected! Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\ARB\pio\vlist_1997.php on line 24 DIVISION Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 You are overwriting your $res variable inside your loop. Change it to this: <?php $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[DIV]</td>\n"; echo "</tr>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 BINGO!!! Thank you so much! That got it going! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.