Page 1 of 1

passing Arrays

Posted: Sun Dec 21, 2008 10:34 am
by abolmeer
Hello, can someone please help me with a problem I'm having with this script?

The result entered doesn't show after I submit form. It shows on the next submit and so on.

Any ideas would be very much appreciated, thanks.

Code: Select all

[color=#0000FF]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Names Array</title>
<style>
    td{ border: 1px solid black;}
    table{ margin: 20px; margin-right: auto; margin-left: auto;}
    .missing{ background-color: #99CC99; border-collapse: collapse;}
</style>
 
</head>
 
 
 
<body>  
    <?php
        $loop = array();
        $names = array(array());
        $i = 0;
        
        $loop = unserialize(urldecode($_POST['loop']));
        $names = unserialize(urldecode($_POST['names']));
        
        if(isset($_POST['clear'])){
            $loop = array();
            $names = array();
        }
        
        if(!empty($loop)){
            echo '<table>';
            foreach($loop as $v){
                $i++;
            }
            for($d = 0; $d < $i; $d++){
                $x = 0;
                $y = 1;
                echo '<tr><td>' . $names[$d][$x] . ' ' . $names[$d][$y] . '</td></tr>';
            }
            echo '</table>';
        }
        
        echo '<form action="" method="post"><table>
            <tr><td>First</td><td><input type="text" name="first" /></td></tr>
            <tr><td>Last</td><td><input type="text" name="last" /></td></tr>
            <tr><td></td><td>
                <input type="submit" name="input" value="Input" />&nbsp;
                <input type="submit" name="clear" value="Clear" />
            </td></tr>
            </table>';
        
        if(isset($_POST['input'])){
            $x = 0;
            $y = 1;
            $names[$i][$x] = $_POST['first'];
            $names[$i][$y] = $_POST['last'];
            $i++;
            $loop[] = $i; 
        }   
        
        $s_loop = urlencode(serialize($loop));
        $s_names = urlencode(serialize($names));
        
        echo '<input type="hidden" name="loop" value="' . $s_loop . '" />
            <input type="hidden" name="names" value="' . $s_names . '" />
            </form>';
            
        
        
    ?>
        
        
 
</body>
</html>[/color]

Re: passing Arrays

Posted: Sun Dec 21, 2008 1:40 pm
by abolmeer
After lots of guessing, came up with this and it's giving the required results:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Names Array</title>
<style>
    td{ border: 1px solid black;}
    table{ margin: 20px; margin-right: auto; margin-left: auto;}
    .missing{ background-color: #99CC99; border-collapse: collapse;}
</style>
 
</head>
<body>  
    <?php
        
        if(isset($_POST['ctr'])){
            $i = $_POST['ctr'];         
        } else{
            $i = 0; 
        }       
        if(isset($_POST['clear'])){
            $i = 0;
            empty($names);
        }
        
        $names = array(array());        
        $names = unserialize(urldecode($_POST['names']));       
        
        if(isset($_POST['input']) && $_POST['first'] != '' && $_POST['last'] != ''){
            $x = 0;
            $y = 1;
            $names[$i][$x] = $_POST['first'];
            $names[$i][$y] = $_POST['last'];
            $i++;           
        }
                
        $s_names = urlencode(serialize($names));            
 
        if($i >0){
            echo '<table>';
            for($d = 0; $d <$i; $d++){
                $x = 0;
                $y = 1;
                echo '<tr><td>' . $names[$d][$x] . ' ' . $names[$d][$y] . '</td></tr>';
            }
            echo '</table>';
        }
        
        echo '<form action="" method="post">
            <table>
                <tr><td>First</td><td><input type="text" name="first" /></td></tr>
                <tr><td>Last</td><td><input type="text" name="last" /></td></tr>
                <tr><td></td><td>
                    <input type="submit" name="input" value="Input" />&nbsp;
                    <input type="submit" name="clear" value="Clear" />
                </td></tr>
            </table>';      
        
        echo '<input type="hidden" name="loop" value="' . $s_loop . '" />' . "\n" . '
            <input type="hidden" name="names" value="' . $s_names . '" />' . "\n" . '
            <input type="hidden" name="ctr" value="' . $i . '" />' . "\n" . '
            </form>';
    ?>  
 
</body>
</html>