Changing Row Colors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Changing Row Colors

Post by icesolid »

My program prints out a list of cases that are in a database. If a case was set to rush delivery then the row in the table is red. However this is not working for me. (Note: the row color is changed via CSS ex: greenBar or redBar)

Here is my code:

Code: Select all

<form method="post" action="manager.php?cmd=assign">
                <table align="right" width="520" border="0" cellpadding="0" cellspacing="1">
                  <tr>
                    <td align="center" height="30" class="topBar"><b>ASSIGN</b></td>
                    <td align="center" height="30" class="topBar"><b>INSPT</b></td>
                    <td align="center" height="30" class="topBar"><b>CONTROL #</b></td>
                    <td align="center" height="30" class="topBar"><b>DATE ORD</b></td>
                    <td align="center" height="30" class="topBar"><b>CUST</b></td>
                    <td align="center" height="30" class="topBar"><b>INSURED</b></td>
                    <td align="center" height="30" class="topBar"><b>LOCATION</b></td>
                    <td align="center" height="30" class="topBar"><b>RT</b></td>
                  </tr>
                  <?php
                  $result = mysql_query("SELECT * FROM cases WHERE location_county='" . $_POST["location_county"] . "' AND assigned='false' ORDER BY control_number ASC");

                  $ids = array();

                  while($row = mysql_fetch_assoc($result)) {
                      array_push($ids, $row["id"]);
                  }

                  foreach($ids as $id) {
                      $result = mysql_query("SELECT * FROM cases WHERE location_county='" . $_POST["location_county"] . "' AND assigned='false' ORDER BY control_number ASC");
                      $row = mysql_fetch_array($result);

                      if($row1["rush"] == true) {
                          $class = "redBar";
                      } else {
                          $class = "greenBar";
                      }
                  ?>
                  <tr>
                    <td align="center" height="30" class="<?php echo $class; ?>"><input type="checkbox" name="assign[<?php echo $id; ?>]" value="assigned" class="fields"></td>
                    <td align="center" class="<?php echo $class; ?>">
                    <select name="inspector[<?php echo $id; ?>]" class="fields">
                    <option value="">---</option>
                    <?php
                    $result1 = mysql_query("SELECT * FROM users WHERE account_type='Inspector' ORDER BY user_code ASC");

                    while($row1 = mysql_fetch_array($result1)) {
                    ?>
                        <option value="<?php echo $row1["user_code"]; ?>"><?php echo $row1["user_code"]; ?></option>
                    <?php
                    }
                    ?>
                    </select>
                    </td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo $row["control_number"]; ?></td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo $row["date_ordered"]; ?></td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo $row["user_code"]; ?></td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo substr($row["name_of_insured"], "0", "15"); ?></td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo substr($row["location_city"], "0", "10"); ?></td>
                    <td align="center" class="<?php echo $class; ?>"><?php echo substr($row["survey_type"], "0", "2"); ?></td>
                  </tr>
                  <?php
                  }
                  ?>
                  <tr>
                    <td align="right" height="35" colspan="8" class="topBar"><input type="submit" name="assign_cases" value="Assign Cases" class="buttons">&nbsp;&nbsp;</td>
                  </tr>
                </table>
                </form>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I didn't read through all of your code, but I did notice this:

Code: Select all

$result = mysql_query("SELECT * FROM cases WHERE location_county='" . $_POST["location_county"] . "' AND assigned='false' ORDER BY control_number ASC");
                      $row = mysql_fetch_array($result);

                      if($row1["rush"] == true) {
                          $class = "redBar";
                      } else {
                          $class = "greenBar";
                      }
Shouldn't it be:

Code: Select all

if($row["rush"] == true) {
and not

Code: Select all

if($row1["rush"] == true) {
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

No Fix

Post by icesolid »

Actually, my problem is that the program prints out only one record, but different id numbers for the array.

Ex: there are 2 rows of data in the database. It prints out the same name and information on 2 rows, but with the different ids in the array.

I need the other information to spit out for the other row of info.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

More Info

Post by icesolid »

If you need more information explaining this send me a message please!
logic-8
Forum Newbie
Posts: 5
Joined: Sun Jan 22, 2006 6:29 pm

Post by logic-8 »

Maybe try something like this...

Code: Select all

$r = 0;
                  foreach($ids as $id) {
                  $r++;
                  $mod_result = $r % 2;
                      $result = mysql_query("SELECT * FROM cases WHERE location_county='" . $_POST["location_county"] . "' AND assigned='false' ORDER BY control_number ASC");
                      $row = mysql_fetch_array($result);

                      if($mod_result == 0) {
                          $class = "redBar";
                      } else {
                          $class = "greenBar";
                      }
hope this helps -Logic
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I too haven't read through all your code, sorry.. it's difficult to read.

But!

Structure your script in a way so that all the DB interaction happens in one place, then all the displaying of data happens in another - for ease of maintenance (and easy on the eyes when you need to read it :P)

Similar to (in a procedural fashion):

Code: Select all

<?php

//DB Stuff..

function SqlClean ($string)
{
    if (get_magic_quotes_gpc()) {
        $string = stripslashes($string);
    }
    return mysql_real_escape_string($string);
}

$link = mysql_connect('host', 'user', 'pass') or die('Could not connect to DB');
mysql_select_db('db', $link) or die('Could not select DB');

$result = mysql_query("SELECT * FROM `table` WHERE `column` = '" . SqlClean($value) . "'", $link);

$numrows = mysql_num_rows($result);

//display stuff..
//table headers
echo "<table class=\"table\">\n";
echo "<tr class=\"heading\"><td>Header1</td><td>Header2</td><td>etc..</td>\n";

//table content
$c = 0;
while ($row = mysql_fetch_assoc($result)) {
    $class = (($c % 2) == 0 ? 'primary' : 'secondary');

    echo "<tr class=\"{$class}\"><td>{$row['column1']}</td><td>{$row['column2']}</td><td>{$row['etc']}</td></tr>";

    $c++;
}

echo "</table>";

//close off DB stuff..
mysql_free_result($result);
mysql_close($link);

?>
Post Reply