What is the problem with this do while loop?

Mark Steggles
Mark Steggles used Ask the Experts™
on
Page is to display top score by player (this piece works fine) and
highlight where they have gone better or worse than par (green good,
red bad etc)

So I set variable up to select top scores then echo for results
($row_Score) to be displayed - works
Introduce another variable $colourx which checks value from result
($row_Score) and if == 2 (once working I will put another variable for
par) display colour green

So in example attached if value is 2 then colour should be green else
it will be black. This working fine for first row returned but any
subsequent rows just use colour from first row. (attached screenshot
example)

template.php
screengrab.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Here is the template.php script in the code snippet.  It's a lot of code and I cannot follow it all, but there may be a germ of an idea in this..

See line 63-64

$Scores = mysql_query($query_Scores, $Golf) or die(mysql_error());
$row_Scores = mysql_fetch_assoc($Scores);

The mysql_fetch_assoc($Scores) function call removes the first row from the results set.

Then on line 154 we have this:

  do { ?>

And on line 171 we have this:

    <?php } while ($row_Scores = mysql_fetch_assoc($Scores)); ?>

So it appears that your script is handling the first row of the results set very differently from the way it handles the other rows of the results set.  Not sure why you would want to do it this way, but it seems to be one of the things that happens to programmers who try to use Dreamweaver as the code base when they want to write a script using PHP.

A better general design is to try to get your code separated into logic layers and presentation layers.  You would do all your queries and build all your data strings in the logic part of the script, then you would write the data out to the browser in the presentation part of the script.  To the extent that your logic and presentation are intermixed (sorry, Dreamweaver) you will introduce some very difficult-to-debug spaghetti code.

Best of luck with it, ~Ray
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>f0re.co.uk</title> 
<link rel="stylesheet" type="text/css" href="web.css" /> 

</head>
<?php
session_start();


if(!isset($_SESSION['loggedin'])) {    
// If the session 'loggedin' is NOT set forward the user back to the login form with the error set
   header('Location: http://localhost/index.php?error=1');
   exit();        // Otherwise show the rest of the page (admin section)
}

?>
<?php require_once('Connections/Golf.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database, $Golf);
$query_Scores = "select player.firstname, course.id, course.course, score.date,score.h1, score.h2, score.h3, score.h4, score.h5, score.h6, score.h7, score.h8,score.h9, course.p1, score.total, course.par
		from player player, courses course, score score
				join (select id, cid, pid, min(s2.total) as min_total from score s2 group by cid) as s2 on score.cid=s2.cid and score.total=s2.min_total
					where
						score.pid=player.id
						and 
						score.cid=course.id;";
										
$Scores = mysql_query($query_Scores, $Golf) or die(mysql_error());
$row_Scores = mysql_fetch_assoc($Scores);
$totalRows_Scores = mysql_num_rows($Scores);


?> 
<body class="content">
<div id="container">  
<div id="sidebar1"> 
	<p><a href= "template.php">Home</a></p> 
	<p><a href= "viewcourses.php">View courses</a></p> 
	<p><a href= "Viewscore.php">View scores</a></p> 
   	<p><a href= "Newcourse.php">New course</a></p>
    <p><a href= "Addscore.php">Add score</a></p> 
	<p><a href= "Newplayer.php">New Player</a></p> 
    <p><a href= "hof.php">Hall of fame</a></p> 
	
    <p>&nbsp;</p> 
  <!-- end #sidebar1 --></div> 

    
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
                <blockquote>
                  <p>Top Scores</strong></p>
                  <p>&nbsp;</p>
                </blockquote>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>


<div id = "mainContent">

<table id= "table1" summary = "Scores" class = "table1">
  <tr>
    <td><span>Date</span></td>
    <td><span>Player</span></td>
    <td><span>Course</span></td>
    <td><span>Hole 1</span></td>
    <td><span>Hole 2</span></td>
    <td><span>Hole 3</span></td>
    <td><span>Hole 4</span></td>
    <td><span>Hole 5</span></td>
    <td><span>Hole 6</span></td>
    <td><span>Hole 7</span></td>
    <td><span>Hole 8</span></td>
    <td><span>Hole 9</span></td>
    <td><span>Best score</span></td>
    <td><span>Par</span></td>
  </tr>
  
  <?php 
  
  $colour = "#000000";
    $colour2 = "#000000";
	  $colour3 = "#000000";
	    $colour4 = "#000000";
		  $colour5 = "#000000";
		    $colour6 = "#000000";
			  $colour7 = "#000000";
			    $colour8 = "#000000";
				  $colour9 = "#000000";

if ($row_Scores['h1'] == 2)
		 $colour= '#00FF00';

		 if ($row_Scores['h2'] == 2)
		 $colour2= '#00FF00';
		 if ($row_Scores['h3'] == 2)
		 $colour3= '#00FF00';
		 if ($row_Scores['h4'] == 2)
		 $colour4= '#00FF00';
		 if ($row_Scores['h5'] == 2)
		 $colour5= '#00FF00';
		 if ($row_Scores['h6'] == 2)
		 $colour6= '#00FF00';
		 if ($row_Scores['h7'] == 2)
		 $colour7= '#00FF00';
		 if ($row_Scores['h8'] == 2)
		 $colour8= '#00FF00';
		 if ($row_Scores['h9'] == 2)
		 $colour9= '#00FF00';
		 
		 else if ($colour = 'black');

  do { ?>
  	<tr>	
	<td><span><?php echo $row_Scores['date']; ?></span></td>
	<td><span><?php echo $row_Scores['firstname']; ?></span></td>
	<td><span><?php echo $row_Scores['course']; ?></span></td>
	<td><span><?php echo "<font color='$colour'> ".$row_Scores['h1']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour2'> ".$row_Scores['h2']." </font>"; ?></span></td>
	<td><span><?php echo "<font color='$colour3'> ".$row_Scores['h3']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour4'> ".$row_Scores['h4']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour5'> ".$row_Scores['h5']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour6'> ".$row_Scores['h6']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour7'> ".$row_Scores['h7']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour8'> ".$row_Scores['h8']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour9'> ".$row_Scores['h9']." </font>"; ?></span></td>	   
	<td><span><?php echo $row_Scores['total']; ?></span></td>
	<td><span><?php echo $row_Scores['par']; ?></span></td>
    </tr>
    <?php } while ($row_Scores = mysql_fetch_assoc($Scores)); ?>
</table>
<div id = "botfoot">
<P>MJ</P>
<!-- end #mainContent --></div> 
    </div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> 
	<!-- end #container --></div> 
	
</body>
</html>
<?php
mysql_free_result($Scores);
?>

Open in new window

Expert of the Year 2008
Top Expert 2008
Commented:
the first php block ( the one that contains session_start(); etc) MUST appear at the very start of the page or the sessions will not work nor the redirection (when needed).

Move the variables within the do{...} construct.

The "else if($colour='black');" doesn't make sense to me.  You probably meant to check for the 'h1' like you were doing for the others.  If not, I suspect you meant
else if( $colour=='black' );

OR perhaps:
else $colour='black';//This seems to be more likely than the above.
<?php
session_start();
if(!isset($_SESSION['loggedin'])) {    
// If the session 'loggedin' is NOT set forward the user back to the login form with the error set
   header('Location: http://localhost/index.php?error=1');
   exit();        // Otherwise show the rest of the page (admin section)
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>f0re.co.uk</title> 
<link rel="stylesheet" type="text/css" href="web.css" /> 
</head>
<?php 
require_once('Connections/Golf.php'); 

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database, $Golf);
$query_Scores = "select player.firstname, course.id, course.course, score.date,score.h1, score.h2, score.h3, score.h4, score.h5, score.h6, score.h7, score.h8,score.h9, course.p1, score.total, course.par
		from player player, courses course, score score
				join (select id, cid, pid, min(s2.total) as min_total from score s2 group by cid) as s2 on score.cid=s2.cid and score.total=s2.min_total
					where
						score.pid=player.id
						and 
						score.cid=course.id;";
										
$Scores = mysql_query($query_Scores, $Golf) or die(mysql_error());
$row_Scores = mysql_fetch_assoc($Scores);
$totalRows_Scores = mysql_num_rows($Scores);


?> 
<body class="content">
<div id="container">  
<div id="sidebar1"> 
	<p><a href= "template.php">Home</a></p> 
	<p><a href= "viewcourses.php">View courses</a></p> 
	<p><a href= "Viewscore.php">View scores</a></p> 
   	<p><a href= "Newcourse.php">New course</a></p>
    <p><a href= "Addscore.php">Add score</a></p> 
	<p><a href= "Newplayer.php">New Player</a></p> 
    <p><a href= "hof.php">Hall of fame</a></p> 
	
    <p>&nbsp;</p> 
  <!-- end #sidebar1 --></div> 

    
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
                <blockquote>
                  <p>Top Scores</strong></p>
                  <p>&nbsp;</p>
                </blockquote>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>


<div id = "mainContent">

<table id= "table1" summary = "Scores" class = "table1">
  <tr>
    <td><span>Date</span></td>
    <td><span>Player</span></td>
    <td><span>Course</span></td>
    <td><span>Hole 1</span></td>
    <td><span>Hole 2</span></td>
    <td><span>Hole 3</span></td>
    <td><span>Hole 4</span></td>
    <td><span>Hole 5</span></td>
    <td><span>Hole 6</span></td>
    <td><span>Hole 7</span></td>
    <td><span>Hole 8</span></td>
    <td><span>Hole 9</span></td>
    <td><span>Best score</span></td>
    <td><span>Par</span></td>
  </tr>
  
  <?php 
  do { 
  
  $colour = "#000000";
    $colour2 = "#000000";
	  $colour3 = "#000000";
	    $colour4 = "#000000";
		  $colour5 = "#000000";
		    $colour6 = "#000000";
			  $colour7 = "#000000";
			    $colour8 = "#000000";
				  $colour9 = "#000000";

if ($row_Scores['h1'] == 2)
		 $colour= '#00FF00';

		 if ($row_Scores['h2'] == 2)
			 $colour2= '#00FF00';
		 if ($row_Scores['h3'] == 2)
			 $colour3= '#00FF00';
		 if ($row_Scores['h4'] == 2)
			 $colour4= '#00FF00';
		 if ($row_Scores['h5'] == 2)
			 $colour5= '#00FF00';
		 if ($row_Scores['h6'] == 2)
			 $colour6= '#00FF00';
		 if ($row_Scores['h7'] == 2)
			 $colour7= '#00FF00';
		 if ($row_Scores['h8'] == 2)
			 $colour8= '#00FF00';
		 if ($row_Scores['h9'] == 2)
			 $colour9= '#00FF00';		 
		 //else if ($colour = 'black');

		 //you probable meant this intead of "else if" above
		 if ($row_Scores['h1'] == 2)
			 $colour= '#00FF00';		 
  
  ?>
  	<tr>	
	<td><span><?php echo $row_Scores['date']; ?></span></td>
	<td><span><?php echo $row_Scores['firstname']; ?></span></td>
	<td><span><?php echo $row_Scores['course']; ?></span></td>
	<td><span><?php echo "<font color='$colour'> ".$row_Scores['h1']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour2'> ".$row_Scores['h2']." </font>"; ?></span></td>
	<td><span><?php echo "<font color='$colour3'> ".$row_Scores['h3']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour4'> ".$row_Scores['h4']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour5'> ".$row_Scores['h5']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour6'> ".$row_Scores['h6']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour7'> ".$row_Scores['h7']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour8'> ".$row_Scores['h8']." </font>"; ?></span></td>	  
	<td><span><?php echo "<font color='$colour9'> ".$row_Scores['h9']." </font>"; ?></span></td>	   
	<td><span><?php echo $row_Scores['total']; ?></span></td>
	<td><span><?php echo $row_Scores['par']; ?></span></td>
    </tr>
    <?php } while ($row_Scores = mysql_fetch_assoc($Scores)); ?>
</table>
<div id = "botfoot">
<P>MJ</P>
<!-- end #mainContent --></div> 
    </div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> 
	<!-- end #container --></div> 
	
</body>
</html>
<?php
mysql_free_result($Scores);
?>

Open in new window

Mark StegglesWeb Developer
Top Expert 2006

Author

Commented:
Thanks Hielo that fixed it.

It was actually my friend's code but I posted it for him as I don't know much about php

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial