I asked this question yesterday but no one replied after I tried to get further help. I've been trying to figure out the problem in the code I was given in my previous post but it's not giving me the results I want. Right now what it does is it writes a separator after each row but what I really want is if the name of the manager from the previous row is different then write a separator and keep going on with loop.
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
You're mixing $row and $rows, you would be okay doing this:
$team = "";
while ($row = mysqli_fetch_array($results)) {
if($team && ($row['Manager'] != $team)){ // skip first separator
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
$team = $row['Manager'];
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
Am I missing something, or can you simply display the data, then assign it to the variable further in the loop?
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">".$team."</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
$team = $row[0]['Manager'];
}
}
What is $rowS['Manager'] ? $rowS is unknown. use $row['Manager'] like:
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team) {
$team = $row['Manager'];
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
Just move the value allocation $team = $rows['Manager']; in the if condition.
while ($row = mysqli_fetch_array($results)) {
$team = "";
foreach($row as $rows){
if($rows['Manager'] != $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
$team = $rows['Manager'];
}
echo "<tr>";
echo "<td>".$rows['Manager']."</td>";
echo "<td>".$rows['Employee_ID']."</td>";
echo "<td>".$rows['Name']."</td>";
echo "</tr>";
}
}
Related
Good day,
I am trying to select a table from MySQL and generaly I use this code:
$sql="CALL selectCreatedTableByName('".$tableNameIn."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='restable'>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['nameE'] . "</td>";
echo "<td>" . $row['nameN'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();
but now, when I don't know the names of the table's columns, how can I select this table?
Thank you so much.
You are not selecting the table, you are selecting from the table. (at least I'm guessing that is the case since we don't know what your procedure actually does).
The PHP array returned uses the attribute names from the resultset as keys hence....
while($row = $result->fetch_assoc()) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
If you want a header row, then use a state variable to flag the first row.
$fetched=0;
while($row = $result->fetch_assoc()) {
if (!fetched) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<th>$name</th>\n";
}
echo "</tr>\n";
}
$fetched++;
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
I have the below code to show all data from a MySQL database in a HTMl database:
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!
For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=
Is there a way to decrypt the data before displaying it in the table?
I usually decrpyt the date in the following way:
$name_firstD = simple_decrypt($name_first , "secured");
You need to have an array of columns which are encrypted.
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
$encrypted_columns = array('password','code', 'first_name');
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
put the name of your encrypted column inside $encrypted_columns array.
Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.
i want it to echo a (-) where a student does not take the subject.thanks in advance
<table border='1'>
<tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
</tr>
<?php
//loop to display the names of students
$i = 1;
foreach ($overallresults as $result) {
echo "<tr>";
echo "<td>";
echo $i++;
echo "</td>";
$admNo = $result->admNo;
$total_marks = $result->total_marks;
$agp = $result->aggregate_points;
$mean_grade = $result->mean_grade;
$agp = $result->aggregate_points;
$result_id = $result->result_id;
$fname = "";
foreach ($students as $student) {
// print_r($student);
$admNo1 = $student->admNo;
if ($admNo == $admNo1) {
// print_r($student);
$fname = $student->firstName;
$mname = $student->middleName;
$lname = $student->lastName;
//}
// }
//echo "<tr>";
echo "<td>";
echo $fname . " " . $mname . " " . $lname;
echo "</td>";
}
}
foreach ($subjectresults as $subresult) {
// print_r($result);
$score = "0";
$admNo3 = $subresult->admNo;
$subCode = $subresult->subCode;
$score = $subresult->score;
if ($admNo == $admNo3) {
if ($subCode == '232') {
$score = $score;
}
if ($subCode == '101') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '102') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '121') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '231') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '232') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '233') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '311') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '312') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '313') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '443') {
echo "<td>";
if (!$score) {
echo 0;
} else {
echo $score;
}
echo "</td>";
}
if ($subCode == '565') {
echo "<td>";
echo $score;
echo "</td>";
}
}
}
?>
<?php
if (isset($term)) {
$term = $term;
}
if (isset($form)) {
$form = $form;
}
if (isset($year)) {
$year = $year;
}
if (isset($examCategory)) {
$examCategory = $examCategory;
}
//if ($admNo == $admNo1) {
// print_r($student);
//}
// }
echo "<td>";
echo $total_marks;
echo "</td>";
echo "<td>";
echo $mean_grade;
echo "</td>";
echo "<td>";
echo $agp;
echo "</td>";
echo "<td>";
echo "</td>";
//}
}
?>
</table>
<?php
}
?>
</div>
the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121
I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;
<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
101=>20,
102=>30,
103=>30,
104=>45
),);
$result2 = array(
'studentId'=>2,
'score'=>array(
101=>34,
102=>54,
103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>
Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.
I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c
Here I want to fetch the field names from the table name which is stored in the variable $table. And want to make a table headers of the field names. What's wrong with this approach I tried:
<?php
$sql=mysql_query("show fields from $table");
if(mysql_num_rows($sql))
while($res = mysql_fetch_object($sql))
{
?>
<th><?php echo $res->field; ?></th>
<?
}
else
{
echo "No data to display";
}
?>
$printTHs = true;
while($res = mysql_fetch_assoc($sql))
{
if ($printTHs)
{
printTableHeader($res);
$printTHs= false;
}
echo "<tr>";
foreach($res as $val)
{
echo "<td>" . $val . "</td>";
}
echo "</tr>";
}
function printTableHeader($res)
{
echo "<tr>";
foreach($res as $col => $val)
{
echo "<th>" . $col . "</th>";
}
echo "</tr>";
}
The query has to be
$sql=mysql_query("show columns from $table");
Use mysql_fetch_assoc() and then get the keys using array_keys(). This will return an array with all the keys.
Use foreach() to get your table headers
Not the optimal solution, but would do it:
$results = mysql_query('SELECT * FROM ' . $table);
if (mysql_num_rows($results))
{
echo '<table>';
$first = TRUE;
while ($row = mysql_fetch_assoc($results))
{
if ($first = TRUE)
{
echo '<tr>';
foreach ($row as $k => $v)
echo '<th>' . $k . '</th>';
echo '</tr>';
$first = FALSE;
}
echo '<tr>';
foreach ($row as $column)
echo '<td>' . $column . '</td>';
echo '</tr>';
}
echo '</table>';
}