I believe this has something to do with my $paramList and $paramList1... The do-while loop is executing but nothing is being reported for those two variables. I declared them as undefined arrays then set them equal to the number of objects and scripts. Did I do something wrong? Any suggestions to try?
Thanks.
PHP:
<?php
$savefile = 'testing1.html';
ob_start();
function emu_getallheaders()
{
foreach($_SERVER as $name => $value)
if(substr($name, 0, 5) == 'HTTP_')
$headers[str_replace('X-Secondlife-', 'X-SecondLife-', str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))))] = $value;
return $headers;
}
$headers = emu_getallheaders(); // Just replace any use of `apache_request_headers()` with `emu_getallheaders()`.
$objectName = $headers["X-SecondLife-Object-Name"];
$objectKey = $headers["X-SecondLife-Object-Key"];
$ownerKey = $headers["X-SecondLife-Owner-Key"];
$ownerName = $headers["X-SecondLife-Owner-Name"];
$region = $headers["X-SecondLife-Region"];
$sensorNum = $_POST["sensorNum"];
$numOfObjects = $_POST["numOfObjects"];
$numOfScripts = $_POST["numOfScripts"];
$maxSensors = 4;
$paramList = array();
$paramList1 = array();
$paramList[$sensorNum] = $numOfObjects;
$paramList1[$sensorNum] = $numOfScripts;
$j = 1;
echo <<<EOT
<html>
<CENTER>
<H1>
<b>Heat Map<br><br></b>
</H1>
EOT;
do{
echo <<<EOT
<CENTER>
<H1>
<b>Sensor $j <br></b>
</H1>
</CENTER>
<font size="5" face="arial" color="red">
<CENTER>
Total number of objects running:
$paramList[$j]
<br>
Total number of scripts running:
$paramList1[$j]
<br>
</font>
</CENTER>
EOT;
$j = ($j + 1);
}while($j <= $maxSensors);
<<<EOT
</body>
</html>
EOT;
$thePara = ob_get_contents();
file_put_contents('testing1.html', $thePara);
?>
And this is my Second Life script in case someone wants to see:
//Get the number of objects and scripts in a given area
key requestid;
integer typeConst;
integer objects;
integer scripts;
integer two = 2;
webPage()
{
requestid = llHTTPRequest("http://www.wbi-icc.com/students/SL/thisisatest.php",
[HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/x-www-form-urlencoded"],
"numOfScripts=" + (string)scripts
+ "&sensorNum=" + (string)two);
}
default
{
state_entry()
{
}
//Activate on a touch
touch_start(integer total_number)
{
typeConst = 0;
llSensor("", NULL_KEY, SCRIPTED, 20.0, PI);
}
//Use the sensors to keep a count of the total objects and scripts
sensor(integer detected)
{
if(typeConst == 0)
{
llOwnerSay("There are "+(string)detected
+" scripted objects in range.");
typeConst = 1;
llSensor("", NULL_KEY, PASSIVE, 20.0, PI);
}
else if(typeConst == 1)
{
llOwnerSay("There are "+(string)detected
+" objects in range.");
}
}
no_sensor()
{
if(typeConst == 0)
{
llOwnerSay("There are no scripted objects in range.");
typeConst = 1;
llSensor("", NULL_KEY, PASSIVE, 20.0, PI);
}
else
{
llOwnerSay("No objects in range.");
}
}
}
Related
I am writing a bit of php code to output a random value
$max_mal = (3 - $oray);
$oray = 1;
$max = 100;
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
and for each loop goes here
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
I am looking to get decimal values upto 5 numbers, but once a negative value comes it results out as 0.000-1 instead of -0.00001
$myresultb = str_pad($mario, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
$total_score = 300;
echo $myresultf;
Secondly I am new to php learning so am I doing this PHP correct or it needs improvement
I have a div to show total score like this
<div id="total_score"></div>
and another div to show current score which value comes as echo $myresultf;
<div id="current_score"></div>
I want to update total score in real time with jquery wheneven button is clicked and <?php echo $myresultf ?> is refreshed in real time also
$("#play").click(function() {
var currentscore = $("#current_score").val();
var totalscore = $("#total_score").val();
how to do this.....
});
Try this:
$max = 100;
$oray = 1;
$max_mal = (3 - $oray);
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
$negative_var=false;
if($myresult < 0)
{
$negative_var=true;
$myresult = 0-$myresult;
}
$myresultb = str_pad($myresult, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
if($negative_var)
$myresultf="-".$myresultf;
$total_score = 300;
echo $myresultf;
simple use as follow:
$myresultb =str_replace('-','',$myresultb);
if($myresult == -1) {
$myresultf = '-0.'.$myresultb.'<br/>';
}
else {
$myresultf = '0.' . $myresultb . '<br/>';
}
so the task I'm trying to complete here is comparing a user input number against the fibonacci sequence and if it is a fibonacci number the program will echo true, if not it will echo false.
Can someone tell me where I'm going wrong?
this is my first file:
<?php
function print_fibonacci($n){
$first = 0;
$second = 1;
echo "Fibonacci Series: \n";
echo $first.' '.$second.' ';
for($i=2;$i<$n;$i++){
$third = $first + $second;
echo $third.' ';
$first = $second;
$second = $third;
}
}
/* Function call to print Fibonacci series upto 6 numbers. */
print_fibonacci(16);
?>
<form method="POST" action="fibonacci3.php"><br>
<label>Input a number to check if it is fibonacci or not.</label><br>
<input name="fib" type="text" placeholder="#" /><br>
<input type="submit" value="OK!" />
</form>
This outputs the fibonacci sequence until the 16th fibonacci number, and is followed by a form which allows a user to enter a number.
The next file is fibonacci3.php as referenced in the form.
<?php
include "fibonacci2.php";
$n = $_POST["fib"];
function fibonacci($n) {
//0, 1, 1, 2, 3, 5, 8, 13, 21
/*this is an error condition
returning -1 is arbitrary - we could
return anything we want for this
error condition:
*/
if((int)$n <0){
return -1;
echo "False";
}
if ((int)$n == 0){
return 0;
echo "0";
}
if((int)$n == 1 || $n == 2){
return 1;
}
$int1 = 1;
$int2 = 1;
$fib = 0;
for($i=1; $i<=$n-2; $i++ )
{
$fib = $int1 + $int2;
//swap the values out:
$int2 = $int1;
$int1 = $fib;
}
if ($fib = $int1 + $int2 && $n == $fib){
echo "True!";
} else {
echo "False!";
}
return $fib;
}
fibonacci((int)$n);
?>
I thought this might be correct but it's not outputting anything when the user inputs a number.
$n = 'Your number';
$dRes1 = sqrt((5*pow($n, 2))-4);
$nRes1 = (int)$dRes1;
$dDecPoint1 = $dRes1 - $nRes1;
$dRes2 = sqrt((5*pow($n, 2))+4);
$nRes2 = (int)$dRes2;
$dDecPoint2 = $dRes2 - $nRes2;
if( !$dDecPoint1 || !$dDecPoint2 )
{
echo 'True';
}
else {
echo 'False';
}
Getting Notice: Undefined offset: 25 in
C:\wamp\www\finalProjectDemo\search.php on line 32
I'm trying to read in from a file and search for a specific name and address within that for output. I know a database would be best. This is for a class assignment I'm giving out that's specifically set to work this way. I believe I almost have it all, but am just getting this problem. Fairly new to PHP.
I have this code:
<html>
<body>
<?php
// read lines into array
// search array for string
// get 7 lines from there.
$i = 0;
$fileName = "addresses.txt";
$readFile = fopen($fileName, 'r');
$readByLineArray = array();
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
while (!feof($readFile))
{
$readByLineArray[$i] = fgets($readFile);
//echo "$readByLineArray[$i] read from array position $i";
//echo "<br />";
$i++;
}
fclose($readFile);
$arrLength = count($readByLineArray);
$currentArrayPosition = 0;
for ($x=0;$x<=$arrLength;$x++){
if ($searchFirstName == $readByLineArray[$x])
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $readByLineArray[$x])
{
$searchLastNameSuccess = 1;
$currentArrayPosition = $x - 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
for ($y=0;$y<=7;$y++){
echo "$readByLineArray[$currentArrayPosition]<br />";
$currentArrayPosition++;
}
?>
</body>
</html>
Thanks for all your help!
Ben---
Try foreach :-
foreach ($readByLineArray as $temp){
if ($searchFirstName == $temp)
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $temp)
{
$searchLastNameSuccess = 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
Change your for loop like this..
for ($x=0;$x<$arrLength;$x++){ //<--- Should be < and not <=
Say if your array count is 3 , so the array elements keys are arranged as 0,1,2. When you put <= in the looping as condition , your code will check for an non-existent key with an index of 3 which will thrown an Undefined Offset notice.
EDIT :
The easier way....
<html>
<body>
<?php
$fileName = "addresses.txt";
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
foreach(file($fileName) as $recno=>$records)
{
if(stripos($records,$searchFirstName)!==false && stripos($records,$searchLastName)!==false)
{
$searchFirstNameSuccess = 1;
$searchLastNameSuccess = 1;
echo "Match Found at Position : $recno";
break;
}
}
?>
</body>
</html>
For the sake of simplicity let's say I have the following array:
$a = array();
$a[0]['lab'] = 1;
$a[0]['name'] = 'test1';
$a[1]['lab'] = 1;
$a[1]['name'] = 'test2';
$a[2]['lab'] = 2;
$a[2]['name'] = 'test3';
$a[3]['lab'] = 2;
$a[3]['name'] = "test4";
$a[4]['lab'] = 2;
$a[4]['name'] = "test5";
Keep in mind that the length of that array is completely variable and the number of items associated with each lab can vary as well. There might be one per lab there might be 100.
My desired HTML structure is the following:
<div class="parent"> <!-- Records for lab == 1 -->
<div class="child">test1</div>
<div class="child">test2</div>
</div>
<div class="parent"> <!-- Records for lab == 2 -->
<div class="child">test3</div>
<div class="child">test4</div>
<div class="child">test5</div>
</div>
I currently have a for loop with a bunch of extra logic now which is ugly/inefficient and occasionally misses the closing tag for the last "parent" div. I know there is a far more elegant way to do this and I would love to see what others have come up with.
** Edit:
Here is the logic I have in place now which is actually working for the test cases I have thrown at it but it looks horrible:
<?php
$labId = 0;
for($i = 0; $i < count(a); $i++)
{
if(($i+1) < count($a)) { $j = $i + 1;}
if($labId == 0 || $labId != $a[$i]['lab'])
{
echo '<div class="parent">';
}
echo '<div class="child">'.$a['name'].'</div>';
if(($a[$j]['lab'] != $labId && $a[$i]['lab'] != $labId && $labId != 0) || count($a) == 1)
{
echo '</div>';
}
$labId = $a[$i]['lab'];
}
?>
I would do it like this:
$a = ... // copied from your code
$newArray = array();
foreach($a as $e){
$newArray[$e['lab']][] = $e['name'];
}
foreach($newArray as $lab){
echo "<div class=\"parent\">\n";
foreach($lab as $child){
echo "\t<div class=\"child\">".$child."</div>\n";
}
echo "</div>\n";
}
Hope that's simple enough.
The code below should do the trick without creating a new array
foreach ($a as $key0 => $labs) {
foreach ($labs as $key1 => $value) {
if ($key1 == 'lab') {
echo '<div class="parent">';
} else {
echo '<div class="child">'.$value.'</div>';
}
}
//Close parent divs
if (isset($key0['lab'])) {
echo '</div>';
}
}
My solution was somewhat of a combination of the responses and some other trial and error.
<?php
// define original test array
$a = array();
$a[0]['lab'] = 1;
$a[0]['name'] = 'test1';
$a[1]['lab'] = 1;
$a[1]['name'] = 'test2';
$a[2]['lab'] = 2;
$a[2]['name'] = 'test3';
$a[3]['lab'] = 2;
$a[3]['name'] = "test4";
$a[4]['lab'] = 2;
$a[4]['name'] = "test5";
// new array has to be defined outside the foreach() loop, or it will be treated as a local var
$anew = array();
// for each entry, assign to $anew[lab#][next_avail_entry#]['name'] = <value>
foreach ($a as $arec)
$anew[$arec['lab']][]['name'] = $arec['name'];
// output: one parent div for each lab, plus subdivs for as many values as are avail for each lab
foreach ($anew as $alab)
{
printf("<div class=\"parent\">\n");
foreach ($alab as $arec)
printf(" <div class=\"child\">%s</div>\n", $arec['name']);
printf("</div>\n");
}
?>
* Edit: One other solution I found was to query for the labs and items separately and loop through the lab as the outer loop and the items as an inner foreach loop.
I have a solution to the below problem in PHP.
But it is taking too much time to execute for 10 digit numbers. I want to know where am I going wrong ?
I am new to dynamic programming .Can someone have a look at this ?
Problem
In Byteland they have a very strange monetary system.
Each Bytelandian gold coin has an integer number written on it. A coin n
can be exchanged in a bank into three coins: n/2, n/3 and n/4.
But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars. The exchange
rate is 1:1. But you can not buy Bytelandian coins.
You have one gold coin. What is the maximum amount of American dollars
you can get for it?
========================================================
<?php
$maxA=array();
function exchange($money)
{
if($money == 0)
{
return $money;
}
if(isset($maxA[$money]))
{
$temp = $maxA[$money]; // gets the maximum dollars for N
}
else
{
$temp = 0;
}
if($temp == 0)
{
$m = $money/2;
$m = floor($m);
$o = $money/3;
$o = floor($o);
$n = $money/4;
$n = floor($n);
$total = $m+$n+$o;
if(isset($maxA[$m]))
{
$m = $maxA[$m];
}
else
{
$m = exchange($m);
}
if(isset($maxA[$n]))
{
$n = $maxA[$n];
}
else
{
$n = exchange($n);
}
if(isset($maxA[$o]))
{
$o = $maxA[$o];
}
else
{
$o = exchange($o);
}
$temp = max($total,$m+$n+$o,$money);
$maxA[$money]=$temp; //store the value
}
return $temp;
}
$A=array();
while(1)
{
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(feof($handle))
{
break;
}
array_push($A,trim($line));
}
$count =count($A);
for($i=0;$i<$count;$i++)
{
$val = exchange($A[$i]);
print "$val \n";
}
?>
Here a reformatted version of the code for the ones (like I) who could understand the above. It doesn't improve anything.
function exchange($money) {
static $maxA = array(0 => 0);
if (isset($maxA[$money])) {
return $money;
}
$m = floor($money/2);
$o = floor($money/3);
$n = floor($money/4);
$total = $m+$n+$o;
if (isset($maxA[$m])) {
$m = $maxA[$m];
} else {
$m = exchange($m);
}
if (isset($maxA[$n])) {
$n = $maxA[$n];
} else {
$n = exchange($n);
}
if (isset($maxA[$o])) {
$o = $maxA[$o];
} else {
$o = exchange($o);
}
return $maxA[$money] = max($total, $m + $n + $o, $money);
}
I still have my code from this problem. But it's in c++. It's by no means the most efficient, but it passed. It might not be too hard to port to php.
#include <cstdio>
#include <queue>
using namespace std;
unsigned long results;
queue to_test;
int main()
{
char tmp_val[30];
unsigned long coin_value = 1;
while (coin_value)
{
scanf("%s", tmp_val);
coin_value = 0;
results = 0;
for (int w = 0; tmp_val[w] != '\0'; w++)
{
coin_value *= 10;
coin_value += tmp_val[w] - 0x30;
}
if (coin_value != 0)
{
to_test.push(coin_value);
while(!to_test.empty())
{
unsigned long tester = to_test.front();
to_test.pop();
unsigned long over2 = tester/2;
unsigned long over3 = tester/3;
unsigned long over4 = tester/4;
if (tester < over2 + over3 + over4)
{
to_test.push(over2);
to_test.push(over3);
to_test.push(over4);
}
else
{
results += tester;
}
}
printf("%lu\n", results);
}
}
}