Review My Foreach Action
Posted: Wed May 28, 2008 10:13 pm
hi introduce me, hairul azami from bandung indonesia.
I want to view some post data from my sql query, that has been create from the form before:
I had create some php code like this:
but I get missing result for each item.
I trust to use twice continue, but here the result:
I think this is should be the right result:
tell me what's wrong with my code ?
I want to view some post data from my sql query, that has been create from the form before:
Code: Select all
CREATE TABLE `post_array` (
`id` int(11) NOT NULL auto_increment,
`nama` varchar(255) collate latin1_general_ci NOT NULL,
`nilai_kecantikan` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;
--
-- Dumping data for table `post_array`
--
INSERT INTO `post_array` (`id`, `nama`, `nilai_kecantikan`) VALUES
(1, 'karina', 50),
(2, 'emoy', 70),
(3, 'tasya', 95);
Code: Select all
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('ujicoba');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>POST ARRAY</title>
</head>
<body>
<?
if($_POST['submit'])
{
$qry = mysql_query("SELECT * FROM post_array ORDER BY id");
$num = mysql_num_rows($qry);
foreach($_POST['nilaiCewe'] as $n)
{
foreach($_POST['namaCewe'] as $nm)
{
if($n <= 0 || $nm == '')
{
continue;
}
echo $n .' untuk =>> '. $nm . '<br>';
continue 2;
}
}
}
else
{
?>
<strong>NILAI KECANTIKAN:</strong><br>
<form action="" method="post">
<?
$qry = mysql_query("SELECT * FROM post_array ORDER BY id");
$num = mysql_num_rows($qry);
while($row = mysql_fetch_array($qry))
{
echo "<strong>$row[nama]</strong>";
?>
<input name="nilaiCewe[]" type="text" value="<? echo $row[nilai_kecantikan]; ?>" />
<input name="namaCewe[]" type="hidden" value="<? echo $row['nama']; ?>" />
<br>
<?
}
?>
<input name="submit" type="submit" value="submit" />
</form>
<?
}
?>
</body>
</html>I trust to use twice continue, but here the result:
Code: Select all
50 untuk =>> karina
70 untuk =>> karina
95 untuk =>> karinaCode: Select all
50 =>> karina
70 =>> emoy
95 =>> tasyatell me what's wrong with my code ?