webguync Posted June 25, 2009 Share Posted June 25, 2009 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 More sharing options...
WolfRage Posted June 25, 2009 Share Posted June 25, 2009 Ok so what is it doing right now? What does the outputted HTML source code look like? Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863603 Share on other sites More sharing options...
phant0m Posted June 25, 2009 Share Posted June 25, 2009 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; } Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863605 Share on other sites More sharing options...
webguync Posted June 25, 2009 Author Share Posted June 25, 2009 I changed the code to a class, but it doesn't seem to be picking it up. When I view source I don't see tr class="hilite" class="nohilite" which is the name I have given my classes in the CSS. Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863681 Share on other sites More sharing options...
phant0m Posted June 25, 2009 Share Posted June 25, 2009 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? Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863695 Share on other sites More sharing options...
flyhoney Posted June 25, 2009 Share Posted June 25, 2009 <?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); ?> Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863712 Share on other sites More sharing options...
webguync Posted June 25, 2009 Author Share Posted June 25, 2009 cool, that did the trick. Thanks! Link to comment https://forums.phpfreaks.com/topic/163677-code-to-stripe-rwos-not-working/#findComment-863739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.