while(strcasecmp(trim($l),"end")!=0 && $pos<count($getjkl))
{
$pieces = preg_split("/[\s]+/",trim($l));
if(substr($pieces[0],0,1)!="#" && count($pieces)>1)
{
if(file_exists($getprj."/mat/".str_replace("\\", "/",$pieces[1])))
{
$found = false;
for($j=0; $j<count($compilefiles); $j++)
{
if(strcasecmp($compilefiles[$j][2],"mat\\".$pieces[1])==0)
$found=true;
}
if($found==false)
{
$compilefiles[$numfiles][0] = $fileoffset;
$compilefiles[$numfiles][1] = filesize($getprj."/mat/".str_replace("\\", "/",$pieces[1]));
$compilefiles[$numfiles][2] = "mat\\".$pieces[1];
$fileoffset = $fileoffset + $compilefiles[$numfiles][1];
$numfiles++;
}
}
else if(file_exists($getprj."/3do/mat/".str_replace("\\", "/",$pieces[1])))
{
$found = false;
for($j=0; $j<count($compilefiles); $j++)
{
if(strcasecmp($compilefiles[$j][2],"3do\\mat\\".$pieces[1])==0)
$found=true;
}
if($found==false)
{
$compilefiles[$numfiles][0] = $fileoffset;
$compilefiles[$numfiles][1] = filesize($getprj."/3do/mat/".str_replace("\\", "/",$pieces[1]));
$compilefiles[$numfiles][2] = "3do\\mat\\".$pieces[1];
$fileoffset = $fileoffset + $compilefiles[$numfiles][1];
$numfiles++;
}
}
}
$l=$getjkl[$pos];
$pos++;
}
This here is a piece of code where I read a file and check if the listed files in there exist. Well, I actually read the file like this:
$getjkl = preg_split("/\R/",file_get_contents($levelfile));
and then step through it line by line. This block is repeated 10 times with slight differences for different file types. I then realize that I'll have to go through some file types a few more times because more file names appear later on. But instead of repeating this code, I thought I could just call this code block several times. So I do something like this:
while(strcasecmp(trim($l),"end")!=0 && $pos<count($getjkl))
{
$pieces = preg_split("/[\s]+/",trim($l));
if(substr($pieces[0],0,1)!="#" && count($pieces)>1)
{
GetMats($getprj,$pieces,$compilefiles,$numfiles,$fileoffset);
}
$l=$getjkl[$pos];
$pos++;
}
function GetMats(&$getprj,&$pieces,&$compilefiles,&$numfiles,&$fileoffset)
{
if(file_exists($getprj."/mat/".str_replace("\\", "/",$pieces[1])))
{
$found = false;
for($j=0; $j<count($compilefiles); $j++)
{
if(strcasecmp($compilefiles[$j][2],"mat\\".$pieces[1])==0)
$found=true;
}
if($found==false)
{
$compilefiles[$numfiles][0] = $fileoffset;
$compilefiles[$numfiles][1] = filesize($getprj."/mat/".str_replace("\\", "/",$pieces[1]));
$compilefiles[$numfiles][2] = "mat\\".$pieces[1];
$fileoffset = $fileoffset + $compilefiles[$numfiles][1];
$numfiles++;
}
}
else if(file_exists($getprj."/3do/mat/".str_replace("\\", "/",$pieces[1])))
{
$found = false;
for($j=0; $j<count($compilefiles); $j++)
{
if(strcasecmp($compilefiles[$j][2],"3do\\mat\\".$pieces[1])==0)
$found=true;
}
if($found==false)
{
$compilefiles[$numfiles][0] = $fileoffset;
$compilefiles[$numfiles][1] = filesize($getprj."/3do/mat/".str_replace("\\", "/",$pieces[1]));
$compilefiles[$numfiles][2] = "3do\\mat\\".$pieces[1];
$fileoffset = $fileoffset + $compilefiles[$numfiles][1];
$numfiles++;
}
}
}
Took me a while to figure out that the variables don't automatically follow and get changed in a function unless I &reference them. However, after doing this to two code blocks, it seems to slow down extra much so that I hit the 30 second execution limit. Are functions really that much more expensive? And what would be the best way to call back to repeating code while still retaining the arrays and counters?
$getprj = string = folder
$pieces = array
$compilefiles = array (set a ways back)
$numfiles = int ( = 0 from the start) (counter)
$fileoffset = int ( = 0 from the start) (counter)
Related
I'm running a simple script which puts an integer through the formula of the Collatz conjecture and adds the output of each step into an array.
I want to use a function to detect if there's a cycle in the array, using Floyd's algorithm. And though I feel like I'm not doing a bad job, I don't seem to get it right. At this moment I'm getting the error Trying to get property 'next' of non-object in C:\xampp\htdocs\educom\week3\functions.php on line 12
See my code below. Any feedback is greatly appreciated!
include("functions.php");
$n = $_POST['number'];
$step = 0;
$reeks1 = array();
$cycle = 0;
echo "Your entry is: ". $n ."<br><br>";
while($n!==1 && $cycle==0){
$cycle = detect_cycle(array($reeks1));
if($n % 2 == 0){
$n = $n / 2;
array_push($reeks1, "$n");
$step++;
echo $step .": ". $n ."<br>";
}else{
$n = ($n * 3) + 1;
array_push($reeks1, "$n");
$step++;
echo $step .": ". $n ."<br>";
}
}
functions.php:
function detect_cycle($node){
if ($node==NULL){
return FALSE;
}
$turtle = $node;
$rabbit = $node->next;
while($rabbit != NULL){
if($rabbit === $turtle){
return TRUE;
}elseif($rabbit->next == NULL){
return FALSE;
}else{
$turtle = $turtle->next;
$rabbit = $rabbit->next->next;
}
}
return FALSE;
}
Check this out. IMPORTANT I don't know is this according to your theory. but it won't give you errors if you use like this.
function detect_cycle($node){
if ($node==NULL){
return FALSE;
}
$turtle = $node;
$rabbit = $node[0];
while($rabbit != NULL){
if($rabbit === $turtle){
return TRUE;
}elseif($rabbit[0] == NULL){
return FALSE;
}else{
$turtle = $turtle[0]; // use the number of the element key starting from 0
$rabbit = $rabbit[0][1];
}
}
return FALSE;
}
Below is the code which I'm willing to compress:
$somefield = 0;
if ($config->get('var1.one') && is_numeric($config->get('var1.one'))) {
$somefield = $this->entityManager->getStorage('node')->load($config->get('var1.one'));
}
$different_field = 0;
if ($config->get('var2.two') && is_numeric($config->get('var2.two'))) {
$different_field = $this->entityManager->getStorage('node')->load($config->get('var2.two'));
}
After your comment, what I think you want is something like:
$somefield = checkVar("var1.one");
$different_field = checkVar("var2.two");
function checkVar($name) {
if ($config->get($name) && is_numeric($config->get($name))) {
return $this->entityManager->getStorage('node')->load($config->get($name));
} else {
return 0;
}
}
I think this what you wanted. The names I used are not necessarily the best ones, you should use some better suited to the actual use of the function.
Another way to do it.
$field = [];
$array= ['var1.one', 'var2.two'];
for ($i = 0; $i < count($array); $i++) {
if ( $config->get($array[i] ) && is_numeric($config->get($array[$i])) )
{
array_push($field, $this->entityManager->getStorage('node')->load($config->get($array[$i])));
}
}
So I m trying to get a random string generator to work with a for loop. I have gotten it to loop the number of times it should but it refuses to generate a new string per loop. Can someone look at my code and show me where i went wrong? Also there is no way of using unqid so do not mention it please.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$key = $_POST['keysd'];
if(isset($key) && is_string($key))
{
switch($key)
{
case "ksc";
$algor = "78.0000.".rnumstr(7);
break;
case "kpl";
$algor = "76.0000.".rnumstr(7);
break;
case "kfi";
$algor = "D01EB0A01472".rnumstr(1).strtoupper(ralphstr(3));
break;
}
$sum = $_POST['sum'];
$alg = $algor;
if(isset($sum))
{
for ($i = 0; $i < $sum; $i++)
{
echo $alg.'<br/>';
}
}
}
}
If you want to generate new $alg per loop's iteration, you must call your switch's code every iteration. Refactor your code:
function getRandomValue($key)
{
switch($key)
{
case "ksc":
return "78.0000.".rnumstr(7);
case "kpl":
return "76.0000.".rnumstr(7);
case "kfi":
return "D01EB0A01472".rnumstr(1).strtoupper(ralphstr(3));
}
}
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$key = $_POST['keysd'];
if(isset($key) && is_string($key))
{
$sum = $_POST['sum'];
if(isset($sum))
{
for ($i = 0; $i < $sum; $i++)
{
$alg = getRandomValue($key);
echo $alg.'<br/>';
}
}
}
}
I am trying to calculate a value based on a price field in an entity reference field.
I currently have this, which works...
if (isset($entity->field_choose_a_package['und'][0]['target_id'])) {
$package1nid = $entity->field_choose_a_package['und'][0]['target_id'];
$package1 = node_load($package1nid);
$package1price = $package1->field_price['und'][0]['value'];
} else {
$package1price = 0;
}
if (isset($entity->field_choose_a_package['und'][1]['target_id'])) {
$package2nid = $entity->field_choose_a_package['und'][1]['target_id'];
$package2 = node_load($package2nid);
$package2price = $package2->field_price['und'][0]['value'];
} else {
$package2price = 0;
}
if (isset($entity->field_choose_a_package['und'][2]['target_id'])) {
$package3nid = $entity->field_choose_a_package['und'][2]['target_id'];
$package3 = node_load($package3nid);
$package3price = $package3->field_price['und'][0]['value'];
} else {
$package3price = 0;
}
$packagestotal = $package1price + $package2price + $package3price;
$entity_field[0]['value'] = $packagestotal;
However, there could be an unlimited amount of packages added, and rather than me replicate the code for 20+ packages to try and cover my bases, there must be a way I can do a for each loop.
I have tried something like this,
$arr = $entity->field_choose_a_package['und'];
foreach ($arr as &$value) {
if (isset($entity->field_choose_a_package['und'][$value]['target_id'])) {
$package1nid = $entity->field_choose_a_package['und'][$value]['target_id'];
$package1 = node_load($package1nid);
$package1price = $package1->field_price['und'][$value]['value'];
} else {
$package1price = 0;
}
}
unset($value);
but I cant figure out how to increment the variables, or if even need to? Can i just calculate the totals from the foreach?
$packagestotal = 0;
$numPackages = 3;
for($i = 0; $i <= $numPackages; $i++) {
if(isset($entity->field_choose_a_package['und'][$i]['target_id'])) {
${'package' . $i . 'nid'} = $entity->field_choose_a_package['und'][$i]['target_id'];
${'package' . $i} = node_load(${'package' . $i . 'nid'});
$packagestotal += ${'package' . $i}->field_price['und'][0]['value'];
}
}
$entity_field[0]['value'] = $packagestotal;
That should work.
Although, I would recommend that you wrap the package variables in an array rather than using variable variables as then the code would be much more readable and you could access each package attribute using $package[$i]
i am trying to find a way to validate some numbers in a php array, checking if each value is its greater than the last value.
Here is an example:
$number['num1']=1;
$number['num2']=2;
$number['num3']=3;
$number['num4']=4;
if($number['num1'] > $number['num2'] || $number['num1'] > $number['num3'] ||
$number['num1'] > $number['num4']){
//Some error
}
i can manually check each but is there an easier way, any suggestions?
You can achieve this pretty easily with a simple loop and using the PHP array pointer functions next and current:
$array = [1,2,3,4];
$isValid = true;
$current = current($array);
while($next = next($array)) {
if($next <= $current) {
$isValid = false;
break;
}
$current = $next;
}
var_dump($isValid);
Example: http://ideone.com/3uHPMq
Scopey beat me to it, but here's what I did:
$number['nums'][4] = 4;
$number['nums'][2] = 2;
$number['nums'][1] = 600;
$number['nums'][3] = 3;
// Note, I rearranged the order above just to make sure it
// works no matter what order the values get put in
function isAscending($arr) {
ksort($arr);
for ($i=0; $i<count($arr); $i++) {
if (isset($arr[$i-1])) {
if ($arr[$i-1] > $arr[$i]) {
return false;
}
}
}
return true;
}
var_dump(isAscending($number['nums'])); // false