Jump to content

[SOLVED] Alternating Row Colors With MySQL Results


suttercain

Recommended Posts

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

Link to comment
Share on other sites

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....

 

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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... :'(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.