2016-04-14 5 views
-1

ループから別のPHPページに値を渡したいと思います。最新の値を渡すだけです。私は配列を使用しようとしましたが、動作しません。ループから別のPHPページに渡すためにループから値を取得するには

 $equation_x0 = equation ($x0); 
$equation_x1 = equation ($x1); 

$k=1; 


echo '<table colspan = "2" align = "center">'; 
echo '<tr><th style="width:350px;height:50px">ITERATIONS</th>'; 
echo '<th style="width:350px;">X</th></tr>'; 
echo '</table>'; 

do { 


if (abs ($equation_x1-$equation_x0) < $delta) { 

echo ' Solution cannot be found. ' . '<br>'; 
return 0; 

} 

$dx = ($x0*$equation_x1) - ($x1*$equation_x0); 
$d = $equation_x1 - $equation_x0; 
$x2 = number_format($dx/$d,5); 
$equation_x2 = equation($x2); 
$equation_x0 = $equation_x1; 
$equation_x1 = $equation_x2; 
$x0 = $x1; 
$x1 = $x2; 

echo '<table colspan = "2" align = "center">'; 
echo '<tr><td style="width:350px;height:50px" align = "center">' . $k . '</td>'; 
echo '<td style = "width:350px" align = "center">' . $x2 . '</td></tr>'; 
echo '</table>'; 

if ($k == $max_iter) { 

break; 

} 

$k++; 

} while (abs($equation_x2) > $delta && $k < $max_iter); 

$_SESSION['k'] = $k; 
echo $x2; 


echo '<h1 align = "center" style = "color:red"><br>The required solution is ' . $x2 . '</h1>' ; 


$result = mysqli_query($connect, "SELECT * FROM project ORDER BY id DESC LIMIT 1 "); 
$row = mysqli_fetch_assoc($result); 

$id='id'; 
echo $equation; 
echo '<br>' . $row['id']; 

echo '<br><a href="plotsecant.php?id=' . $row['id'] . ' " >PLOT</a>'; 

echo '</figure>'; 

echo '</div>'; 
} 

?> 

私は$×2の最後の値をエコーが、私は別の目的のため、別のPHPページに$×2のすべての値を渡したいです。方法はありますか?

//配列のものの解を見つけましたが、今では別のPHPページの配列項目を出力するのに問題があります。私は得続ける(注意:文字列への変換アレイ)

を私は、これは配列の項目をプリントアウトすることが私のコードで以前のコード

$roots = array(); 

do { 

if (abs ($equation_x1-$equation_x0) < $delta) { 

echo ' Solution cannot be found. ' . '<br>'; 
return 0; 

} 

$dx = ($x0*$equation_x1) - ($x1*$equation_x0); 
$d = $equation_x1 - $equation_x0; 
$x2 = number_format($dx/$d,6); 
$equation_x2 = equation($x2); 
$equation_x0 = $equation_x1; 
$equation_x1 = $equation_x2; 
$x0 = $x1; 
$x1 = $x2; 

echo '<table colspan = "2" align = "center">'; 
echo '<tr><td style="width:350px;height:50px" align = "center">' . $k . '</td>'; 
echo '<td style = "width:350px" align = "center">' . $x2 . '</td></tr>'; 
echo '</table>'; 

if ($k == $max_iter) { 

break; 

} 

$k++; 

$roots[]=$x2; 

} while (abs($equation_x2) > $delta && $k < $max_iter); 


for($i=0; $i<count($roots);$i++) { 
$roots[$i] . '<br>'; 
} 

$_SESSION['roots']= $roots; 

に修正します。

$x2 = $_SESSION['roots']; 

foreach($x2 as $roots) { 
echo $roots . '<br>'; 
} 

//私のコードを修正しました。それはすでに動作します。

+0

を試してみてください? –

+0

@RobbieAverill $ roots = array(); foreach($ x2 as $ x_2){ $ roots [] = $ x_2; } echo $ roots; // foreachループを使ってみましたが間違っていますか? –

+0

'$ _SESSION ['x2'] = $ x2;'を$ _SESSION ['x2'] [] = $ x2;に変更し、次のページで '$ _SESSION ['x2']'を使用します。配列をループすることができます –

答えて

-1
$x2 = number_format($dx/$d,5); 

$ x2を配列に変更する構文はありません。あなたは「それは動作しません」と言う - - あなたはそれを試してみましたどのように表示することができます

あなたは配列が必要

$x2 = isset($_SESSION['x2']) ? $_SESSION['x2']:[] ; 
... 
$x2[] = number_format($dx/$d,5); 
... 
$_SESSION['x2'] = $x2; 
+0

あなたは私にどのように表示できますか?ごめんなさい。 –

+0

$ x2 = isset($ _ SESSION ['x2'])?$ _セッション['x2']:[]; ... $ x2 [] = number_format($ dx/$ d、5); ... $ _SESSION ['x2'] = $ x2; –

関連する問題