Review My Foreach Action

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairulazami
Forum Newbie
Posts: 4
Joined: Wed May 28, 2008 10:05 pm

Review My Foreach Action

Post by hairulazami »

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:

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);
 
 
I had create some php code like this:

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>
but I get missing result for each item.

I trust to use twice continue, but here the result:

Code: Select all

50 untuk =>> karina
70 untuk =>> karina
95 untuk =>> karina
I think this is should be the right result:

Code: Select all

50 =>> karina
70 =>> emoy
95 =>> tasya

tell me what's wrong with my code ?
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Review My Foreach Action

Post by hansford »

You can't embed the foreach loop as you never get off the first name 'karina'. Use this type logic instead.


$i = 0;
foreach($_POST['nilaiCewe'] as $n)
{
if(($_POST['namaCewe'][$i] == "") || ($n <= 0 )){
$i++; //increase 'i' by one for the next loop
continue; //no need to stay in loop because our conditions aren't met

}
echo $n .' untuk =>> '. $_POST['namaCewe'][$i] . '<br>';
$i++; //if we skipped over the 'if' loop
}
Post Reply