Edoardo Edoardo - 1 month ago 8
PHP Question

arrays and index php

i have some experience in programming languages like java, jsp, javascript, c, and c++ and recently i start to work with php but probably there is something that i don't know about arrays and index of arrays because i receive too many errors for few lines of code.
I'm trying to search on the web but i don't find nothing that can help me to solve my problems.
This is my code:

$proposte_o = array();
$proposte_m = array();
$orachiusura = 18;
$durata = 30;
$ora_pre = array("10", "15");
$min_pre = array("30", "20");
$durata_pre = array("20", "40");
$oraproposta = 9;
$minuti = 0;
$ora_p = 13;
$ora_r = 15;
$i = 0;
$a = 0;
while ($orachiusura - $oraproposta > 1 && 60 - $minuti >= $durata && $i <= $ora_pre.length) {
if ($ora_p - $oraproposta <= 0) {
$oraproposta = $ora_r;
$minuti = 0;
} else if ($ora_p - $oraproposta = 1 && $minuti + $durata >= 60) {
$oraproposta = $ora_r;
$minuti = 0;
}
if (ora_pre[i] - oraproposta > 0) {
$proposte_o[a] = $oraproposta;
$proposte_m[a] = $minuti;
$a++;
if ($minuti += $durata = 60) {
$oraproposta += 1;
$minuti = 0;
} else if ($minuti += $durata > 60) {
$oraproposta += 1;
$minuti = 60 - $minuti;
$minuti = $durata - $minuti;
} else if ($minuti += $durata < 60) {
$minuti += $durata;
}
} else if (visite_pre[i] - oraproposta == 0) {
if ($min_pre[i] - minuti > $durata) {
$proposte_o[a] = $oraproposta;
$proposte_m[a] = $minuti;
$a++;
if ($minuti += $durata = 60) {
$oraproposta += 1;
$minuti = 0;
} else if ($minuti += $durata > 60) {
$oraproposta += 1;
$minuti = 60 - $minuti;
$minuti = $durata - $minuti;
} else if ($minuti += $durata < 60) {
$minuti += $durata;
}
} else {
if ($min_pre[i] + durata_pre[i] < 60) {
$oraproposta = $ora_pre[i];
$minuti = $min_pre[i] + durata_pre[i];
$i++;
} else if ($min_pre[i] + durata_pre[i] == 60) {
$oraproposta = $ora_pre[i] + 1;
$minuti = 0;
$i++;
} else if ($min_pre[i] + durata_pre[i] > 60) {
$oraproposta = $ora_pre[i] + 1;
$minuti = 60 - $minuti;
$minuti = $durata - $minuti;
$i++;
}
}
} else if (visite_pre[i] - oraproposta < 0) {
if ($min_pre[i] + durata_pre[i] < 60) {
$oraproposta = $ora_pre[i];
$minuti = $min_pre[i] + durata_pre[i];
$i++;
} else if ($min_pre[i] + durata_pre[i] == 60) {
$oraproposta = $ora_pre[i] + 1;
$minuti = 0;
$i++;
} else if ($min_pre[i] + durata_pre[i] > 60) {
$oraproposta = $ora_pre[i] + 1;
$minuti = 60 - $minuti;
$minuti = $durata - $minuti;
$i++;
}
}
}


For this code i receive a lot of errors, that are:
Notice: Use of undefined constant length - assumed 'length' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 22

Notice: Array to string conversion in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 22

Notice: Use of undefined constant ora_pre - assumed 'ora_pre' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 30

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 30

Warning: Illegal string offset 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 30

Notice: Use of undefined constant oraproposta - assumed 'oraproposta' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 30

Notice: Use of undefined constant visite_pre - assumed 'visite_pre' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 44

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 44

Warning: Illegal string offset 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 44

Notice: Use of undefined constant oraproposta - assumed 'oraproposta' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 44

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 45

Notice: Undefined index: i in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 45

Notice: Use of undefined constant minuti - assumed 'minuti' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 45

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 60

Notice: Undefined index: i in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 60

Notice: Use of undefined constant durata_pre - assumed 'durata_pre' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 60

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 60

Warning: Illegal string offset 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 60

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 61

Notice: Undefined index: i in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 61

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 62

Notice: Undefined index: i in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 62

Notice: Use of undefined constant durata_pre - assumed 'durata_pre' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 62

Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 62

Warning: Illegal string offset 'i' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 62

Notice: Use of undefined constant a - assumed 'a' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 91

Notice: Undefined index: a in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 91

Notice: Use of undefined constant a - assumed 'a' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 92

Notice: Undefined index: a in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 92

Notice: Use of undefined constant length - assumed 'length' in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 22

Notice: Array to string conversion in C:\xampp\htdocs\ShuttleFIX\proposte.php on line 22

In my file the line 22 is the line where there is the condition of the while.
Can someone help me?
Thank's

Answer

You have many variables that do not start with dollar ($). In PHP all variables must start with dollar.

The .length you have in your code is not a function in PHP. Instead use count($array);

I corrected your code. Please check the array $visite_pre in lines 37 & 68, which is not mentioned anywhere in your code and returns error.

<?php
$proposte_o = array();
$proposte_m = array();
$orachiusura = 18;
$durata = 30;
$ora_pre = array("10", "15");
$min_pre = array("30", "20");
$durata_pre = array("20", "40");
$oraproposta = 9;
$minuti = 0;
$ora_p = 13;
$ora_r = 15;
$i = 0;
$a = 0;
while ($orachiusura - $oraproposta > 1 && 60 - $minuti >= $durata && $i <= count($ora_pre)) {
    if ($ora_p - $oraproposta <= 0) {
        $oraproposta = $ora_r;
        $minuti = 0;
    } else if ($ora_p - $oraproposta = 1 && $minuti + $durata >= 60) {
        $oraproposta = $ora_r;
        $minuti = 0;
    }
    if ($ora_pre[$i] - $oraproposta > 0) {
        $proposte_o[$a] = $oraproposta;
        $proposte_m[$a] = $minuti;
        $a++;
        if ($minuti += $durata = 60) {
            $oraproposta += 1;
            $minuti = 0;
        } else if ($minuti += $durata > 60) {
            $oraproposta += 1;
            $minuti = 60 - $minuti;
            $minuti = $durata - $minuti;
        } else if ($minuti += $durata < 60) {
            $minuti += $durata;
        }
    } else if ($visite_pre[$i] - $oraproposta == 0) {
        if ($min_pre[$i] - $minuti > $durata) {
            $proposte_o[$a] = $oraproposta;
            $proposte_m[$a] = $minuti;
            $a++;
            if ($minuti += $durata = 60) {
                $oraproposta += 1;
                $minuti = 0;
            } else if ($minuti += $durata > 60) {
                $oraproposta += 1;
                $minuti = 60 - $minuti;
                $minuti = $durata - $minuti;
            } else if ($minuti += $durata < 60) {
                $minuti += $durata;
            }
        } else {
            if ($min_pre[$i] + $durata_pre[$i] < 60) {
                $oraproposta = $ora_pre[$i];
                $minuti = $min_pre[$i] + $durata_pre[$i];
                $i++;
            } else if ($min_pre[$i] + $durata_pre[$i] == 60) {
                $oraproposta = $ora_pre[$i] + 1;
                $minuti = 0;
                $i++;
            } else if ($min_pre[$i] + $durata_pre[$i] > 60) {
                $oraproposta = $ora_pre[$i] + 1;
                $minuti = 60 - $minuti;
                $minuti = $durata - $minuti;
                $i++;
            }
        }
    } else if ($visite_pre[$i] - $oraproposta < 0) {
        if ($min_pre[$i] + $durata_pre[$i] < 60) {
            $oraproposta = $ora_pre[$i];
            $minuti = $min_pre[$i] + $durata_pre[$i];
            $i++;
        } else if ($min_pre[$i] + $durata_pre[$i] == 60) {
            $oraproposta = $ora_pre[$i] + 1;
            $minuti = 0;
            $i++;
        } else if ($min_pre[$i] + $durata_pre[$i] > 60) {
            $oraproposta = $ora_pre[$i] + 1;
            $minuti = 60 - $minuti;
            $minuti = $durata - $minuti;
            $i++;
        }
    }
}