Jump to content

alternating row colours


joebudden

Recommended Posts

Hi guys I've followed the phpfreaks tutorial to do this but cant seem to get it working...

Here is my code

[code]
<?php
$sql = "select name,surname,ability,fitness,teamid
from player,$tempgame
where player.id = $tempgame.id and gameid = '$gameid' and teamid = '$teamid'";

$res = mysql_query($sql) or die (mysql_error());

$id = "select id from $tempgame where teamid = '$teamid'";

$res2 = mysql_query($id) or die (mysql_error());

$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;
?>
<table border="1" width="200px">
<?php
while(($x = mysql_fetch_assoc($res2))&&($r = mysql_fetch_assoc($res)))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo '<tr bgcolor="$row_color"><td>'
?>
<a href="javascript:openWindow('information.php?id=<?php echo $x['id'];?>', 500, 400, 200, 200)">
<?php
echo $r['name'].' <b>'.$r['surname'].'</b>';
?>
</a>
<?php
$row_count++;
}
?>
[/code]

Any ideas ??? , thanks in advance
Link to comment
Share on other sites

can anyone help me with this ??? its so annoying because it should be soooo simple

here is my entire page.....

[code]
<?php
//start the games session
session_start();

//connect to database
include("connect.php");

//var containing game id
$gameid = $_SESSION["gameid"];
$teamname = $_SESSION['teamname'];
$teamid = $_SESSION['teamid'];
$tempgame = 'GAME'.$gameid;

//EXIT button
if (isset($_POST['exit']))
{
$exit = "delete from game where gameid = '$gameid'";
mysql_query($exit) or die (mysql_error());
}

?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Football Manager</title>
<link rel=stylesheet href="style.css" type="text/css">
<script type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
<script language="javascript">
<!--
function openWindow(url,width,height,left,top) {
  window.open(url, "", "width="+width+",height="+height+",left="+left+",top="+top);
}
//-->
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr><td><div class="title"></div></td></tr>
<tr><td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="continue" value="Continue" onclick="MM_goToURL('parent','draw.php');return document.MM_returnValue"/>
<input type="submit" name="exit" value="Exit" onclick="MM_goToURL('parent','start.php');return document.MM_returnValue"/>
<?php
if(isset($_POST["exit"]))
{
$delsql5 = "drop table $tempgame where gameid = '$gameid'";
mysql_query($delsql5) or die (mysql_error());
//$delsql4 = "delete from $tempgame where gameid = '$gameid'";
//mysql_query($delsql4) or die (mysql_error());
$delsql2 = "delete from team where gameid = '$gameid'";
mysql_query($delsql2) or die (mysql_error());
$delsql3 = "delete from game where gameid = '$gameid'";
mysql_query($delsql3) or die (mysql_error());
}
?>
<br />
</td></tr>
</table>
</body>
</html>
<?php

$color1 = "red";
$color2 = "blue";
$r_count = 0;

$sql = "select name,surname,ability,fitness,teamid
from player,$tempgame
where player.id = $tempgame.id and gameid = '$gameid' and teamid = '$teamid'";

$res = mysql_query($sql) or die (mysql_error());

$id = "select id from $tempgame where teamid = '$teamid'";

$res2 = mysql_query($id) or die (mysql_error());

?>
<table border="1" width="200px">
<?php
while(($x = mysql_fetch_assoc($res2))&&($r = mysql_fetch_assoc($res)))
{
$row_color = ($r_count % 2) ? $color1 : $color2;
echo '<tr bgcolor="$row_color"><td>'?>
<a href="javascript:openWindow('information.php?id=<?php echo $x['id'];?>', 500, 400, 200, 200)">
<?php
echo $r['name'].' <b>'.$r['surname'].'</b>';
?>
</a>
<?php
$r_count++;
}
echo '</table>';
?>
</form>
[/code]
Link to comment
Share on other sites

this is a pretty easy thing to do... you can also search the forums for 'zebra'
[code]
<?php
$i = 0;
echo"<table>\n";
while($row = mysql_fetch_array($query)){
if($i % 2 == 0){
$alt_color = "FFFFFF";
}else{
$alt_color = "DDDDDD";
}
echo "<tr bgcolor=\"". $alt_color ."\"><td>cell1</td><td>cell2</td></tr>\n";
                $i++;
}
echo "</table>\n";
?>
[/code]


i [i]think[/i] your problem is with your strings... try
[code]echo "<tr bgcolor=\"". $rowcolor ."\">;[/code]

and check your source code as well to see what's being printed.
Link to comment
Share on other sites

[quote author=ProjectFear link=topic=124595.msg516593#msg516593 date=1170113999]
your not actually closing the <tr> and <td> tags anywhere in the loop. should you be?
try echoing out the row color to see if it is doing what it is supposed to be doing.
[/quote]

no he isnt, i think he just wants one of us to surgically inject it into his existing code for him.
Link to comment
Share on other sites

[quote author=ProjectFear link=topic=124595.msg516593#msg516593 date=1170113999]
your not actually closing the <tr> and <td> tags anywhere in the loop. should you be?
try echoing out the row color to see if it is doing what it is supposed to be doing.
[/quote]

Project fear is right, you're not closing those tags... so it may actually be rendering in some browsers, but it won't work properly until you do.
Link to comment
Share on other sites

well see how you open up your echo with ". if you were to use those again in the echo then php thinks your closing the line. So to stop that you can put a \ in front of the ". Or you could do it like this:

[code]echo "<tr bgcolor='{$row_color}'><td>";[/code]

but it all comes down to what you want to do. :)
Link to comment
Share on other sites

[quote author=ProjectFear link=topic=124595.msg516611#msg516611 date=1170114681]
well see how you open up your echo with ". if you were to use those again in the echo then php thinks your closing the line. So to stop that you can put a \ in front of the ". Or you could do it like this:

[code]echo "<tr bgcolor='{$row_color}'><td>";[/code]

but it all comes down to what you want to do. :)
[/quote]

that is spot on PF :)...

i tend to always use " " instead of using ' ' ..
Link to comment
Share on other sites

[quote author=joebudden link=topic=124595.msg516602#msg516602 date=1170114394]
cheers boo lolly!!!

i used this

[code]echo "<tr bgcolor=\"". $row_color ."\"><td>";[/code]

as opposed to this...

[code]echo '<tr bgcolor="$row_color"><td>';[/code]

and now its working..

Just for future reference what does the \ do ???
[/quote]

i'm mighty glad it worked for ya =). for further info check out the manual on [url=http://www.php.net/manual/en/language.types.string.php]strings[/url]
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.