FooKelvin
-
Posts
138 -
Joined
-
Last visited
Posts posted by FooKelvin
-
-
-
I have a questions table and feedback table. I able to foreach the question header but not the feedback.
My code as above:
<?php $sql2 = "SELECT DISTINCT[question_id],[q_text] FROM [question]"; $ques = array(); $stmt2 = sqlsrv_query($conn, $sql2, $ques); while ($row = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) { $ques[$row['question_id']] = $row['q_text']; } $sql = "SELECT [question_id], [Expr3],[Expr2] FROM [feedback] ORDER BY [Expr2] ASC"; $data = array(); $stmt = sqlsrv_query($conn, $sql); while (list($qid, $a, $eid) = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) { if (!isset($data[$qid][$eid])) { $data[$qid][$eid] = $newArray2; } $data[$qid][$eid][] = $a; } ?> <div class="container"> <?php // produce table header echo "<table border='1' id='table2excel'>\n"; echo "<tr><th>Employee ID</th>"; // loop over array of questions for the header foreach ($ques as $question) { echo "<th>$question</th>"; } echo "</tr>\n"; foreach ($data as $qid => $question) { foreach ($question as $question => $replies) { echo "<tr><td>$question</td>"; foreach (array_keys($ques) as $q_id) { echo "<td>"; echo $replies[$q_id]; echo "</td>"; } echo "</tr>\n"; echo "</tr>\n"; } } echo "</table>\n";
Questions Table:
The question 4, might be multiple answer.
Feedback Table:
Result:
Expected Result:
The question id no.4 will allow multiple choice to select, hence there will be multiple feedback for that particular question.
-
Hi All,
Would like to ask whether can the drop down list in html can be control by Jquery with this condition?
Each question can only rate for 3 times, the following drop box for that question should disable and not allow to make any rating.
here is my HTML code:
<div> <span id="questionnaire-question">Q1) Please QuestionA</span> <table> <tr style="width: 365px;"> <td><label>A1</label></td> <td> <select name="ranking[13]" class="02A" group="02A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>A2</label></td> <td> <select name="ranking[14]" class="02A" group="02A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>A3</label></td> <td> <select name="ranking[15]" class="02A" group="02A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>A4</label></td> <td> <select name="ranking[16]" class="02A" group="02A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> </table> </div> <div> <span id="questionnaire-question">Q2) Please QuestionB</span> <table> <tr style="width: 365px;"> <td><label>B1</label></td> <td> <select name="ranking[21]" class="03A" group="03A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>B2</label></td> <td> <select name="ranking[22]" class="03A" group="03A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>B3</label></td> <td> <select name="ranking[23]" class="03A" group="03A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> <tr style="width: 365px;"> <td><label>B4</label></td> <td> <select name="ranking[28]" class="03A" group="03A"> <option value="" >Please Rank</option> <option>1</option> <option>2</option> <option>3</option> </select> </td> </tr> </table> </div>
Thank You.
-
Forget the class thing. It would be nice but it's probably too complicated for you to do right now.
This code
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $rows[] = $row; }
is where you fetch all the data from the database. Inside, each course1 in $row will be a DateTime object. When you json_encode() everything, DateTime objects turn into that date/timezone_type/timezone thing. You don't want that.So instead of using course1 as is, change the value to be a simple string: you need "/Date(", the Unix timestamp which you can get with DateTime::getTimestamp(), and ")/".
$row["course1"] = "/Date(" . $row["course1"]->getTimestamp() . ")/";
Do that before you add $row into $rows.Try that, then post your code if you're still having problems.
Thanks!
now it successfully convert to timestamp format
But yet, the date not able to show up.
hmm...let me figure it out..
-
It doesn't look like you've tried anything I said. How about doing the
from earlier and seeing if that works for you? If not, post the code you tried.
Yup, I tried to put it the php class like this, am i put in a correct place?
class JsonDateTime extends DateTime implements JsonSerializable { //<== Getting Red line error "JsonDateTime" public function __construct(DateTime $dt) { parent::__construct($dt->format("r")); } public function jsonSerialize() { return "/Date(" . $this->getTimestamp() . ")/"; } } $result = sqlsrv_query( $conn, "SELECT COUNT(*) AS RecordCount FROM [A_Sys].[dbo].[testdate]"); $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
The browser show HTTP ERROR 500
-
What is your code?
$result = sqlsrv_query( $conn, "SELECT COUNT(*) AS RecordCount FROM [A_Sys].[dbo].[testdate]"); $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC); $recordCount = $row['RecordCount']; //Get records from database $result = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY [emp_id]) As Row, * FROM [A_Sys].[dbo].[testdate]) As PeopleWithRowNumbers WHERE Row >" . $_REQUEST["jtStartIndex"] . "AND Row <= " . $_GET['jtStartIndex'] ." + ". $_GET['jtPageSize'].""; $stmt = sqlsrv_query($conn, $result); //Add all records to an array $rows = array(); while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $rows[] = $row; } //$recordCount = $row['RecordCount']; //Return result to jTable $jTableResult = array(); $jTableResult['Result'] = "OK"; $jTableResult['TotalRecordCount'] = $recordCount; $jTableResult['Records'] = $rows; print json_encode($jTableResult);
This is my DB:
emp_id course1
---------------------------
1012345 1/5/2017
My Print JSON
{"Result":"OK","TotalRecordCount":1,"Records":[{"Row":"1","emp_id":"1012345","course1":{"date":"2017-01-05 00:00:00","timezone_type":3,"timezone":"Asia\/Brunei"}}]}
-
The first sample code is from documentation, the second code sample is my own output..
Here is the link of documentation.
the problem that i facing now is the date format. i cant get the exactly the date format in the documentation. which is converted to unix timestamp.
Documentation => http://jtable.org/GettingStarted
-
this is my current php code.
$rows = array(); while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $rows[] = $row; // <= do you mean i need to convert it here? } //$recordCount = $row['RecordCount']; //Return result to jTable $jTableResult = array(); $jTableResult['Result'] = "OK"; $jTableResult['TotalRecordCount'] = $recordCount; $jTableResult['Records'] = $rows; print json_encode($jTableResult);
-
Hi All,
I am using JTable Plug in and using PHP code as my server language.
The plug in requires json encode in this format,
** please ignore other data. except for "RecordDate" **
{ "Result":"OK", "Records":[ {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"} ] }
What i have for my output is,
{ "Result": "OK", "TotalRecordCount": 1, "Records": [ {"Row": "1","emp_id": "Nhhh","course1": {"date": "2017-01-06 00:00:00","timezone_type": 3,"timezone": "Asia\/Brunei"} }] }
Im currently using datetime as my data type in my database (SQL Server), and the output is empty due to wrong json date format.
thanks for the help.
-
This is my array:
Array ( [name] => [events] => Array ( [0] =>A.doc [1] =>B.pdf [2] =>C.pdf ) )
when echo it out, there is one extra row in between.
like this..
Please Ignore it. i manage to make it correct..
-
-
So are you going to ignore the solution and keep working on this non-problem just for fun? Seems a bit stupid, but good luck.
Hi Jacquest1, Im testing your code as well..is working actually..
-
It's easy to make PHP look bad when you don't try to use it well.
<?php foreach ($data as $eid => $cdata) { ?> <tr> <td class="eid"><?=html_escape($eid)?></td> <?php foreach ($cdata as $content) { ?> <td> <ul> <?php foreach ($content as $file) { ?> <li><a href="#"><?=html_escape($file)?></a></li> <?php } ?> </ul> </td> <?php } ?> </tr> <?php } ?>
First of all, do you understand what the code you have already is doing? How it works?Yup, my code currently able to join the array value in the same "td", but then i need them to be separate, cause i need to store their own href link.
-
If you can turn the string
<td><a href="#">A.pdf</a>;<br><a href="#">B.pdf</a>;<br><a href="#">C.xlsx</a></td>
into aprefix A.pdf separator B.pdf separator C.xslx suffix
format, all with the same "separator", then it fits into the code you already have.Do you see how you can do that?
Hi requinix,
sorry, I dont get you..
-
Hi, i have my foreach loop codes here. What i trying to do is loop them out and list them with their own href link, but now the problem is, all of them are sharing href link.
My Code:
foreach ($data as $eid => $cdata) { $trows .= "<tr><td class='eid'>{$eid}</td>"; foreach ($cdata as $fc => $content) { $trows .= "<td><a href='#'>" . join(';<br>', $content) ."</a></td>"; } $trows .= "</tr>\n"; }
Here is my output:
After Inspect Element of HTML code:
<tr><td class="eid">E1234</td><td><a href="#">A.pdf;<br>B.pdf;<br>C.xlsx</a></td></tr>
Expected Output:
<tr><td class="eid">E1234</td><td><a href="#">A.pdf</a>;<br><a href="#">B.pdf</a>;<br><a href="#">C.xlsx</a></td></tr>
-
You also need a line of code that adds the month category into the array. Outside of the loop.
$out[$element['category']]['category'] = $element['category'];
Take a look at the documentation for array_values. Do you see what it does to arrays? It strips off the custom keys and replaces them with regular numeric keys.You need to do that to your array before you json_encode() it - those custom keys vs numeric keys are what makes the difference between getting {} objects or [] arrays in the JSON.
Yes Requinix,
Thanks for the explanation, its really helps me!
-
Thanks Requinix,
I have change to
foreach($data as $element) { $out[$element['category']][$element['trainFunction']] = $element['value1']; }
Output:
{"November":{"Engineering":7,"Leadership & Soft Skills":8,"Lean":3,"Quality":4},"December":{"Engineering":2,"Leadership & Soft Skills":3,"Quality":1}}
But i do not understand,
"use array_values() on $out to strip off the outermost keys and turn it into a regular array. "
-
Update Expected Output:
[{ "A": 7, "B": 8, "C": 3, "D": 4, "category": "November" }, { "A": 1, "B": 2, "C": 1, "category": "December" }]
-
Hi All,
Here is my code, trying to encode the result from database.
$stmt = sqlsrv_query($conn, $result); $data = array(); while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $data[] = $row; } //echo json_encode( $data ); $out = array(); foreach($data as $element) { $out[$element['category']][] = array($element['trainFunction']=> $element['value1']); } echo json_encode( $out );
Result of JSON Echo:
{"November":[{"A":7},{"B":8},{"C":3},{"D":4}],"December":[{"A":1},{"B":2},{"C":1}]}
Expected Output:
{ "November": [{ "A": 7, "B": 8, "C": 3, "D": 4 }], "December": [{ "A": 1, "B": 2, "C": 1 }] }
-
Hi Barand,
Sorry. i found where is going wrong. Your code words perfectly. there is because my data type using nvarchar, so the array include a lot of spaces.
thanks again Barand.
-
Hi Barand,
am i missed out anything? because the output show "completed only"
I tried to understand the for loop :
$certres = "Entitle for Cert $cert"; for ($lvl = 1; $lvl <= $cert; $lvl++) { echo "cert:".$cert; //<-- new added echo "level:".$lvl; //<-- new added if (min($studata[$lvl]) < 50) { $certres = 'Complete only'; break; } }
i found that :
cert:1 level:1cert:2 level:1
-
Thanks Barand, its working perfectly.
I have change on the condition and this more reality.
For the certification level, the level 2 certificate should not entitle if the student fail to entitle certificate level 1.meaning,in order to get cert level 1,- student have to pass MS1 until MS6
in order to get cert level 2,- student have to pass MS1 until MS9
To do this, i should check thru each row array?or can it be like check get Certificate 1 (complete only or Entitle for cert1) column value to check? -
I would have expected your course data to contain a reference to the certificate level (1 or 2)
Hi Barand,
You mean
Certificate 1>MS1-MS6
Certificate 2 >MS7-MS12
?
-
Hi,
I have a list of tracking, separate by module. Example
- Certificate 1
- MS1
- MS2
- MS3
- MS4
- MS5
- MS6
- Certificate 2
- MS7
- MS8
- MS9
- MS10
- MS11
- MS12
Each module have their own scoring. There is also another filed to track the status.
In order to entitle for the certificate 1, they need to pass all the module under certificate 1. the passing condition >= 50.
Eg: In order to get Certificate 1, the marks of MS1 - MS6 should >=50.
What i facing now,
- i failed to sum the marks
- the status of certificate display in the wrong column.
here is my code:
$newarray = array(1 => array(), 2 => array(), 3 => array(), 4 => array(), 5 => array(), 6 => array(), 7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array(), 12 => array()); $data = array(); $trows = ''; $sql = "SELECT eid,[Name] ,[course] ,[marks] ,[id] ,[courselvl] FROM [scview]"; $stmt = sqlsrv_query($conn, $sql); while (list($eid, $name, $course, $marks, $id, $courselvl,$a) = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) { if (!isset($data[$eid])) { //$data[$eid][$qid] = $newArray2; $data[$eid]['name'] = $name; $data[$eid]['module'] = $newarray; } $item = $marks; $data[$eid]['module'][$id][] = $item; } foreach ($data as $id => $mdata) { $trows .= "<tr><td>{$mdata['name']}</td>"; foreach ($mdata['module'] as $id => $marks) { $trows .= "<td>" . join(';<br>', $marks) . "</td>"; } $trows .="<td>Entitle For Cert 1</td>"; $trows .="<td>Entitle For Cert 2</td>"; $trows .= "</tr>\n"; }; ?> <table border='1' id="example" style="width:100%"> <thead> <tr> <th>Name</th> <th>MS1</th> <th>MS2</th> <th>MS3</th> <th>MS4</th> <th>MS5</th> <th>MS6</th> <th>Certificate</th> <th>MS7</th> <th>MS8</th> <th>MS9</th> <th>MS10</th> <th>MS11</th> <th>MS12</th> <th>Certificate2</th> </tr> </thead> <tbody> <?php echo $trows; ?> </tbody> </table> </body>
Here is my DB. I have 2 DB, and i join them.
course table:
id course 1 MS1 2 MS2 3 MS3 4 MS4 5 MS5 6 MS6 7 MS7 8 MS8 9 MS9 10 MS10 11 MS11 12 MS12
enrollment table:
Name course marks eid Lim MS7 40 e2 Lim MS8 50 e2 Lim MS9 66 e2 Lim MS10 76 e2 Lim MS11 56 e2 Lim MS12 55 e2 Lim MS1 40 e2 Lim MS2 50 e2 Lim MS3 66 e2 Lim MS4 76 e2 Lim MS5 56 e2 Lim MS6 55 e2 Foo MS1 60 e1 Foo MS2 60 e1 Foo MS3 76 e1 Foo MS4 76 e1 Foo MS5 86 e1 Foo MS6 75 e1 Foo MS7 30 e1 Foo MS8 30 e1 Foo MS9 36 e1 Foo MS10 36 e1 Foo MS11 66 e1 Foo MS12 65 e1
After Join table, and i run the query Output:
Name course marks id courselvl eid Lim MS7 40 7 MS7 e2 Lim MS8 50 8 MS8 e2 Lim MS9 66 9 MS9 e2 Lim MS10 76 10 MS10 e2 Lim MS11 56 11 MS11 e2 Lim MS12 55 12 MS12 e2 Lim MS1 40 1 MS1 e2 Lim MS2 50 2 MS2 e2 Lim MS3 66 3 MS3 e2 Lim MS4 76 4 MS4 e2 Lim MS5 56 5 MS5 e2 Lim MS6 55 6 MS6 e2 Foo MS1 60 1 MS1 e1 Foo MS2 60 2 MS2 e1 Foo MS3 76 3 MS3 e1 Foo MS4 76 4 MS4 e1 Foo MS5 86 5 MS5 e1 Foo MS6 75 6 MS6 e1 Foo MS7 30 7 MS7 e1 Foo MS8 30 8 MS8 e1 Foo MS9 36 9 MS9 e1 Foo MS10 36 10 MS10 e1 Foo MS11 66 11 MS11 e1 Foo MS12 65 12 MS12 e1
###############################################################################################
The output that display in browser:
Expected Output:
Thank You
- Certificate 1
Foreach no result
in PHP Coding Help
Posted
Hi mac, yes...it is similar questions, but due to
when i export to excel, it automatically merge the table in excel, which is good.. some how i face problem when i Vloopup in excel file, so i am thinking to split them when it has multiple feedback.