Jump to content

newphpcoder

Members
  • Posts

    254
  • Joined

  • Last visited

Everything posted by newphpcoder

  1. Hi, It's my first time to used foreach loop to display data from database and also used foreach loop to display textboxes Now, I felt difficulties in saving data from the textboxes displayed by foreach. Here is my code: <html> <head> </head> <body> <form name="operator_report" action="" method="post" autocomplete="off"> <?php ob_start(); include "connection.php"; $save = isset($_POST['save']); //insert data to database if($save) { $compound_type = $_POST['compound_type']; $op_output = $_POST['output']; $c_output = $_POST['output']; $reject_type[] = $_POST['reject_type']; $reject[] = $_POST['reject']; for($s = 0; $s < count($compound_type) ; $s++) { //---save operators output to op_output, in this part i don't have problem in saving the data saved correctly if ($op_output[$s] !="") { $sql = "INSERT INTO op_output (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output) VALUES ('001', '4', '1','2013-07-16', '1', '$compound_type[$s]', '$op_output[$s]')"; $result = mysql_query($sql, $con); } //---this part I need help in saving reject. //---I guess i have the problem here in looping ... //---this code did not work.. for($i = 0; $i < count($reject_type[$s]) ; $i++) { if($reject[$i] != "") { $sql_re = "INSERT INTO op_reject (op_number, compound_type, reject_type, reject) VALUES ('001','$compound_type[$s]', '$reject_type[$i]', '$reject[$i]')"; $result_re = mysql_query($sql_re, $con); } } } } $id = 'Final Mix'; if($id == 'Final Mix') { echo "<fieldset>"; echo "<legend><H1> Operator's Output and Reject</H1></legend>"; echo "<table>"; echo "<tr>"; echo "<th>Compound</th>"; echo "<th>Output</th>"; //query to select reject acronym $sql = "select r.reject_acro, r.reject_id from process_list AS p LEFT JOIN reject_list AS r ON p.reject_id = r.reject_process_id where p.process_name LIKE '" .$id. "%'"; $rsd = mysql_query($sql); while($rs = mysql_fetch_assoc($rsd)) { $reject[] = $rs['reject_acro']; $reject_id[] = $rs['reject_id']; } if(empty($reject)) { echo""; } else { //display reject acronym as header foreach ($reject as $reject) { echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject' style='border:none; font-weight: bold; ' size='5'></th>"; } echo "</tr>"; } //query select compound_type $sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC"; $res = mysql_query($sql_comp); echo "<tr>"; while($comp = mysql_fetch_assoc($res)){ $compound_type = $comp['compound_type']; //---display compound_type echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'readonly='readonly'></td>"; //---input box for output echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>"; //----i used foreach to loop textbox in every reject acronym. foreach($reject_id AS $reject) { echo "<td style='border:none;'><input type='text' name='reject[]' id='reject' value='' size='7'></td>"; } echo "</tr>"; } echo "</table>"; echo "</fieldset>"; } ?> <input type="submit" name="save" id="save" value="Save"> </form> </body> </html> I also attached my sample output screenshots and the database. Here is the sample scenario. Compound Type---Output---SC---SP---SH--- P28--------------10-------------1-----2 P32--------------20--------5--------------- P32NW--------------------------3---------- I need to save data on op_reject like this: op_number---compound_type----reject_type---reject 001----------P28---------------SP------------1 001----------P28---------------SH------------2 001----------P32---------------SC------------5 001----------P32NW------------SP------------3 Sad to say in my code in saving using forloop it did not work. I post it because i am hoping somebody can help me, but I also tried my best to fixed this. Any help is highly appreciated. Thank you so much. sample.zip
  2. Hi, I have data: shift_id = 1---2---3---4---5 compound_output = 5---10--20--50--10 process_id = 7 compound_type = P28 and now I need to create a query that sum the value of (1+3+(5/ 2)) and (2+4+(5/ 2)) here is my query: SELECT process_id, compound_type, CASE WHEN (shift_id IS NOT NULL AND (shift_id = 1 OR shift_id = 3 OR shift_id = 5)) THEN SUM((compound_output)/2) when (shift_id IS NOT NULL AND (shift_id = 2 OR shift_id = 4 OR shift_id = 5)) THEN SUM((compound_output)/2) END AS val FROM op_output WHERE process_id = 7 and compound_type = 'P28' HAVING val IS NOT NULL and the output of this is: val = 47.500000 but it should be: 7---P28--30 7---P28--65 Thank you so much.. I hope somebody can help me to get the correct values. Thank you so much.
  3. Hi, I have form that displaying report per week. First thing to do is choose what week then automatically display the date/shift that week ranges. here is my weekly_report.php <?php error_reporting(0); session_start(); ob_start(); date_default_timezone_set("Asia/Singapore"); include('connection.php'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <title>Weekly Report</title> <head> <link rel="stylesheet" type="text/css" href="op_report.css" /> <script type="text/javascript" src="jquery.js"></script> <script type='text/javascript' src='jquery.autocomplete.js'></script> <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" /> <script type="text/javascript"> //----auto complete week--// $().ready(function() { $("#week_selected").autocomplete("get_week_list.php", { width: 115, matchContains: true, mustMatch: true, selectFirst: false }); $("#week_selected").result(function(event, data, formatted) { $("#week_number").val(data[1]); }); }); /*AJAX*/ function AJAX(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari return xmlHttp; } catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer return xmlHttp; } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; } catch (e){ alert("Your browser does not support AJAX!"); return false; } } } } //-----get weekdata from week---// function getweekdata() { // if (window.event.keyCode==13 || window.event.keyCode==10) { divid = "week_data"; var url = "get_weekly_data.php"; var str = "id=" + document.getElementById("week_number").value; var xmlHttp = AJAX(); xmlHttp.onreadystatechange = function(){ if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){ // document.getElementById(divid).innerHTML=loadingmessage; } if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { var jsonart = xmlHttp.responseText; document.getElementById(divid).innerHTML = jsonart; } } } xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Content-length", str.length); xmlHttp.setRequestHeader("Connection", "close"); xmlHttp.send(str); // } } </script> </head> <body onload=document.getElementById("week_selected").focus();> <form name="weekly_report" action="" method="post"> <div id="ddcolortabs"> <ul> <li> <a href="index.php" title="Operator's Shift Report"><span>Operator's Shift Report</span></a></li> <li id="current"> <a href="weekly_report.php" title="Reports"><span>Reports</span></a></li> </ul> </div> <br/> <div> <table> <tr> <td style="border: none;">Type Week:</td> <td><input type="text" name="week_selected" id="week_selected" value="" size="15" onkeyup="getweekdata();"></td> </tr> </table> </div> <input type="hidden" name="week_number" id="week_number"> <div id='week_data'> </div> </form> </body> </html> //get_week_list.php <?php ob_start(); include "connection.php"; $q = strtolower($_GET["q"]); if ($q == '') { header("HTTP/1.0 404 Not Found", true, 404); } //else (!$q) return; else{ $sql = "select week_id, week_number from week_list where week_number LIKE '$q%'"; $rsd = mysql_query($sql); $cnt = mysql_num_rows($rsd); if($cnt > 0) { while($rs = mysql_fetch_array($rsd)) { $pid = $rs['week_id']; $pname = $rs['week_number']; echo "$pname|$pid\n"; } } else { header("HTTP/1.0 404 Not Found", true, 404); } } ?> and here is my get_weekly_data.php // which display the data for that week. <?php ob_start(); include "connection.php"; if($_POST["id"]) { $sql = "select r.report_date, s.shift_type FROM op_reports AS r, shift_list AS s WHERE WEEK(report_date) + 1 = '" . ($_POST["id"]) . "' GROUP BY shift_type ORDER BY shift_id ASC"; $res = mysql_query($sql); echo "<table>"; echo "<tr>"; echo "<th>Comp</th>"; while($row = mysql_fetch_assoc($res)) { $report_date = $row['report_date']; $report_shift = $row['shift_type']; echo "<th>$report_date/$report_shift</th>"; } echo "</tr>"; $sql_r = "select r.report_date, s.shift_type FROM op_reports AS r, shift_list AS s WHERE WEEK(report_date) + 1 = '" . ($_POST["id"]) . "' GROUP BY shift_type ORDER BY shift_type DESC"; $res_r = mysql_query($sql_r); echo "<tr>"; echo "<th></th>"; while($r = mysql_fetch_assoc($res_r)){ echo "<th>Output</th>"; } echo "</tr>"; $sql_comp = "SELECT DISTINCT p.process_name , r.process_id, r.report_shift FROM op_reports AS r JOIN process_list AS p ON (p.process_id = r.process_id) WHERE WEEK(report_date) + 1 = '" . ($_POST["id"]) . "' GROUP BY process_name ORDER BY p.process_name ASC"; $res_comp = mysql_query($sql_comp); echo "<tr>"; while($row_comp = mysql_fetch_assoc($res_comp)) { $process = $row_comp['process_name']; $process_id = $row_comp['process_id']; echo "<td>$process</td>"; $comp = "SELECT DISTINCT o.compound_type, o.process_id, o.shift_date FROM op_output AS o WHERE process_id = '$process_id' GROUP BY o.shift_id, compound_type ORDER BY compound_type ASC"; $c = mysql_query($comp); echo "<tr>"; while($co = mysql_fetch_assoc($c)) { $compound_type = $co['compound_type']; $process_i = $co['process_id']; $shift_date = $co['shift_date']; echo "<td>$compound_type</td>"; $sql_output = "SELECT DISTINCT o.compound_type, SUM(o.compound_output) AS compound_output, o.process_id, o.shift_id, o.shift_date FROM op_output AS o WHERE process_id = '$process_id' AND o.compound_type = '$compound_type' GROUP BY o.shift_id, o.shift_date, compound_type ORDER BY o.shift_id ASC"; $res_output = mysql_query($sql_output); while($row_output = mysql_fetch_assoc($res_output)) { $compound_output = $row_output['compound_output']; $shift = $row_output['shift_id']; $compound = $row_output['compound_type']; $date = $row_output['shift_date']; if($shift == 1 && $date == $shift_date) { echo"<td>$compound_output</td>"; } else { echo "<td></td>"; } if($shift == 2 && $date == $shift_date) { echo"<td>$compound_output</td>"; } else { echo "<td></td>"; } if($shift == 3 && $date == $shift_date) { echo"<td>$compound_output</td>"; } else { echo "<td></td>"; } if($shift == 4 && $date == $shift_date) { echo"<td>$compound_output</td>"; } else { echo "<td></td>"; } if($shift == 5 && $date == $shift_date) { echo"<td>$compound_output</td>"; } else { echo "<td></td>"; } } echo "</tr>"; } } echo "</tr>"; echo "</table>"; } ?> I attached my sample screenshots that display by this code and also a format that I need to display and the database. My problem now is on displaying of output per date/shift. It's been todays that I tried to fixed this. I hope somebody can help me. Thank you so much sample updated.zip
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.