您好,
我尝试为学校制作一个小形式,一切都工作正常,因为我做了功能" reorganizeVector()"。现在,当我想在浏览器上尝试我的表单(只有这个网页)时,它加载2~3分钟,我收到Bad Gateway 502错误。我几乎可以肯定我的问题出现在" reorganizeVector()"功能。我试图找到我的功能有什么不对,但我无法找到我做错了什么。
我将添加一些其他文件,如" index.php"明天,因为它在凌晨3点18分在这里,我必须在凌晨5点起床上学,所以......
如果你能帮助我,我将不胜感激
请帮帮我!
B.T.W.抱歉我的英语不好,我是法国加拿大人,我尽力做到最好。
<?php
if(version_compare(PHP_VERSION, '5.4.0') >= 0)
{
if(session_status() == PHP_SESSION_NONE)
session_start();
}
elseif(session_id() == '')
session_start();
define('MIN_CAR', 3);
define('MAX_CAR', 30);
define('MIN_QTY', 1);
define('MAX_QTY', 1000);
if(isset($_POST['name']) AND isset($_POST['qty']) AND isset($_POST['category']))
{
$nameValide = (strlen($_POST['name'])>=MIN_CAR AND strlen($_POST['name'])<=MAX_CAR);
$qtyValide = ($_POST['qty']>=MIN_QTY AND $_POST['qty']<=MAX_QTY);
if($nameValide AND $qtyValide)
{
filter_var($_POST['qty'], FILTER_SANITIZE_NUMBER_INT);
if(!isset($_SESSION['vector']))
{
$_SESSION['vector'] = array(
array('name'=>$_POST['name'],
'qty'=>$_POST['qty'],
'cat'=>$_POST['category']));
}
else
array_push($_SESSION['vector'], array('name'=>$_POST['name'], 'qty'=>$_POST['qty'], 'cat'=>$_POST['category']));
}
else
{
if(!$nameValide)
error('invalid name (between '.MIN_CAR.' and '.MAX_CAR.' caracters)');
if(!$qtyValide)
error('invalid quantity (between '.MIN_QTY.' and '.MAX_QTY.')');
}
}
function error($text)
{
if(isset($error))
array_push($error, '<p class="error">'.$text.'</p>');
else
$error = array('<p class="error">'.$text.'</p>');
}
//THIS IS THE FUNCTION THAT CAUSE THIS ISSUE BUT I DONT KNOW WHAT IN THE FUNCTION CAUSE IT
function reorganizeVector()
{
foreach($_SESSION['vector'] as $value)
{
if(isset($array))
array_push($array, array($value['name'], $value['cat']));
else
$array = array(array($value['name'], $value['cat']));
}
$backup = $array;
$x = 1;
for($i=0; $i<sizeof($array); $i++)
{
//name qty cat
$int = null;
$index = null;
do
{
$shifted = array_shift($array);
$index = array_search($shifted, $array)+$x;
if(!empty($index))
{
if(isset($temp))
array_push($int, $index);
else
$int = array($index);
}
$x++;
}
while(!empty($index));
if(isset($temp))
array_push($temp[$x], $int);
else
$temp = array($x=>$int);
}
foreach($temp as $key => $secondary)
{
$primary = $_SESSION['vector'];
$newQty = $primary[$key]['qty'];
foreach($secondary as $index)
{
$newQty += $primary[$index]['qty'];
}
$primary[$key]['qty'] = $newQty;
if(isset($newVec))
array_push($newVec, $primary[$key]);
else
$newVec = array($primary[$key]);
}
$_SESSION['vector'] = $newVec;
}
//--------------------------------------------------------------------HTML--------------------------------------------------------------------
?>
<h2>Add an article</h2>
<?php
if(isset($error))
foreach($error as $value)
echo $value;
?>
<form method="post" action="">
<p>
<label name="name">Name :</label>
<input type="text" name="name" pattern="^.{3,30}$" title="Between <?php echo MIN_CAR.' and '.MAX_CAR; ?> caracters" required>
<br>
<label name="qty">Quantity : </label>
<input type="text" name="qty" size="4" pattern="(^(0*[1-9]{1}[0-9]{0,2})$)|(^1000$)" title="Value between <?php echo MIN_QTY.' and '.MAX_QTY; ?>" required>
</p>
<p>
<label name="category">Category :</label><br>
<input name="category" type="radio" value="kitchen" checked> Kitchen<br>
<input name="category" type="radio" value="electronic"> Electronic<br>
<input name="category" type="radio" value="other"> Other<br>
<input name="category" type="radio" value="hardware"> Hardware<br>
<input name="category" type="radio" value="sport"> Sport<br>
<br>
<button type="submit">Validate</button>
</p>
</form>
<?php
if(isset($_SESSION['vector']))
{
reorganizeVector();//<---------------------------------------FUNCTION CALLED HERE
?>
<hr>
<table border="1" style="width:400px">
<tr>
<th style="width:50%">Name</th>
<th>Quantity</th>
<th>Category</th>
</tr>
<?php
foreach($_SESSION['vector'] as $value)
{
echo '
<tr>
<td>'.$value['name'].'</td>
<td>'.$value['qty'].'</td>
<td>'.$value['cat'].'</td>
</tr>
';
}
?>
</table>
<?php
}
?>
&#13;
答案 0 :(得分:0)
我认为你在这一行中进入了一个无限循环:while(!empty($index));
它似乎没有很好地定义while
循环。