I've faced problem that I cant resolve in my simple code
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
echo $char.$num. "<br>";
if($char=="C"&&$num==3){
echo 'Found it<br>';
break;
}
}
echo '----------------------------<br>';
}
num.txt contains numbers like this
1
2
3
4
5 per line
char contains character Like A B C D per line
.
and as result
it dose not remove the C3 in text step after break
but i want it like this way without the founded C3
A1
B1
C1
D1
--------------
A2
B2
C2
D2
--------------
A3
B3
C3
Found it
D3
--------------
A4
B4
D4
--------------
A5
B5
D5
--------------
so C is no longer in the loop and continue if add B5 and so on
Please help i need this assignment
If you had regular array, you could remove "C" from it, but since you are getting data implicitly from file, this is the solution. Not the most elegant, but works:
// add flag for found value
$found = false;
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
// condition to show an entry
if(!$found || $char != "C") {
echo $char.$num. "<br>";
if($char == "C" && $num == 3) {
echo 'Found it<br>';
// you do not need 'break', remove it and set flag instead
$found = true;
}
}
}
echo '----------------------------<br>';
}
I think your code will be like this:
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
if($char=="C"&&$num==3){
echo 'Found it<br>';
break;
}
else{
echo $char.$num. "<br>";
}
}
echo '----------------------------<br>';
}
Hope this help.
<?php
$arr1 = ['A','B','C','D'];
$arr2 = [1,2,3,4,5];
for($i = 0; $i < count($arr1); $i++) {
for($j = 0; $j < count($arr2); $j++){
if($arr1[$i] == "C" && $arr2[$j] == 3){
echo "Found It<br/>";
break;
}
echo $arr1[$i]."".$arr2[$j]."<br/>";
}
echo "----------------------<br/>";
}
?>
Related
In Excel, column names are characters from A to Z, if there are more columns needed, it continues with AA, AB ...
I want to write a function, that converts integers to those excel column names.
0 .... A
25 ... Z
26 ... AA
702 ... AAA
.
.
.
The solution I came up with is working up to AZ, but I want it to work further.
function indexToXlxsColumn($index, $prefix="")
{
if($index < 26)
{
return $prefix.chr($index+65);
}else{
return indexToXlxsColumn($index % 26, "A");
}
}
How to adapt this function to work for every index without producing spaghetti code?
Here is the probleme
return indexToXlxsColumn($index % 26, "A");
You away set the next floor prefix to A , what happends when u have
input = 53 , the resultat should be "BB"
Code on paper
function indexToXlxsColumn($index, $prefix="")
{
if($index < 26) // 53 isnt less then 26
// second loop , 1 is less then 26
{
return $prefix.chr($index+65);
// $prefix = A
// chr($index+65) = B
// return "A"."B" ;
}else{
return indexToXlxsColumn($index % 26, "A");
// indexToXlxsColumn(53 % 26, "A") -> (1, "A")
}
}
---UPDATE---
Follow the ask here is the answer
function indexToXlxsColumn($index, $suffix ="")
{
if($index < 26){
return chr($index+65).$suffix ;
}
return indexToXlxsColumn(($index - $index%26)/26-1, chr($index%26+65).$suffix );
}
One of the implementations:
function indexToXlxsColumn($index)
{
$name = '';
while($index > 0) {
$mod = ($index - 1) % 26;
$name = chr(65 + $mod).$name;
$index = (int)(($index - $mod) / 26);
}
return $name;
}
// echo indexToXlxsColumn(26); // Z
echo indexToXlxsColumn(33); // AG
// echo indexToXlxsColumn(800); // ADT
I would do it a little differently. It bothered me to work with chr(). So I once stored the alphabet in a string and iterated over it until the index was successfully resolved.
max "zz"
<?php
function getIn($i) {
$str = 'abcdefghijklmnopqrstuvwxyz';
$r = (int) floor($i / 26) ;
$c = $i % 26;
return ($r) < 1 ? $str[$c] : $str[$r-1] . $str[$c];
}
echo getIn(52); // output: "ba"
Update with max "zzz"
function getIn($i) {
$str = 'abcdefghijklmnopqrstuvwxyz';
$r = (int) floor($i / 26);
$rr = $r >= 27 ? $r - 27 : null;
$c = $i % 26;
if ( $rr !== null) {
return $str[$c] : $str[$r-1] . $str[$c];
}
return ($r) < 1 ? $str[$c] : $str[$r-1] . $str[$c];
}
echo getIn(800); // ddu
echo getIn(1377); // zzz
PLACEHOLDER parts of the code return squiffy values! Needs review before using in the wild!!
In case anyone wants a utility function that does this, here's a python implementation:
n2AA performs the calculation from numeric to "AA" format.
def n2AA(n):
n=n-1
alphabet=["_"] + [chr(c+65) for c in range(0,26)]
w=len(alphabet)-1
accum=[]
for e in range(5,-1,-1):
expon=w**e
s=(n//expon)
r=n%expon
n=n-(s*expon)
if e>0:
accum.append(alphabet[s])
else:
accum.append(alphabet[s+1])
return "".join([a for a in accum if a != "_"])
And AA2n performs the inverse function, starting with "AA" format and returning the column number (starting at 1).
def AA2n(AA):
alphabet=["_"] + [chr(c+65) for c in range(0,26)]
w=len(alphabet)-1
accum=[]
for c in range(0,len(AA)):
expon=w**(len(AA)-1-c)
accum.append((alphabet.index(AA[c]))*expon)
return sum(accum)
i want every possible byte sequence between range(x,y), like range(0,0xFFFF) would give me 00...FF.0000.0001...FFFF ~
this works, but i suspect there's a much easier way to do it which escapes me, any suggestions?
<?php
function incbinstring(string $str): string
{
/*
if(strlen($str)===0){
return "\x00";
}
*/
$scanCurrent = strlen($str) - 1;
while ($scanCurrent >= 0) {
if ($str[$scanCurrent] !== "\xFF") {
break;
}
--$scanCurrent;
}
if ($scanCurrent < 0) {
// they're all \xFF... add new byte and all zeroes
return str_repeat("\x00", strlen($str) + 1);
}
if ($scanCurrent !== strlen($str) - 1) {
// preceeded by a bunch of \xFF\xFF\xFF , zero them out
// ps, this can be optimized to a substr()+str_repeat()
for ($i = strlen($str) - 1; $i > $scanCurrent; --$i) {
$str[$i] = "\x00";
}
}
// increment the first non-\xFF with 1
$str[$scanCurrent] = chr(ord($str[$scanCurrent]) + 1);
return $str;
}
$str = "\x00";
for ($i = 0; $i < 0xFFFF + 600; ++$i) {
echo bin2hex($str),"\n";
$str = incbinstring($str);
}
outputs, in part
00
01
02
03
04
05
06
07
(...)
f3
f4
f5
f6
f7
f8
f9
fa
fb
fc
fd
fe
ff
0000
0001
0002
0003
0004
(...)
fffb
fffc
fffd
fffe
ffff
000000
000001
000002
000003
000004
000005
000006
000007
<?php
function counterHex ($fromHex, $toHex ) {
$fromDec = hexdec($fromHex);
$toHex = hexdec($toHex);
for ($i = $fromHex; $i< $toHex; $i++){
echo dechex($i) .", ";
}
}
counterHex(1,'ff');
see: http://sandbox.onlinephpfunctions.com/code/d70d2766297c32039e01dd868e791b476bc92a87
As mentioned before, I am a beginner when it comes to PHP coding. I have made a text file which has my students information, as per below - this includes their name, marks for subjects, e-mail address and date of registration:
Ann Thompson : 50,90,82,64,75 : ann#amuniversity.com: 2016-02-01
Jeremiah Hanson: 80, 75, 88 : jeremiah#amuniversity.com: 2016-03-02
Billy Jones: 89, 72, 46, 54 : billy#amuniversity.com: 2016-04-12
and I have also made a form which inputs a field which takes in the filename which the user wishes to read from and a field for the filename which the user wishes to write to.
Where do I start to get the output below?
Summary for students:
Ann Thompson enrolled in 2016-02-01, has an average of 48, with a symbol F.
Jeremiah Hanson enrolled in ....
Billy Jones enrolled in ....
It should also show the date at the bottom when it was last modified.
Please kindly help
Please check the following code. I commented on the code wherever I thought needed. Let me know if you don't understand something from it. And obviously, you need to update the checkGrade function as per your grade system, but you get the idea :)
<?php
// Read the input file line by line into "input" array
$filename = "input.txt";
$input = explode("\n", file_get_contents($filename));
// Go through each line
for($i = 0; $i < sizeOf($input); $i++){
// split line by :
$line = explode(":", $input[$i]);
// Initialize variables based on position
$name = $line[0];
$numbers = $line[1];
$email = $line[2];
$registration = $line[3];
// Calculate average
// Remove any spaces first from the numbers
$numbers = str_replace(' ', '', $numbers);
// Split the numbers by ,
$numbersArray = explode(",", $numbers);
$sum = 0;
// Calculate sum
for($j = 0; $j < sizeOf($numbersArray); $j++){
$num = (int)$numbersArray[$j];
$sum += $num;
}
$avg = $sum/sizeof($numbersArray);
// Print it
echo "$name enrolled in $registration, has an average of $avg, with a symbol ". checkGrade($avg) ."<br>";
}
// Last modified date of the input file
echo "<br><br><br>";
if (file_exists($filename)) {
echo "This file was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
function checkGrade($num){
if ($num < 65){
$grad = 'F';
}
else if ($num<= 66 && $num >=65){
$grad = 'D';
}
else if ($num <= 69 && $num >=67){
$grad = 'D+';
}
else if ($num <= 73 && $num >=70){
$grad = 'C-';
}
else if ($num <= 76 && $num >=74){
$grad = 'C';
}
else if ($num<= 79 && $num >=77 ){
$grad = 'C+';
}
else if ($num <= 83 && $num >=80){
$grad = 'B-';
}
else if ($num <= 86 && $num >=84){
$grad = 'B';
}
else if ($num <= 89 && $num >=87){
$grad = 'B+';
}
else if ($num <= 93 && $num >=90){
$grad = 'A-';
}
else if ($num <= 96 && $num >=94){
$grad = 'A';
}
else if ($num >= 97){
$grad = 'A+';
}
return $grad;
}
?>
Here is the output:
Ann Thompson enrolled in 2016-02-01, has an average of 72.2, with a symbol C-
Jeremiah Hanson enrolled in 2016-03-02, has an average of 81, with a symbol B-
Billy Jones enrolled in 2016-04-12, has an average of 65.25, with a symbol D
This file was last modified: May 03 2017 13:12:13.
I would like to increment a letter by 2. This way, A will become C, Z will become AB.
It works well when I increment by 1 with $letter++ but I can't find a way to do it.
Thanks for any help
You could use a function like this one to increment variable as many time as wished :
function increment($val, $increment = 2)
{
for ($i = 1; $i <= $increment; $i++) {
$val++;
}
return $val;
}
Usage :
$var = increment('a', 2);
var_dump($var);
You should use the functions chr(); and ord(); to get the ASCII value and return it. Example code:
$letter = 'A';
//catch Y to add a letter + reset to A
if(strtoupper(substr($letter,-1)) == 'Y'{
$letter = 'A'.substr($letter,0,strlen($letter)-1).'A';
}
//catch Z to add a letter + reset to B
else if(strtoupper(substr($letter,-1)) == 'Z'){
$letter = 'A'.substr($letter,0,strlen($letter)-1).'B';
}
//nothing to catch here
else{
$letter = substr($letter,0,strlen($letter)-1).(chr(ord(substr($letter,-1))+2));
}
// $letter = chr(ord($letter) + 2); //returns C
You should build in a function to make the jump from Z to AB and from Y to AA.
for ($i=A;$i<Z;$i++){
echo ++$i." ";
}
Try this.
I am getting $final value from database and displaying in a table according to user. Now I have a value i.e., this is I was showing in the frontend
Userid Total Letter
36 45
5 67
78 90
42 82
12 57
Back End Code
$sql = mysql_query('SELECT userid, total FROM history');
echo '<table><th>Userid</th>
<th>Total</th>
<th>Grade</th>
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td>
<td>'.$row['$total'].'</td>
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
$letterp= $row['letter'];
switch($row['$total'])
{
case $row['$total'] == $score;
echo '<td>'.$letterp.'</td>';
break;
case $row['$total'] >= $score;
echo '<td>'.$letterp.'</td>';
break;
}
</tr>
}
I have a letter table
ID SCORE letter
1 100 A+
2 90 A-
3 80 B+
4 73 B-
5 65 C
6 55 D
7 45 E
8 0 F
Switch case is a sample I wrote here. I need to give condition like if userid scores 45 then I need to show letter E and if userid scores 67 then I need to show B-(b'coz the scorefield defines 65 to 54 is B-) . This is the exact requirement.
Something like this should do:
SELECT
CASE score
WHEN = 100 THEN 'A+'
WHEN >= 90 and < 100 THEN 'A-'
WHEN >= 80 and < 89 THEN 'B+'
WHEN >= 73 and < 80 THEN 'B-'
// and so one...
ELSE 'F'
END as myGrade,
studentID
from
table1
Edit: Sorry, I thought you were trying to do it in the database (where I would have probably done it if I just wanted the grade, not the score. Here is the PHP code anyhow:
$myScore=90; // for example
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
$myGrade=getMyGrade($myScore);
echo $myGrade; // output: A-
Based on the function I wrote:
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td><td>'.$row['$total'].'</td>';
// In your code:
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
echo '<td>'.getMyGrade($row['letter']).'</td>';
}
echo'</tr>';
}