I want to validate if a current row in every field has data on it and if it has not then display no data. Please help me. Also it is advisable to use echo in displaying the table data or should i stick to the html. Thanks in advance! I've been searching the net and was not able to find some answers. lol
$ct_list = mysqli_query($conn,"select * from ct LEFT JOIN station on ct.station_id=station.station_id LEFT JOIN dilg_emp on dilg_emp.station_id=station.station_id where dilg_emp.dilg_emp_id= $session_id");
if (mysqli_num_rows($ct_list) > 0){
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
$ct_FName = $ct_row_list['ct_FName'];
$ct_MName = $ct_row_list['ct_MName'];
$ct_LName = $ct_row_list['ct_LName'];
$ct_SName = $ct_row_list['ct_SName'];
$ct_address = $ct_row_list['ct_address'];
$ct_birthday = $ct_row_list['ct_birthday'];
$ct_age = $ct_row_list['ct_age'];
$ct_gender = $ct_row_list['ct_gender'];
$ct_level_educ = $ct_row_list['ct_level_educ'];
$ct_course = $ct_row_list['ct_course'];
$ct_school = $ct_row_list['ct_school'];
$ct_prev_covid = $ct_row_list['ct_prev_covid'];
$ct_training_date = $ct_row_list['ct_training_date'];
$ct_deployment_date = $ct_row_list['ct_deployment_date'];
$ct_prev_emp = $ct_row_list['ct_prev_emp'];
echo "<tr>
<td>".
$x++."
</td>
<td>
$ct_FName $ct_MName $ct_LName $ct_SName
</td>
<td>
$ct_address
</td>
<td>
$ct_birthday
</td>
<td>
$ct_age
</td>
<td>
$ct_gender
</td>
<td>
$ct_level_educ
</td>
<td>
$ct_course
</td>
<td>
$ct_school
</td>
<td>
$ct_prev_covid
</td>
<td>
$ct_training_date
</td>
<td>
$ct_deployment_date
</td>
<td>
$ct_prev_emp
</td>
</tr>";
}
}else{
echo "<tr><td><b>Nothing to display</b></td></tr>";
}
You can loop over the row array, replacing empty values with No data.
$optional_fields = ["ct_SName"];
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
foreach ($ct_row_list as $name => &$value) {
if (in_array($name, $optional_fields)) {
continue;
} elseif (empty($value)) {
$value = "No data";
}
}
// rest of your code goes here
It's hard to tell exactly what you mean, if a row has any empty columns then skip that row?
If you select 15 columns in the query or if you know that SELECT * will return 15 columns, then filter out the empty ones and see if there are 15:
if (mysqli_num_rows($ct_list) > 0) {
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
if (count(array_filter($ct_row)) == 15) {
echo "<tr>
<td>$x++</td>
<td>$ct_FName $ct_MName $ct_LName $ct_SName</td>"; //etc...
}
}
}
You could also check and skip:
if (count(array_filter($ct_row)) < 15) {
continue;
}
//echo your HTML
Related
I am working on a grading system but whenever I input a grade, it saves a blank instead of the submitted value.
The form is on the table itself and not on a separate page. I know doing it on a separate page will solve my problem but I want to do it on the table itself.
Like this
Here is the code for the table
<tr>
<th>Student Number</th>
<th>Course</th>
<th>Email</th>
<th>Name</th>
<th>Grade</th>
<th>Remark</th>
<th>Add/Edit</th>
<?php
$table = "Users";
$fetch = $database->getReference($table)->getValue();
if($fetch > 0)
{
foreach($fetch as $key => $row)
{
?>
<tr>
<td> <?= $row['Student_number']; ?> </td>
<td> <?= $row['Course']; ?> </td>
<td> <?= $row['Email']; ?> </td>
<td> <?= $row['Name']; ?> </td>
<?php
if(!$row['grade']
{
?>
<td><input type = "text" name = "grade" id = "grade"></input></td>
<td><button type = "submit" name = "update" value = "<?=$key?>"</button></td>
<td>N/A</td>
}
else
{
?>
<td><?=$row[grade];?></td>
<?php
}
}
}
?>
And here is the code for the input
if(isset($_POST[update]))
{
$key = $_POST[update];
$grade = $_POST[grade];
$postGrade = ['grade' => $grade,];
$table = 'Users;
$updateResult = $database->getReference($table)->update($postGrade);
}
I need help finding what's wrong with my code that makes the input a blank instead of the intended submitted value.
I have a table of users and a table of scores in golf tournaments for those users. When a user plays a tournament he records a score in the results table using a form. I want to display a table of results showing the full list of users and the scores in the tournaments. There are eight columns of scores in the table - one for each course played. I am struggling with the php code to display the resulting scores. Where a player has played, his score appears correctly but if the following player in the table has not played his score is shown as the score for the player above him in the table. This continues down the list until a genuine score is encountered. I have tried to find the answer to this without success. Here is my code for just a single course. If I can overcome the problem for this I am sure the code will work for the other courses.
$query = "SELECT * FROM emg_users ORDER BY user_login";
$results=mysql_query($query);
$results_array = array();
while ($row = mysql_fetch_array($results)) {
$results_array[$row['ID']] = $row;
}
foreach ($results_array as $id => $record) {
$player = $record['user_login'];
?>
<tr>
<td class="padded_c" nowrap="nowrap"><?php echo $player;?></td> <!-- Player name -->
<?php
$query = "SELECT * FROM 0_tournament_test WHERE player = '".$player."' AND course = 'St_Andrews'";
$results=mysql_query($query);
$results_array = array();
while ($row = mysql_fetch_array($results)) {
$results_array[$row['id']] = $row;
}
foreach ($results_array as $id => $record) {
$pplayer = $record['player'];
if ($pplayer = $player) {
$sta = $record['score'];
}
else {
unset($sta);
}
$id++;
}
?>
<td class="padded_c" nowrap="nowrap"></td>
<td nowrap="nowrap"><?php echo $sta;?></td>
<td nowrap="nowrap"><?php echo $rsg;?></td>
<td nowrap="nowrap"><?php echo $bet;?></td>
<td nowrap="nowrap"><?php echo $oak;?></td>
<td nowrap="nowrap"><?php echo $kia;?></td>
<td nowrap="nowrap"><?php echo $cng;?></td>
<td nowrap="nowrap"><?php echo $mer;?></td>
<td nowrap="nowrap"><?php echo $oly;?></td>
<td nowrap="nowrap"><?php echo $allshots;?></td>
<td nowrap="nowrap"><?php echo $sss;?></td>
<td nowrap="nowrap"><?php echo '';?></td>
</tr>
<?php
$id++;
}
?>
</table>
I think you forgot some login...
Little Change in your code :--
if ($pplayer == $player) {
$sta = $record['score'];
}
else
{
unset($sta);
}
Add this condition to your code...
I have a table which echos back some SQL counts for each user using foreach. I want to have a total which will add up all the counts foreach. Any ideas would be appreciated.
$students = get_course_students($course->id, 'lastname');
$toggle=0;
if (!empty($students)) {
foreach ($students as $student) {
$user=get_record('user','id',$student->id);
$atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
$personalcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 1 AND user = $user->id AND course = $course->id");
$reportscountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_reports WHERE user = $user->id AND course = $course->id");
echo '<tr class="r'.$toggle.'">
<td class="cell c'.$toggle.'" valign="top" nowrap="true" align="left">'.fullname($user).'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$atriskcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$personalcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$reportscountrecords.'</td>
</tr>';
if($toggle==0)$toggle=1;
else $toggle=0;
}
}
echo'<tr>
<td><strong>Totals</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td></td>
</tr>';
echo'</table>';
If I really understand your question, it's quite easy ; just declare a variable per counter before your foreach loop and increase it with count value.
For example :
$atriskcountrecordsGlobal = 0;
if (!empty($students)) {
foreach ($students as $student) {
...
$atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
$atriskcountrecordsGlobal += $atriskcountrecords;
...
}
What I would like to do is number each row in my table. I can't manually number the table myself, as all of the info in it is retrieved from a database. Is this possible with jQuery or PHP? Here's a screen shot of the table:
I tried searching for this, and did not find anything that helped me.
Here is the PHP / HTML that is displaying the table:
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Created on</th>
<th style="width:65px;">Status</th>
<th>Actions</th>
</tr>
<?php
[...]
//Display the results
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
As you can see, it's displayed with a while loop.
If anyone knows a way to number each line in my table with jQuery or PHP, please help me :)
$trCounter=0;
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td>$trCounter++</td>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
or you could always use the :eq api in your jquery selector or its equivalent to work with the index but like I asked it depends on what you want to do with the index.
I would go with PHP solution listed by Atul Gupta.
To add more - you also can to start iteration based on which page you are.
<?php
$i = ($page-1) * $itemsPerPage;
while(....)
{
echo $i;
$i++;
}
?>
if you are on the second page of your list would get something like 11,12,13,14,....
jQuery:
$(function () {
var i = 0;
$('table thead tr').prepend('<th>#</th>');
$('table tbody tr').each(function () {
i += 1;
$(this).prepend('<td>' + i + '</td>');
});
});
PHP
<tr>
<th>Sr. No</th> // Add header for counter
<th>Name</th>
...
</tr>
...
$i=1; // add counter -----------corrected-------------
while($info = mysql_fetch_assoc($result)){
//data
...
echo "
<tr>
<td>$i</td> // include counter to table
<td style=\"min-width: 87px;\">$name</td>
...
</tr>
";
$i++; // increment counter
}
Set an auto increment property in the SQL table, which can be used as an index, and will increase automatically when a new entry is added?
I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png
But i cant get it to be like i want, instead it prints the rank on every row:
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = "<tr><td> </td>\n";
}
echo " <td>$g->name</td></tr>\n";
}
What I want:
<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td> </td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td> </td>
<td>Zazi The Beast</td>
</tr>
<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td> </td>
<td>Saint Sinner</td>
</tr>
<tr>
<td> </td>
<td>Midvalley the Hornfreak</td>
</tr>.......................
What I get:
<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>
<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner
</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.
Try changing your if statement to something like this:
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = $g->rankname;
} else {
echo "<tr><td> </td>\n";
}
It prints the rank name if it's a new rank name, else it prints empty space.
The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages:
- You don't need to depend on the order of the results returned
- You don't need to maintain dodgy logic (like 'old_rank')
- You can display them more nicely (with a rowspan for repeated ranks
I believe the total code is more compact too.
// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
if ( !in_array($g->rankname, $ranks) ) {
$ranks[htmlentities($g->rankname)] = array();
}
$ranks[$g->rankname][] = htmlentities($g->name);
}
// do other program logic here
// end of program
?>
<!-- display the page -->
<table>
<tr>
<th>Rank</th><th>Users</th>
</tr>
<?php foreach($ranks as $rankName => $userList): ?>
<tr>
<td rowspan="<?php echo (string)sizeof($userList); ?>">
<?php echo $rankName; ?>
</td>
<td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
</tr>
<?php endforeach; ?>
</table>
I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
echo '<tr>' . "\n";
echo '<td>';
if ($g->rankname != $old_rank) {
$old_rank = $g->rankname;
echo $old_rank;
} else {
echo ' ';
}
echo '</td>';
echo '<td>' . $g->name . '</td>' . "\n";
echo '</tr>' . "\n";
}