newphpcoder Posted February 29, 2012 Share Posted February 29, 2012 Hi.. I have while loop inside while loop but I encountered problem in displaying data inside while loop or the second while loop. here is my code: <?php error_reporting(0); $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <script type="text/javascript"> function showDetails(pcode) { var clickElement = pcode.value; var click_id = pcode.id; // var value_ = document.getElementById(click_id).checked // = document.getElementById(clickElement).checked; //var Table = (document.getElementsByName('list')[0].value); var Table = document.getElementById('kanban_list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; //alert(strSelect) for (var i = 0; i < rows.length; i++) { var row = rows[i]; //row.style.display = (row.id.substr(0,3) == strSelect) ? 'none' : ''; //row.style.display = (row.id.substr(0,3) == strSelect) ? // row.style.display = 'none'; // row.style.display = ''; if (row.id.substr(0,3) == strSelect) { row.style.display = ((document.getElementById(click_id).checked) == false) ? 'none' : '' } //(document.getElementById(click_id).checked == false) ? 'none' : '' : ''; } } </script> <?php $sql = "SELECT kc.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, p.max_lot, p.min_lot FROM kanban_checker kc JOIN plan p ON kc.PCODE = p.PCODE ORDER BY p.PCODE"; $result = mysql_query($sql, $con); ?><label>Display Details:</label><input onclick='showDetails(this);' id='chkDetail' type='checkbox' checked='checked' value='wip'/> <?php echo "<table id='kanban_list'>"; echo "<tr> <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> MAX LOT </th> <th> MIN LOT </th> </tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr> <td>$row[PCODE]</td> <td> </td> <!-- <td>$row[LOT_CODE]</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[max_lot]</td> <td>$row[min_lot]</td> </tr>"; $sql = "SELECT kd.LOT_CODE, kd.wip_chemicalweighing, kd.wip_compounding, kd.wip_extrusion, kd.wip_forming, kd.wip_deflashing FROM kanban_data kd JOIN plan p ON kd.PCODE = p.PCODE ORDER BY p.PCODE "; $result_kanban_data = mysql_query($sql, $con); while($row_data = mysql_fetch_assoc($result_kanban_data)){ echo "<tr id='wip'> <td></td> <td> $row_data[LOT_CODE]</td> <td> $row_data[wip_chemicalweighing]</td> <td> $row_data[wip_compounding]</td> <td> $row_data[wip_extrusion]</td> <td> $row_data[wip_forming]</td> <td> $row_data[wip_deflashing]</td> </tr>"; } } echo "</table>"; ?> I attach the image of result from this code But the output that I want is all LOT_CODE with P35 PCODE will only display below P35 and all LOTCODE with P35M PCODE will only display below P35M. Thank you I hope somebody can help me.. Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/ Share on other sites More sharing options...
Muddy_Funster Posted February 29, 2012 Share Posted February 29, 2012 ok, running loops inside other loops is, of course, fine. However, running queries inside of loops is really not. You can get all the information from a single query and then use conditional logic to display it the way you want. I recomend revising your whole fetch code to use only a single query. Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322224 Share on other sites More sharing options...
newphpcoder Posted February 29, 2012 Author Share Posted February 29, 2012 I tried it using this query: SELECT kc.PCODE, kd.LOT_CODE, kd.wip_chemicalweighing, kd.wip_compounding, kd.wip_extrusion, kd.wip_forming, kd.wip_deflashing, 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, p.max_lot, p.min_lot FROM kanban_data kd JOIN kanban_checker kc ON kd.PCODE = kc.PCODE JOIN plan p ON kc.PCODE = p.PCODE ORDER BY kc.PCODE And I attach the result of this query. As you can see the firstrow repeated. which I don't want to happen ... I want the first row will be distinct. Thank you But I got problem in displaying data from tables plan and kanban_checker I want them display only from first row not display in second row which is kanban_data. I want plan and kanban_checkewr will be distinct. I tried everything but still I'm failed. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322226 Share on other sites More sharing options...
Muddy_Funster Posted February 29, 2012 Share Posted February 29, 2012 use conditional logic on the display code. $checkMe = ""; while(...){ if ($field['returned'] != $checkMe){ //check if the value of the field is new $checkMe = $field['returned']; // if it is set it as the value to check the next field against echo "<td>$checkMe</td>"; // and display the new value } else{ echo "<td> </td>"; // if the value is not new only display blank space } //...rest of code inside while loop } Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322230 Share on other sites More sharing options...
newphpcoder Posted February 29, 2012 Author Share Posted February 29, 2012 Sorry, I don't understand what is the use of if condition Thank you Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322232 Share on other sites More sharing options...
Muddy_Funster Posted February 29, 2012 Share Posted February 29, 2012 really? what don't you understand about it? Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322235 Share on other sites More sharing options...
newphpcoder Posted February 29, 2012 Author Share Posted February 29, 2012 this part: $checkMe = "";while(...){if ($field['returned'] != $checkMe){ //check if the value of the field is new$checkMe = $field['returned']; // if it is set it as the value to check the next field againstecho "<td>$checkMe</td>"; // and display the new value}else{echo "<td> </td>"; // if the value is not new only display blank space} i don't know what code should I will replace that thank you Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322236 Share on other sites More sharing options...
newphpcoder Posted February 29, 2012 Author Share Posted February 29, 2012 I don't know how can I display First Row which is the PCODE-----ect. once Help me :'( Quote Link to comment https://forums.phpfreaks.com/topic/257966-while-loop-inside-while-loop/#findComment-1322237 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.