dthtvk3666 2010-04-30 10:51
浏览 7
已采纳

PHP循环...需要一些建议

I have the following code:

$q1 = $_POST["q1"];
$q2 = $_POST["q2"];
$q3 = $_POST["q3"];
$q4 = $_POST["q4"];
$q5 = $_POST["q5"];
$q6 = $_POST["q6"];
$q7 = $_POST["q7"];
$q8 = $_POST["q8"];

At the moment, this is hard coded and I need to manually change it each time, I'd like to use variables instead so that it's not a manual process.

Is it a case of using a loop, while or foreach?

If I had the the information $q and q in an array would that help?

Thanks,

Homer.

  • 写回答

5条回答 默认 最新

  • dqbn76906 2010-04-30 10:59
    关注

    Consider adjusting your forms to use Array notation, e.g.

    <ul>
        <li><input name="q[]" /></li>
        <li><input name="q[]" /></li>
        <li><input name="q[]" /></li>
        <li><input name="q[]" /></li>
        ...
    </ul>
    

    This would make $_POST['q'] contain an array with all input values given for 'q', which you can then easily iterate over with foreach like this:

    foreach($_POST['q'] as $q) {
        // do something with $q
    }
    

    See http://www.johnrockefeller.net/html-input-forms-sending-in-an-array-in-php/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?