Jump to content

code to stripe rwos not working


webguync

Recommended Posts

I have some PHP code which is supposed to create a background color on alternate rows making the table more readable. The code as I have it now isn't working. I am pretty sure I am using the code correctly, so I may just have it in the wrong place.

 

The full code is:

 

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost','','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tablename", $con);
$result = mysql_query("SELECT rpc0609.employee_id,rpc0609.employee_name, roster0609.title, roster0609.last_name,roster0609.first_name,roster0609.employee_id,rpc0609.assessor_id,rpc0609.assessor_name,rpc0609.score1,rpc0609.score2,rpc0609.score3,rpc0609.score4,rpc0609.score5,rpc0609.score6,rpc0609.score7,rpc0609.date_created,rpc0609.date_uploaded FROM rpc0609 LEFT JOIN  roster0609 USING (employee_id) WHERE rpc0609.assessor_id = '".$q."' ORDER BY rpc0609.employee_id, rpc0609.date_uploaded ASC") or die(mysql_error());
echo "<table>
<tr>

<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Title</th>
<th>score 1</th>
<th>score 2</th>
<th>score 3</th>
<th>score 4</th>
<th>score 5</th>
<th>score 6</th>
<th>score 7</th>
<th>Assessor Name</th>
<th>Assessor ID</th>
<th>Call Number (1-4)</th>
<th>Date Created</th>
<th>Date Uploaded</th>
</tr>";
$count = 0;
$CallNumber = 1;	

while($row = mysql_fetch_array($result))
  {

$currentID = $row['employee_id'];

    if($count == 0 || $currentID != $previousID) {
        $CallNumber = 1; }
    else {
        $CallNumber++;
	}
$i = 0;
while($db_fetch = mysql_fetch_array($db_query))
{
echo "
";
if($i%2 == 0)
{
echo "<tr bgcolor='#F4F6FA'>";
$i++;
}
else
{
echo "<tr bgcolor='#E9EDF5'>";
$i++;
}
}

  echo "<td>" . $row['employee_id'] . "</td>";
  echo "<td>" . ucwords($row['employee_name']) . "</td>";

  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['score1'] . "</td>";
  echo "<td>" . $row['score2'] . "</td>";
  echo "<td>" . $row['score3'] . "</td>";
  echo "<td>" . $row['score4'] . "</td>";
  echo "<td>" . $row['score5'] . "</td>";
  echo "<td>" . $row['score6'] . "</td>";
  echo "<td>" . $row['score7'] . "</td>";
  echo "<td>" . $row['assessor_name'] . "</td>";
  echo "<td>" . $row['assessor_id'] . "</td>";
  echo "<td>" . $CallNumber . "</td>";
  echo "<td>" . $row['date_created'] . "</td>";
  echo "<td>" . $row['date_uploaded'] . "</td>";
  echo "</tr>";

$previousID = $currentID;
    
    $count++;

  }
echo "</table>";
mysql_close($con);

?>

 

the code specifically for the striping is:

 

if($i%2 == 0)
{
echo "<tr bgcolor='#F4F6FA'>";
$i++;
}
else
{
echo "<tr bgcolor='#E9EDF5'>";
$i++;
}

 

Link to comment
https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/
Share on other sites

bgcolor? that's outdated

I advise you to use CSS, also you cannot apply any background color to a tr, but rather assign a class to a tr, like

<tr class="uneven">

 

in CSS, use the following:

 

/*even rows*/
td{
  background-color: #color;
}

.uneven td{
  background-color: #color; 
}

well, your logic is kinda flawed

 

 

this code, for instance, will never execute

<?php
while($db_fetch = mysql_fetch_array($db_query))
{
echo "
";
if($i%2 == 0)
{
echo "<tr bgcolor='#F4F6FA'>";
$i++;
}
else
{
echo "<tr bgcolor='#E9EDF5'>";
$i++;
}
}
?>

 

Why? because $db_query is not a valid mysql resource id

 

Also, why do you have two while loops?

<?php

$q = $_GET["q"];

$con = mysql_connect('localhost','','');

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("tablename", $con);

$query = "
SELECT rpc0609.employee_id, rpc0609.employee_name, roster0609.title, roster0609.last_name, roster0609.first_name, roster0609.employee_id, rpc0609.assessor_id, rpc0609.assessor_name, rpc0609.score1, rpc0609.score2, rpc0609.score3, rpc0609.score4, rpc0609.score5, rpc0609.score6, rpc0609.score7, rpc0609.date_created, rpc0609.date_uploaded
FROM rpc0609 
LEFT JOIN roster0609 
USING (employee_id) 
WHERE rpc0609.assessor_id = '".$q."' 
ORDER BY rpc0609.employee_id, rpc0609.date_uploaded ASC
";

$result = mysql_query($query) or die(mysql_error());

?>
<table>
<tr>
	<th>Employee ID</th>
	<th>Employee Name</th>
	<th>Employee Title</th>
	<th>score 1</th>
	<th>score 2</th>
	<th>score 3</th>
	<th>score 4</th>
	<th>score 5</th>
	<th>score 6</th>
	<th>score 7</th>
	<th>Assessor Name</th>
	<th>Assessor ID</th>
	<th>Call Number (1-4)</th>
	<th>Date Created</th>
	<th>Date Uploaded</th>
</tr>
<?php
$count = 0;
$CallNumber = 1;

while ($row = mysql_fetch_array($result))
{
$currentID = $row['employee_id'];
$CallNumber = ($count == 0 || $currentID != $previousID) ? 1 : $CallNumber + 1;

echo '<tr class="'.(($count % 2 == 0) ? 'even' : 'odd').'">';
  echo "<td>" . $row['employee_id'] . "</td>";
  echo "<td>" . ucwords($row['employee_name']) . "</td>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['score1'] . "</td>";
  echo "<td>" . $row['score2'] . "</td>";
  echo "<td>" . $row['score3'] . "</td>";
  echo "<td>" . $row['score4'] . "</td>";
  echo "<td>" . $row['score5'] . "</td>";
  echo "<td>" . $row['score6'] . "</td>";
  echo "<td>" . $row['score7'] . "</td>";
  echo "<td>" . $row['assessor_name'] . "</td>";
  echo "<td>" . $row['assessor_id'] . "</td>";
  echo "<td>" . $CallNumber . "</td>";
  echo "<td>" . $row['date_created'] . "</td>";
  echo "<td>" . $row['date_uploaded'] . "</td>";
  echo "</tr>";

$previousID = $currentID;
$count++;
}

echo "</table>";

mysql_close($con);

?>

Archived

This topic is now archived and is closed to further replies.

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