newphpcoder Posted March 16, 2012 Share Posted March 16, 2012 hi... I have query to display data and I use while loop. here is my code: echo "<table id='kanban_list'>"; echo "<tr> <th><label>Display Details:</label><input onclick='showDetails(this);' id='chkDetail' type='checkbox' checked='checked' value='wip'/></th> <th> PCODE </th> <th> LOT CODE </th> <th> CHEMICAL WEIGHING </th> <th> COMPOUNDING </th> <th> EXTRUSION </th> <th> FORMING </th> <th> DEFLASHING </th> <th> KANBAN </th> <th> VIRTUAL </th> <th> TOTAL </th> <!--<th> MIN LOT </th>--> </tr>"; $sql = "SELECT DISTINCT kd.PCODE, kc.count_wip_chemical_weighing, kc.count_wip_compounding, kc.count_wip_extrusion, kc.count_wip_forming, kc.count_wip_deflashing, kc.kanban, kc.virtual, kc.total FROM kanban_checker kc JOIN kanban_data kd ON kc.PCODE = kd.PCODE ORDER BY kc.PCODE"; $result = mysql_query($sql, $con); while($row = mysql_fetch_assoc($result)){ echo "<tr> <td>Total Lot ID (Lot)</td> <td> $row[PCODE]</td> <td> </td> <td> $row[count_wip_chemical_weighing]</td> <td> $row[count_wip_compounding]</td> <td> $row[count_wip_extrusion]</td> <td> $row[count_wip_forming]</td> <td> $row[count_wip_deflashing]</td> <td> $row[kanban]</td> <td> $row[virtual]</td> <td> $row[total]</td> <!-- <td> $row[min_lot]</td> --> </tr>"; } $sql = "SELECT DISTINCT kd.PCODE, kdc.count_doz_chemical_weighing, kdc.count_doz_compounding, kdc.count_doz_extrusion, kdc.count_doz_forming, kdc.count_doz_deflashing, kdc.kanban_doz, kdc.virtual_doz, kdc.total_doz FROM kanban_checker_doz kdc JOIN kanban_data kd ON kdc.PCODE = kd.PCODE WHERE kd.PCODE = '$row[PCODE]' ORDER BY kdc.PCODE"; $result_qty = mysql_query($sql, $con); while ($row_doz = mysql_fetch_assoc($result_qty)){ echo "<tr> <td>Total Qty (Doz)</td> <!--<td> </td> --> <td> $row_doz[PCODE]</td> <td> </td> <td> $row_doz[count_doz_chemical_weighing]</td> <td> $row_doz[count_doz_compounding]</td> <td> $row_doz[count_doz_extrusion]</td> <td> $row_doz[count_doz_forming]</td> <td> $row_doz[count_doz_deflashing]</td> <td> $row_doz[kanban_doz]</td> <td> $row_doz[virtual_doz]</td> <td> $row_doz[total_doz]</td> <!-- <td> $row[min_lot]</td> --> </tr>"; } echo "</table>"; sample data: Display Details---Compound---Chemical Weighing-----etc... ---------------------P28------------1.00---------------------------- ---------------------P28------------250.00------------------------ ---------------------P30------------2.00-------------------------- ---------------------P30------------520.00--------------------- ---------------------P32------------1.00---------------------------- I need to add alternate background color per compound. for example: P28 green P30 yellow P32 green P33 yellow and so on. Any help is highly appreciated. Thank you ---------------------P32------------520.00----------------------- ---------------------P33------------4.00---------------------------- ---------------------P33------------1000.00--------------------- Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/ Share on other sites More sharing options...
requinix Posted March 16, 2012 Share Posted March 16, 2012 The mechanism is basically the same every time you want to do alternating anything in a loop: $color = the first color you want to use loop { print whatever you want using that $color $color = ($color == first color ? second color : first color); } That ?: line alternates the color: when it's one color it sets it to the other. Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1327998 Share on other sites More sharing options...
Mahngiel Posted March 18, 2012 Share Posted March 18, 2012 the :nth-child pseudo class is perfect for this situation. tr:nth-child { color: blue; } tr:nth-child(even) { color: green; } This will make your rows alternate the font color, with every even (2nd, 4th, 6th, ...) row being green. Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1328854 Share on other sites More sharing options...
newphpcoder Posted March 19, 2012 Author Share Posted March 19, 2012 hi.. already resolve this thread using this code: while ($row_doz = mysql_fetch_assoc($result_qty)){ if($row_doz[PCODE] == 'P28') { $css_class = 'blue-css-class-name'; } elseif($row_doz[PCODE] == 'P30') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P32') { $css_class = 'blue-css-class-name'; } elseif ($row_doz[PCODE] == 'P33') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P35') { $css_class = 'blue-css-class-name'; } elseif ($row_doz[PCODE] == 'P35M') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P35W') { $css_class = 'blue-css-class-name'; } elseif ($row_doz[PCODE] == 'P38') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P41') { $css_class = 'blue-css-class-name'; } elseif ($row_doz[PCODE] == 'P42') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P43') { $css_class = 'blue-css-class-name'; } elseif ($row_doz[PCODE] == 'P46') { $css_class = 'green-css-class-name'; } elseif ($row_doz[PCODE] == 'P47') { $css_class = 'blue-css-class-name'; } else{ $css_class = ''; } echo "<tr class='".$css_class."'> <td>Total Qty (Doz)</td> <!--<td> </td> --> <td> $row_doz[PCODE]</td> <td> </td> <!-- <td>$row[LOT_CODE]</td> --> <td> $row_doz[count_doz_chemical_weighing]</td> <td> $row_doz[count_doz_compounding]</td> <td> $row_doz[count_doz_extrusion]</td> <td> $row_doz[count_doz_forming]</td> <td> $row_doz[count_doz_deflashing]</td> <td> $row_doz[kanban_doz]</td> <td> $row_doz[virtual_doz]</td> <td> $row_doz[total_doz]</td> <!-- <td> $row[min_lot]</td> --> </tr>"; } thank you Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1328886 Share on other sites More sharing options...
Mahngiel Posted March 19, 2012 Share Posted March 19, 2012 Foremost, if it's solved, please mark the thread as such. Secondly, what are you going to do every time there is a change in your PCODEs? Are you going to manually add another conditional statement? How about in the future when somebody else is working with the code, will it still maintain itself? My suggestion to you would be to remove those unneeded conditional statements and simply apply the PCODE value as the class, to wit you could simply markup in your .CSS and never have to write another conditional regarding that again. while ($row_doz = mysql_fetch_assoc($result_qty)){ echo "<tr class='". $row_doz[PCODE] ."'> ... Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1328892 Share on other sites More sharing options...
newphpcoder Posted March 19, 2012 Author Share Posted March 19, 2012 Is it the same output? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1328928 Share on other sites More sharing options...
Mahngiel Posted March 19, 2012 Share Posted March 19, 2012 No, in your previous post, you had set the value of $css_class to some color depending on the PCODE. That is an unnecessary step as you could just identify the rwo by it's product code instead of same conditionally assigned value. Example: <tr class="P38">...</tr> <tr class="P32">...</tr> <style> .P38 { color: blue; } .P32 { color: green; } </style> The same thing is accomplished without the need for all those silly elseif conditionals. Quote Link to comment https://forums.phpfreaks.com/topic/259041-add-css-code-inside-while-loop/#findComment-1329006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.