foreach loop breaks before reaching the limit - php

I've been trying to list 9 tables for each student using the following code -
Up to 8 records are getting successfully printed. But for the next value of $s, the next student gets listed.
Where am I getting it wrong?
$query="SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s=1;
while($row=mysqli_fetch_array($Result)) {
foreach($row as $field) {
if($s==10){
$s=1; break;
} else if($s==1){
$sub="English (WS VOL-1)";
} else if($s==2){
$sub="English (WS VOL-2)";
} else if($s==3){
$sub="English (WS VOL-3)";
} else if($s==4){
$sub="Maths (WS VOL-1)";
} else if($s==5){
$sub="Maths (WS VOL-2)";
} else if($s==6){
$sub="Maths (WS VOL-3)";
} else if($s==7){
$sub="Science (WS VOL-1)";
} else if($s==8){
$sub="Science (WS VOL-2)";
} else if($s==9){
$sub="Science (WS VOL-3)";
}
echo $table;
$s++;
}
}

try this : $i++ should be after end inner loop.
<?php
$query = "SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s = 1;
while ($row = mysqli_fetch_array($Result)) {
foreach ($row as $field) {
if ($s == 10) {
$s = 1;
break;
}
if ($s == 1) {
$sub = "English (WS VOL-1)";
} elseif ($s == 2) {
$sub = "English (WS VOL-2)";
} elseif ($s == 3) {
$sub = "English (WS VOL-3)";
} elseif ($s == 4) {
$sub = "Maths (WS VOL-1)";
} elseif ($s == 5) {
$sub = "Maths (WS VOL-2)";
} elseif ($s == 6) {
$sub = "Maths (WS VOL-3)";
} elseif ($s == 7) {
$sub = "Science (WS VOL-1)";
} elseif ($s == 8) {
$sub = "Science (WS VOL-2)";
} elseif ($s == 9) {
$sub = "Science (WS VOL-3)";
}
echo $table;
}
$s++; //this is only change
}

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

PHP Check if 2 variables are the same, like a test

I need help to check if variables (from a database) are the same, like a test.
First I was using just "==", but I saw this: http://php.net/manual/en/language.operators.comparison.php , and now I'm using "===". But it still doesn't work.
My code:
$uids = array();
$trues = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($uids, $row['UID']);
} while ($row1 = mysqli_fetch_assoc($result1)) {
array_push($uids, $row1['UID']);
} while ($row2 = mysqli_fetch_assoc($result2)) {
array_push($uids, $row2['UID']);
}
for ($i = 0; $i < count($uids); $i++) {
$r = 0;
if ($row['question1'] === $row['uAnswer1']) {
$r++;
} else {
$r = $r;
} if ($row['question2'] === $row['uAnswer2']) {
$r++;
} else {
$r = $r;
} if ($row['question3'] === $row['uAnswer3']) {
$r++;
} else {
$r = $r;
} if ($row['question4'] === $row['uAnswer4']) {
$r++;
} else {
$r = $r;
} if ($row['question5'] === $row['uAnswer5']) {
$r++;
} else {
$r = $r;
} if ($row['question6'] === $row['uAnswer6']) {
$r++;
} else {
$r = $r;
} if ($row['question7'] === $row['uAnswer7']) {
$r++;
} else {
$r = $r;
} if ($row['question8'] === $row['uAnswer8']) {
$r++;
} else {
$r = $r;
} if ($row['question9'] === $row['uAnswer9']) {
$r++;
} else {
$r = $r;
} if ($row['question10'] === $row['uAnswer10']) {
$r++;
} else {
$r = $r;
}
array_push($trues, $r);
echo $uids[$i] . " [" . $r . "]<br>";
}
print_r($trues);
}
What the result is: https://hastebin.com/uxitoyoyib.php
So it actually says everything is right, but I know that it isn't. Can you help me with this?
Thanks!
Compare variables like
if ( strtolower(trim($row['question5'])) === strtolower(trim($row['uAnswer5'])) )
Remove spaces if added with strings, convert the string in lowercase to the exact match of both strings.
use
if ($row['question4'] == $row['uAnswer4']) {
//do something
}
and try to print your 2 variable.
You can use the strcmp() or strcasecmp() functions or the === operator.

Bold the last row in table

How can I make the last row Bold in fpdf?
here is my code
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
Updated Code : Added conditions for the rows until reach the last row.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}
The sample code above creates the row and column my target is to make the last row bold
Simply, you can count() rows and then check if current processed row is the last one.
$totalRows = count($data);
$currentRow = 1;
foreach ($data as $row) {
// (...)
if ($currentRow === $totalRows) {
// this is the last row
}
$currentRow++;
}
Here is the working code.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}

I can't show multiple days of absent

my client ask me to show the in and out of their employees from biometrics..i already did it but it only shows data for 1 day. Can somebody help me..If you can see I had a lot of codes there and it doesn't satisfy my client.I'am working on it for almost a month
this is my code..I'm a beginner.
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_REQUEST['startdate']) && isset($_REQUEST['enddate']))
{
$startdate = $_REQUEST['startdate'];
$enddate = $_REQUEST['enddate'];
$shiftdate = $_REQUEST['shiftdate'];
$category= $_REQUEST['category'];
$counterless = 0;
$ncounter = 0;
$count =0 ;
$loop=0;
$holder=0;
$checker=0;
$wala = 0;
$present = 0;
/*$_SESSION['nightname'] = array();
$_SESSION['nighttime'] = array();
$_SESSION['testname'] = array();
$_SESSION['testtime'] = array();*/
$con = mysqli_connect("localhost","root","","testingdb");
$connect = mysqli_connect("localhost","root","","inoutchecking");
mysqli_query($con,"delete from forprint");
mysqli_query($con,"delete from absentforprint");
$result = mysqli_query($con,"select * from wawart where Column_2 = '$startdate' order by Column_2 asc");
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
$time = $row['Column_3'];
$times = (int)($time);
$inout = $row['Column_4'];
$dates = $row['Column_2'];
if(($times <= 14 and $inout == '1') or ($times >=17 and $inout == '4'))
{
$_SESSION['testname'][$counterless] = $row['Column_1'];
$_SESSION['testtime'][$counterless] = $time;
$counterless++;
}
if(($times <= 20 and $times >= 15) and $inout == '1')
{
$_SESSION['nightname'][$ncounter] = $row['Column_1'];
$_SESSION['nighttime'][$ncounter] = $time;
$ncounter++;
}
}
$wala=1;
}
else
{
$wala=0;
}
if($category == 'dayshift')
{
$shift = 'Day Shift';
}
else if($category == 'nightshift')
{
$shift = 'Night Shift';
}
else
{
$shift = 'Night Shift and Day Shift';
}
echo "<label class='cat'>".$shift."</label>";
echo "<label class='absent'>Absentees ".$startdate."</label>";
if($category == 'dayshift')
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('Employee Number','Lastname','Firstname','Department','Section','Time in','Time out','Shift')");
echo "<div class='dayshift'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th><th>DATE</th><th>IN</th><th>OUT</th></tr>";
for($x = 0; $x<$counterless;$x++)
{
$dempid = $_SESSION['testname'][$x];
$day = mysqli_query($connect,"select * from nightshiftlist where empid = '$dempid' and shift ='dayshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$count++;
if($loop == '1')
{
echo "<td>NO OUT</td></tr>";
$loop=0;
}
$empid = $empinfo['empid'];
$empshift = $empinfo['shift'];
$name = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
$checking = mysqli_query($con,"select * from wawart where Column_1='$empid' and (Column_2 between '$startdate' and '$enddate') order by Column_2 asc");
if($holder == $empinfo['empid'])
{
break;
}
echo"<tr><td>".$empinfo['empid']."</td><td>".$empinfo['lastname']."</td><td>".$empinfo['firstname']."</td><td>".$empinfo['department']."</td><td>".$empinfo['section']."</td>";
if(mysqli_num_rows($checking)>0)
{
while($row=mysqli_fetch_array($checking))
{
$time = $row['Column_3'];
$times = (int)$time;
if($holder == $empinfo['empid'])
{
break;
}
else
{
$inout = $row['Column_4'];
$time = $row['Column_3'];
if($loop ==0)
{
$date = $row['Column_2'];
echo"<td>".$row['Column_2']."</td>";
}
if($loop ==0 )
{
if($inout == '1')
{
$timein = $time;
echo "<td>".$time."</td>";
$loop = 1;
}
if($inout == '4')
{
$timein = 'NO IN';
$timeout = $time;
echo "<td><B>NO IN</td><td>".$time."</td></tr>";
$holder = $empinfo['empid'];
$loop = 0;
}
}
else
{
if($loop =='1' && $inout == '4')
{
$timeout = $time;
echo "<td>".$time."</td></tr>";
}
if($loop =='1' && $inout == '1')
{
$timeout = 'NO OUT';
echo "<td><B>NO OUT</td></tr>";
}
$loop = 0;
$counter = 0;
$holder = $empinfo['empid'];
$checker = 2;
if($checker == 2)
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('$empid','$name','$fname','$dept','$section','$timein','$timeout','$empshift')");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
}
}
}
}
ini_set('max_execution_time', 999999);
}
}
echo"</table></div>";
/*absentees*/
if($wala == 1)
{
$count = 0;
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('Employee Number','Lastname','Firstname','Department','Section')");
echo "<div class='absentees'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th></tr>";
$day = mysqli_query($connect,"select * from nightshiftlist where shift ='dayshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$id = $empinfo['empid'];
$shift = $empinfo['shift'];
$lname = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$date = $empinfo['datefrom'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
for($x = 0; $x<$counterless;$x++)
{
$dempid = $_SESSION['testname'][$x];
if($id == $dempid)
{
$present = 1;
break;
}
else
{
$present = 0;
}
ini_set('max_execution_time', 999999);
}
if($present == 0)
{
echo "<tr><td>".$id."</td><td>".$lname."</td><td>".$fname."</td><td>".$dept."</td><td>".$section."</td></tr>";
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('$id','$lname','$fname','$dept','$section')");
ini_set('max_execution_time', 999999);
$count++;
}
}
echo"</table></div>";
echo"<label class='totalabs'>Total Number of Absentees ".$count."</label>";
}
}/*end of absentees*/
if($category == 'nightshift')
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('Employee Number','Lastname','Firstname','Department','Section','Time in','Time out','Shift')");
echo "<div class='dayshift'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DATE</th><th>DEPARTMENT</th><th>SECTION</th><th>IN</th><th>OUT</th></tr>";
for($x = 0; $x<$ncounter;$x++)
{
$dempid = $_SESSION['nightname'][$x];
$day = mysqli_query($connect,"select * from nightshiftlist where empid = '$dempid' and shift = 'nightshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$count++;
if($loop == '1')
{
echo "<td>NO OUT</td></tr>";
$loop=0;
}
$empid = $empinfo['empid'];
$empshift = $empinfo['shift'];
$name = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
$checking = mysqli_query($con,"select * from wawart where Column_1='$empid' and (Column_2 between '$startdate' and '$enddate') order by Column_2 asc");
if($holder == $empinfo['empid'])
{
break;
}
echo"<tr><td>".$empinfo['empid']."</td><td>".$empinfo['lastname']."</td><td>".$empinfo['firstname']."</td><td>".$empinfo['department']."</td><td>".$empinfo['section']."</td>";
if(mysqli_num_rows($checking)>0)
{
while($row=mysqli_fetch_array($checking))
{
$time = $row['Column_3'];
$times = (int)$time;
if($holder == $empinfo['empid'])
{
break;
}
else
{
$inout = $row['Column_4'];
$time = $row['Column_3'];
$dates = $row['Column_2'];
if($loop ==0 && $inout == '1'&& $dates == $startdate)
{
$date = $row['Column_2'];
echo"<td>".$row['Column_2']."</td>";
}
if($loop ==0 && $dates == $startdate)
{
if($inout == '1')
{
$timein = $time;
echo "<td>".$time."</td>";
$loop = 1;
}
if($inout == '4')
{
$timein = 'NO IN';
$loop = 0;
}
}
else
{
if($loop =='1' && $inout == '4')
{
$timeout = $time;
echo "<td>".$time."</td></tr>";
}
if($loop =='1' && $inout == '1')
{
$timeout = 'NO OUT';
echo "<td><B>NO OUT</td></tr>";
}
$loop = 0;
$counter = 0;
$holder = $empinfo['empid'];
$checker = 2;
if($checker == 2)
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('$dempid','$name','$fname','$dept','$section','$timein','$timeout','$empshift')");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
}
}
}
}
ini_set('max_execution_time', 999999);
}
}
echo"</table></div>";
/*absentees*/
if($wala == 1)
{
$count = 0;
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('Employee Number','Lastname','Firstname','Department','Section')");
echo "<div class='absentees'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th></tr>";
$day = mysqli_query($connect,"select * from nightshiftlist where shift ='nightshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$id = $empinfo['empid'];
$shift = $empinfo['shift'];
$lname = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
for($x = 0; $x<$ncounter;$x++)
{
$dempid = $_SESSION['nightname'][$x];
if($id == $dempid)
{
$present = 1;
break;
}
else
{
$present = 0;
}
ini_set('max_execution_time', 999999);
}
if($present == 0)
{
echo "<tr><td>".$id."</td><td>".$lname."</td><td>".$fname."</td><td>".$dept."</td><td>".$section."</td></tr>";
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('$id','$lname','$fname','$dept','$section')");
$count++;
}
ini_set('max_execution_time', 999999);
}
echo"</table></div>";
echo"<label class='totalabs'>Total Number of Absentees ".$count."</label>";
}
}/*end of absentees*/
}
?>
The first sql select query you are executing needs to be corrected.Your select sql query does not considering end date. Try the below query
$result = mysqli_query($con,"select * from wawart where Column_2 >= '$startdate' and Column_2 <= '$enddate' order by Column_2 asc");

how to sort SQL result in an existing sorting function

Hello I have this sorting function ine one template of CMS. I would like to add if the condition is the second one:
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
I want to add the following MySQL result to show also
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
which is used in this template for sorting:
include($basepath.'/templates/template.channel_item_title.php');
Any ideas how this can be integrated inside the first code function?
So here is the code of the main index template:
<?
session_start();
if(($_REQUEST[mode] == 'favorites' || $_REQUEST[mode] == 'my_uploads') && !$_SESSION[username]) {
header("Location: /login.php");
}
include('admin/db.php');
include($basepath.'/includes/inc.seo.php');
$cacheName = $_SERVER[REQUEST_URI];
$cacheResult = getCache($cacheName);
$cacheTotalPages = getCache("total_pages$cacheName");
if($cacheResult && $cacheTotalPages) {
$array = $cacheResult;
$total_pages = $cacheTotalPages;
}
else {
include($basepath.'/includes/inc.index_queries.php');
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
if($thispage != 'favorites' && $_GET[mode] != 'my_uploads') {
setCache($cacheName,$array,$overall_cache_time);
setCache("total_pages$cacheName",$total_pages,$overall_cache_time);
}
}
$thisfile = 'index';
$webpage="index";
if($isMobile) {
include($basepath.'/templates/mobile.overall_header.php');
}
else {
include($basepath.'/templates/template.overall_header.php');
}
if(empty($_GET[mode]) && !$_GET[page]) {
include($basepath.'/templates/template.home.php');
}
if($webpage=="index" && empty($_GET[mode]) && !$_GET[page])
{}
else
{
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
}
else {
if($isMobile) {
include($basepath.'/templates/mobile.content_item.php');
}
else
{
include($basepath.'/templates/template.content_item.php');
}
}
}
}
else {
echo "Sorry, no results were found.";
}
}
if($isMobile) {
include($basepath.'/templates/mobile.overall_footer.php');
}
else {
include($basepath.'/templates/template.overall_footer.php');
}
?>
I don't think I get your question, but we have to start somewhere...
<?php
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
Next block of code:
<?php
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
if(is_array(result)) {
foreach($result as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.channel_item_title.php');
}

Categories