I am just coding in PHP but not understand below-mentioned coding so please help for understanding coding simply
$new_array=array(100,101,61,1075);
foreach($new_array as $value){
if(!($value%2)){
continue;
}
}
You may read this condition as "is number $value can be divided by 2 without a remainder?"
foreach
this is a Array method which we can use to execute on each element of the array. So in your code
foreach($new_array as $value){
This will assign
$value[0] = 100
$value[1] = 101
$value[2] = 61
$value[3] = 1075
Then in php % mean modules. When this operator is used within two numbers it will output the remainder after deviding first number from second number.See below examples.
5%5 = 0
5%4 = 1
5%3 = 2
10%7 = 3
($value%2)
In here each assigned values from array will be divided from 2 and check the remainder.
$value[0] = 100 => 100%2 = 0
$value[1] = 101 => 101%2 = 1
$value[2] = 61 => 61%2 = 1
$value[3] = 1075 => 1075%2 = 1
(!($value%2)) After using NOT operator(!). This means that ($value%2) should be false.It means that ($value%2) should output a value equal to 0. You can check and understand this code as below.
<?php
$new_array=array(100,101,61,1075);
foreach($new_array as $value){
if(!($value%2)){
echo($value." " );
}
}
?>
Output =100
?php
$new_array=array(100,101,61,1075);
foreach($new_array as $value){
if(($value%2)){
echo($value." , " );
}
}
?>
Output = 101 , 61 , 1075 ,
So hope you can have a idea with this.
For more just follow this links.click here to know about modules operator via official document
Thanks
Related
I am still a novice at PHP scripting.
I have an Array
$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);
From the associative array, the key are the student's exam no and the values are the student's scores. Then I used the 2 inbult PHP functions array_keys and array_values to separate the exam nos from the scores.
$exam_nos=(array_keys($students));
$marks=(array_values($students));
Then I passed the $marks array through the code below:
$i=0;
$occurrences = array_count_values($marks);
$marks = array_unique($marks);
echo '<table border="1">';
foreach($marks as $grade) {
if($grade == end($marks))$i += $occurrences[$grade]-1;
echo str_repeat('<tr><td>'.$grade.': '.($i+1).'</td></tr>',$occurrences[$grade]);
$i += $occurrences[$grade];
}
echo '</table><br />';
output:
94: 1
94: 1
94: 1
91: 4
89: 5
83: 6
65: 7
41: 8
38: 9
37: 11
37: 11
And this is closer to what I want; to rank the scores such that if a tie is encountered, 1 or more positions are skipped, occurs at the end the position the items at the end are assigned a position equivalent toi the total number of ranked items. However, it would be much helpful if this could be done without separating the Array into 2 ...
Questions:
(1) I am pulling my hair how, from the $student array I could have something like:
Exam No Score Position
1201 94 1
1210 94 1
1203 94 1
1200 91 4
1205 89 5
1209 83 6
1206 65 7
1202 41 8
1207 38 9
1204 37 11
1208 37 11
(2) I would like to be able to pick any student by exam no and be able to echo or print out her position e.g
the student 1207 is number 9.
I think I need to capture the postions in a variable, but how do I capture them? Well I don't know!
Could the experts help me here with a better way to achieve my 2 goals (please see questions 1 and 2)? I will try any suggestion that will help me disolve the 'metal blockage' I have hit.
If you're pulling out the students from a database (mentioned in the comments), you could retrieve them with the desired format directly using SQL.
However, I'm going to assume that that's not an option. You could do as follows:
$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);
arsort($students);// It orders high to low by value. You could avoid this with a simple ORDER BY clause in SQL.
$result = array();
$pos = $real_pos = 0;
$prev_score = -1;
foreach ($students as $exam_n => $score) {
$real_pos += 1;// Natural position.
$pos = ($prev_score != $score) ? $real_pos : $pos;// If I have same score, I have same position in ranking, otherwise, natural position.
$result[$exam_n] = array(
"score" => $score,
"position" => $pos,
"exam_no" => $exam_n
);
$prev_score = $score;// update last score.
}
$desired = 1207;
print_r($result);
echo "Student " . $result[$desired]["exam_no"] . ", position: " . $result[$desired]["position"] . " and score: ". $result[$desired]["score"];
Hope it helps you.
I would use a custom object to process the students individually and store them in an array.
$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);
arsort($students); // Sort the array so the higher scores are on top.
$newStudents = array();
$pos = 0;
$count = 0;
$holder = -1; // Assuming no negative scores.
foreach($students as $k=>$v){
$count++; // increment real counter
if($v < $holder || $holder == -1){
$holder = $v;
$pos = $count;
}
$newStudents[] = makeStudent($pos, $v, $k);
// If you want the exam # as the array key.
// $newStudents[$k] = $student;
}
$newStudents = fixLast($newStudents);
// outputs
print_r($newStudents);
foreach($newStudents as $v){
echo "position : " . $v->position . "<br>";
echo "score : " . $v->score . "<br>";
echo "exam : " . $v->exam . "<br>";
}
function makeStudent($pos, $score,$examNo){
$student = new stdClass(); // You could make a custom, but keeping it simple
$student->position = $pos;
$student->score = $score;
$student->exam = $examNo;
return $student;
}
function fixLast($students){
$length = count($students) -1;
$count = 0;
$i = $length;
while($students[$i]->position == $students[--$i]->position){
$count++;
}
for($i = 0; $i <= $count; $i++){
$students[$length - $i]->position = $students[$length - $i]->position + $count;
}
return $students;
}
I'm hoping someone out there can see what I'm missing in using PHP's "substr" to get two halves of a 40 character string:
What I have is variable $dk that's 40 chars long. I've verified the variable's there with an echo statement and it's returning the right value. I've verified the "gkgk" = 1 or 2 as well. But I'm getting nothing as a result. Why?
$dk = "1234567891123456789212345678931234567894";
echo "<br><br>DK = ".$dk."<br><br>";
if ($a == 1) {
echo "A = 1<br><br>";
$gk = substr($dk, 0, 20); //I'm expecting the first 20 chars...12345678911234567892
} else {
echo "A = 2<br><br>";
$gk = substr($dk, 20, 20); //expecting the last 20 chars...12345678931234567894
}
echo "GK = ".$gk;
I tried putting the variable in quotes, just in case it was a syntax issue. All I get is
DK =1234567891123456789212345678931234567894
A = 1
GK =
No clue what's going wrong. I hope I've given enough explanation and my code so people can understand! Thank you for any help.
Code is correct but if you are checking $a is one or two, first of all u must add value to $a variable.
above of this ---> if ($a == 1) { <-------- add this $a=1;
or
above of this ---> if ($a == 1) { <-------- add this $a=2;
I've got a problem which takes up a lot of time. While it's supposed to be really easy (because it's just so simple!).
My problem:
I have these values inside two arraylists:
$row[0]->COUNTER1 20 10 15
$row[0]->GRADE_POINTS 0 3 5
I am supposed to change these arraylists into this example:
$row[0]->COUNTER1 20 0 0 10 0 15
$row[0]->GRADE_POINTS 0 1 2 3 4 5
So the missing values are supposed to have 0 as the counter.
While this isn't that hard to do it I'm probably over thinking it.
The code which I use to create the first set of numbers is:
$result = new SimpleXMLElement($xmlresult);
$xml = $result->children("soapenv", true)->Body->children();
$xmlBody = $xml[0];
$countPerResultaat = array();
foreach($xmlBody->query[0] as $row)
{
$countPerResultaat[] = (int) $row[0]->COUNTER1;
$xaxis[] = (string) $row[0]->GRADE_POINTS;
}
The code I though that would work is this:
for($i; $i<=10; $i++){
//foreach($xmlBody->query[0] as $row)
//{
$row = $xmlBody->query[0];
if($i==$row[0]->GRADE_POINTS){
$countPerResultaat[] = (int) $row[0]->COUNTER1;
$xaxis[] = (string) $row[0]->GRADE_POINTS;
}else{
$xaxis[] = (string) $i;
$countPerResultaat[] = (int) 0;
}
}
But the row can't be used, I really don't know how to fix this. My only solution would be to use another for-loop, which would create 100 values probably.
Thanks for helping in advance!
If I understand correctly and if $row[0]->COUNTER1 and $row[0]->GRADE_POINTS are arrays. You will just need to loop them and use in_array(). Consider this example:
$counter1 = array(20, 10, 15);
$grade_points = array(0, 3, 5);
$new_grade_points = range(min($grade_points), max($grade_points));
foreach($new_grade_points as $key => &$value) {
// check if its part of the missing index if not get the value,
// if its the missing index put 0
$value = (in_array($key, $grade_points)) ? array_shift($counter1) : 0;
}
$counter1 = array_values($new_grade_points); // now contains 20,0,0,10,0,15
$grade_points = array_keys($new_grade_points); // now contains 0,1,2,3,4,5
print_r($counter1);
Sample Output:
Array
(
[0] => 20
[1] => 0
[2] => 0
[3] => 10
[4] => 0
[5] => 15
)
I think you want to count the amount of times a grade has been given? You should just loop through as usual, and when there is no value you should/could define it as 0. After that just count how many duplicates you have in the array. That way the key of the $xaxis is the grade, and the value is the amount of times that grade has been given.
foreach($xmlBody->query[0] as $row)
{
$counter = (int) $row[0]->COUNTER1;
if(counter) $countPerResultaat[] = $counter;
else $countPerResultaat[] = 0;
}
$xaxis = array_count_values($counter);
I have come to this part of script so far:
dif($result>0)
{
$ii=0;
$jj=0;
while (odbc_fetch_row($result))
{
for ($jj = 1; $jj <= odbc_num_fields($result); $jj++)
{
$rr[$ii][$jj]=odbc_result($result,$jj);
if(is_null($rr[$ii][$jj]))
$rr[$ii][$jj] = noData;
echo $rr[$ii][$jj];
echo "<br />";
}
$ii++;
}
}
This works good for creating and populating dynamic tables. But i need also to create dynamic number of single line arrays which consist of column values of arrays i get previously.
Example:
if i get
Array1
2012.01.01 10 20 30
2012.01.02 1 2 3
2012.01.03 11 22 33
i need to convert to
Array2
2012.01.01 2012.01.02 2012.01.03
Array3
10 1 11
Array4
20 2 22
Array5
30 3 33
As i mentioned before the first part of script is needed, so is there a possibility to use result to create single line arrays for further use? I suppose i'm missing something...
What is wrong with a for loop and adding the elements to an array?'
$rr = array(array(1,2,3,4,5),array(6,7,8,9,0),array(11,22,33,44,55));
$result = array();
for($j=0;$j<count($rr[0]);$j++){
$col = array();
for($i=0;$i<count($rr);$i++) {
array_push($col,$rr[$i][$j]);
}
array_push($result,$col);
}
print_r($result);
for a demo see this codepad: http://codepad.org/wgSCtoMV
Bear with me here--this may be a bit confusing.
I am retrieving two sets of data with SQL. Here's the code, with the query. I'm using Zend Framework.
$assignments = $db->fetchAll("SELECT id,name,class FROM assignments");
foreach($assignments as $a) {
$assignmentID = $a['id'];
$studentData = $db->fetchAll(
"SELECT student,assignment,status,assignmentID FROM student_assignments WHERE assignmentID='$assignmentID'"
);
echo "<th>".$a['name']."</th>";
foreach($studentData as $s) {
$bottom .= "<tr><td>" . $s['student'] . " " . $s['assignmentID']
. " ".$s['status'] . "</td></tr>";
$i++;
}
}
echo "</tr>$bottom;";
Here's what the output looks like in the HTML:
|Assignment on 07/07/2012| |Assignment on 07/12/2012| |Assignment on 07/15/2012|
117 1 Y
332 1 N
36 1 N
420 1 N
332 1 Y
326 2 N
212 2 N
461 2 N
117 2 N
212 2 N
212 3 N
326 3 N
117 3 Y
420 3 Y
Now the top part is working great -- it's dynamically showing each assignment in the database. But I've been trying to figure out a way to get the appropriate data to show under those columns, to no effect. This is the closest I've gotten to making it look somewhat correct.
Essentially, the data that has "2" and "3" in the middle should go into the 2nd and 3rd columns, respectively. But this isn't happening because all the data is stored into the $bottom variable, rather than the data for each assignment.
Does anyone have any suggestions? This is driving me crazy, and I feel like the solution is staring me in the face.
Thanks!
First you want to iterate through every of your student assignments, and left join the assignments table to it so you can know the name of the assignment that it is related to.
$students = $db->fetchAll('SELECT sa.student,sa.assignment,sa.status,sa.assignmentID,a.name
FROM student_assignments AS sa
LEFT JOIN assignments AS a ON sa.assignmentID=a.id');
Then with the results, you can build an array to regroup everyone with the same assignment:
$columns = Array();
foreach($students as $s) {
$row = '<tr><td>'.$s['student'].' '.$s['assignmentID'].' '.$s['status'].'</td></tr>';
array_push($columns[$s['name']], $row);
}
Then with this array, you can finally print your content:
foreach ($columns as $key=>$value) {
echo '<th>'.$key.'</th>';
foreach ($value as $v) {
echo $v;
}
}
Of course this can be more compact (reduced into nested loops), and I have no way to fully test it, but it should help you in your process ;)