Forum Moderators: coopster

Message Too Old, No Replies

Colors in array

         

Shaman13

6:34 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Perhaps one of you hot shots can help me with this riddle. How can I make the following array alternate row colors. I know.. I know There are hundreds of examples of this out there. But I haven't found any that also incorporate the ability to sort in the following code. Any and all suggestions are really appreciated! Have a Great Day!
<?php

/* set the allowed order by columns */
$default_sort = 'id';
$allowed_order = array ('id', 'name','PCODEGROUP','FULLGROUP');

/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}

/* connect to db */
mysql_connect ('localhost','root','');
mysql_select_db ('nhla_cases');

/* construct and run our query */
$query = "SELECT * FROM problem ORDER BY $order";
$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);

echo "<TABLE border=1>\n";

echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";

/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";

foreach ($row as $column) {
echo "<TD>$column</TD>\n";

}
echo "</TR>\n";

}

echo "</TABLE>\n";
?>

jatar_k

7:11 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



didn't test this but it's close

$count = 1; 
$c1 = '336699';
$c2 = 'ffffff';
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR bgcolor='#" . ${'c' . $count} . "'>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
$count==1?2:1;
}

bgcolor in the tr is kinda hacky too ;) I would use classes and assign those but, oh well

Shaman13

7:34 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Well that got me the color. But not the alternating rows of color. I'm at a loss. Appreciate the tip but could you help me tweak it just a bit to get the rows to alternate. Also its seems to slow down my query significantly. Why would that be. Thanks

jatar_k

7:40 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the line near the end was wrong, should be

$count=($count==1)?2:1;

like i said, didn't test, not sure why the query would slow down, not much to it

Shaman13

7:50 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Thanks jatar_k! I tried it but no alternating colors yet. I really appreciate the help. I am totally over my head! I scoffed this script up on the web instead of trying to write it myself. Anything else I can try? I appreciate any suggestions! Thanks

jatar_k

8:09 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry the above worked for me, I actually tested it now

how about this snippet for another, slightly different, example

$colour0 = '#336699';
$colour1 = '#ffaa00';
$counter = 0;
while ($counter < 10) {
echo "<br><font color='" . ${colour . ($counter % 2)} . "'>this colour</font>";
$counter++;
}

Shaman13

8:23 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



jatar_k I guess my life was just meant to be colorless! The last snippet actually created the desired effect but not within the table. Above it! I am an idiot. I genuinely appreciate your efforts and hate to waste your time. Thanks

jatar_k

9:20 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that last snippet was just an example of the method, you would have to weave it into your code

mcibor

9:31 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I learned this code somewhere:

$color = array("#0000FF", "#00FF00");
$i = 0;
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR bgcolor=\"".$color[$i]."\">\n";
$i = 1 - $i;

This works fine for two colors
Michal

Shaman13

5:06 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



MCIBOR AND JATAR_K

I really appreciate the suggestions! But being an idiotic newby to Php I have no idea where to plug this into my code! Could I prevail upon you to show me where it might fit in my existing code below and a brief explanation as to how it works. I am struggling to learn some of the more sophisticated coding now.

[edited by: jatar_k at 5:11 pm (utc) on June 8, 2005]
[edit reason] removed code - was the same as msg1 [/edit]

jatar_k

5:35 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my understanding of your code is that this is the chunk that shows the rows of data

/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";

foreach ($row as $column) {
echo "<TD>$column</TD>\n";

}
echo "</TR>\n";

so that is all you need to change to do the colours. In my original fix (which works for me, sry) I edited this code exactly as did mcibor in msg 9.

you should be able to replace some lines with ours and get one of the two to work.

Shaman13

5:42 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Hey Thanks to those last couple of hints I got it! I still feel pretty stupid though. I appreciate the time you guys took to try to help. Just confirms This is a great forum. Everybody is really helpful. I have incorporated the snippet you gave me into my table but now I can't figure out why I am getting duplicate listing for each record. Here is what I have so far.
------------------------------
<?php

/* set the allowed order by columns */
$default_sort = 'CLNAME';
$allowed_order = array ('CLIENTNUM','CASENUM','CLNAME','CFNAME','PCODE','COPEN');
//Added to attempt to create alternating colors
$color = array("#87ECF8", "#CFCFCF");
$i = 0;

/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}

/* connect to db */
mysql_connect ('localhost','root','');
mysql_select_db ('nhla_cases');

/* construct and run our query */
$query = "SELECT CLIENTNUM,CASENUM,CFNAME,CLNAME,PCODE,COPEN FROM _clientsw_cases ORDER BY $order LIMIT 25";
$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);

echo "<TABLE border=0>\n";

echo "<TR>\n";

foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";

if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";

/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);

//while ($row = mysql_fetch_assoc ($result))
while ($row = mysql_fetch_assoc ($result))
//
{
echo "<TR bgcolor=\"".$color[$i]."\">\n";
$i = 1 - $i;
echo '<td>'.$row['CFNAME'].'</td><td>'.$row['CLNAME'].'</td><td>'.$row['PCODE'].'</td><td>'.$row['COPEN'].'</td><td><a
href="money.php? CLIENTNUM='.$row['CLIENTNUM'].'">eligibility</a> ¦<a
href="caseinfo3.php? CASENUM='.$row['CASENUM'].'">case</a> ¦<a
href="caseinfoc.php? CASENUM='.$row['CASENUM'].'">close case</a> ¦<a
href="clientinfo3.php? CLIENTNUM='.$row['CLIENTNUM'].'">client</a> ¦ <TD><a href="T12.php?CASENUM='.$row['CASENUM'].','.$row['CFNAME'].','.$row['CLNAME'].'">Add Time</a></td><td><a href="case_events.php? CASENUM='.$row['CASENUM'].'">Case Hours</a></td></tr>';

//{

//echo "<TR>\n";

foreach ($row as $column) {
echo "<TD>$column</TD>\n";

}
echo "</TR>\n";

}

echo "</TABLE>\n";

?>

Thanks a million!

jatar_k

7:04 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



whats this for

foreach ($row as $column) {
echo "<TD>$column</TD>\n";

}