TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   looping (http://www.talkphp.com/absolute-beginners/2962-looping.html)

Matt 06-15-2008 08:21 PM

looping
 
I'm trying to make a feature for my poll system to create a poll with unlimited options.
First off, the poll adding is formed into two steps, the first step you chose the question of the poll, then you chose how many options you want. The second step displays the amount of options I entered, say I wanted 3 options, so 3 option input fields are created.

That is all I have it down to, I can't figure out how I would insert a row into the database for each option provided, say I have 3 options, after filling them out and submitting the data, I want each option recorded into the database.

Now, here is the code I have so far.

PHP Code:

<?
    
//-----------------------------------------------------------------
    //  Create Poll
    //-----------------------------------------------------------------

    
case 3:

    if (
$_POST['question'] && $_POST['options'])
    {
        
//-----------------------------------------------------------------
        //  Insert the poll
        //-----------------------------------------------------------------

        
$db->insertRow("poll",array('name' => ''.$_POST['question'].'','user' => ''.$userinfo['username'].'','date' => ''.time().''));

        
// not sure what the name of the post is supposed to be retreiving
        
if ($_POST['options'])
        {

        
//-----------------------------------------------------------------
        //  Add in the options
        //-----------------------------------------------------------------

            
startbox("Step Two");

            
$options $_POST[options];

            echo 
"<form action='$PHP_SELF?page=polls&a=3' method='post'><table class='main' cellspacing='1' cellpadding='5'>";
        
            for (
$a 1$a <= $options$a++) 
            {
                    echo 
'<tr class="con1"><td>Option '.$a.': <input type="text" name="option'.$a.'" /></td></tr>';
            }
        
                
// i know this isn't right.
                
if ($_POST[option])
                {
                    
// this loop definitely wont work.
                    
foreach($_POST['option'] as $key => $value
                    {
                        
$poll_option[$key] = $_POST['option'][$key];
                    }
                }
        
?>

        </table><br/>
        <table class="main" cellspacing="1" cellpadding="3">
        <tr class="con1">
            <td width="50%">Actions:</td>
            <td width="50%" align="right"><input type="submit" value="Next &raquo;" /></td>
        </tr>
        </table></form>

    <?php
    
}

    } else {

    
?>

    <div class="box">
    <div class="boxtitle"><?=$language['Poll add title']; ?></div>
    <div class="boxcontent">
    <form action="<?=$PHP_SELF;?>?page=polls&a=3" method="post">
    <table class="main" cellspacing="1" cellpadding="5">
        <tr class="head">
            <td width="70%"><?=$language['Poll add desc']; ?></td>
            <td width="10%" align="center"><?=$language['Poll add desc3']; ?></td>
        </tr>
        <tr class='con1'>
            <td>
               <input type="text" name="question" class="box" style="width:100%;"  size="65" value="" />
            </td>
            <td align="center">
               <input type="text" class="box" name="options" style="width:30px;" />
            </td>
        </tr>
    </table>
    </div></div><br/>
    <table class="main" cellspacing="1" cellpadding="3">
        <tr class="con1">
            <td width="50%">Actions:</td>
            <td width="50%" align="right"><input type="submit" value="Next &raquo;" /></td>
        </tr>
    </table></form>

    <?php

    
}

    break;
?>

This roadblock is definitely setting me back, if possible, it would be great if somebody can provide the loop for me an explain it as well, it would help me avoid coming across a problem like this ever again. Thanks.

SpYkE112 06-16-2008 07:03 AM

You don't use the right name for the options POST array....
PHP Code:

                if ($_POST[option])
                {
                    
// this loop definitely wont work.
                    
foreach($_POST['option'] as $key => $value
                    {
                        
$poll_option[$key] = $_POST['option'][$key];
                    }
                } 

You have used "option" instead of "options"... Don't know if thats the problem...
</span></span>


All times are GMT. The time now is 04:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0