2016-04-14 6 views
-1

Ich möchte Werte von meiner Schleife an eine andere PHP-Seite übergeben. aber es gibt nur den letzten Wert. Ich habe versucht, Array zu verwenden, aber es funktioniert nicht.Wie bekomme ich Werte von Schleife in ein Array, um zu einer anderen PHP-Seite

 $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>'; 
} 

?> 

wiederholte ich nur den letzten Wert des $ x2, aber ich möchte für einen anderen Zweck alle den Wert von $ x2 auf einen anderen PHP-Seite zu übergeben. gibt es einen Weg?

// Ich fand die Lösung für die Array-Dinge, aber jetzt habe ich Probleme, die Array-Elemente in einer anderen PHP-Seite auszudrucken. Ich erhalte (Hinweis: Array String-Konvertierung)

i beheben Sie den früheren Code

$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; 

dies mein Code aus dem Array-Elemente zu drucken.

$x2 = $_SESSION['roots']; 

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

// Ich repariere meinen Code. es funktioniert schon.

+0

Sie benötigen ein Array - Sie sagen, „es funktioniert nicht“ - können Sie zeigen, wie Sie es versucht? –

+0

@RobbieAverill $ roots = array(); foreach ($ x2 als $ x_2) { $ roots [] = $ x_2; } echo $ roots; // Ich habe versucht mit foreach-Schleife ist es falsch? –

+0

Ändern Sie '$ _SESSION ['x2'] = $ x2;' in '$ _SESSION ['x2'] [] = $ x2;' dann verwenden Sie '$ _SESSION ['x2']' auf der nächsten Seite, was sein wird ein Array, das Sie durchlaufen können –

Antwort

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

Es gibt keine Syntax, um $ x2 in ein Array zu ändern.

Versuchen

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

kannst du mir wie zeigen? Es tut mir Leid. –

+0

$ x2 = isset ($ _ SESSION ['x2'])? $ _ SESSION ['x2']: []; ... $ x2 [] = Zahl_format ($ dx/$ d, 5); ... $ _SESSION ['x2'] = $ x2; –

Verwandte Themen