简体   繁体   English

PHP While循环中的While循环

[英]PHP While Loop in While Loop

I've had this problem for a little while and probably didn't ask the question properly last time. 我已经有一段时间这个问题了,可能上次没有正确提出这个问题。 Having dabbled again I'm still very much confused and stuck. 再次涉猎后,我仍然非常困惑和困惑。

I have a MySQL table that I list out a series of checkboxes based on values in this (around 200). 我有一个MySQL表,该表基于其中的值列出了一系列复选框(大约200个)。

I have another MySQL table where a user will store their preferences, when the list loads I wish for the items that the user had previously selected in the second MySQL table to be checkboxes that have the check mark already assigned indicating previous choice. 我有另一个MySQL表,用户将在其中存储他们的首选项,当列表加载时,我希望该用户先前在第二个MySQL表中选择的项目成为复选框,该复选框已分配了选中标记以指示先前的选择。 If you could please take a look at the following and point me in the right direction I'd be grateful. 如果可以的话,请看以下内容,并指出正确的方向,我将不胜感激。

$result = mysql_query("SELECT `car` FROM `carlist` ORDER BY variety ASC");
$result2 = mysql_query("SELECT `car` FROM `lists` WHERE `username` = 'Palendrone' ORDER BY variety ASC");

if (!$result) {
    die("Query to show fields from table failed");
}

if (!$result2) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
}


$fields_num2 = mysql_num_fields($result2);
for($j=0; $j<$fields_num2; $j++)
{
    $field2 = mysql_fetch_field($result2);
}

while($row = mysql_fetch_row($result))
{

    while($row2 = mysql_fetch_row($result2))
    {

        if("$row2[0]" <> "$row[0]")
        {?><input type="checkbox" value="<?php echo "$row[0]"?>" name="<?php echo "$row[0]"?>" id="<?php echo "$row[0]"?>">
            <label for="<?php echo "$row[0]"?>"><?php echo "$row[0]"?></label>
            <?php
        } else
        {?><input type="checkbox" value="<?php echo "$row[0]"?>" name="<?php echo "$row[0]"?>" checked="yes" id="<?php echo "$row[0]"?>">
            <label for="<?php echo "$row[0]"?>"><?php echo "$row[0]"?></label>
            <?php
        }

    } /* end while */

} /* end while */

I figured the first While loop to load the main 200 items in, then for each input in that table it cross checks against the users selection table in the second while loop, in that loop I have the if statement working in reverse order so as not to check it then uncheck it. 我想出了第一个While循环,用于加载主要的200个项目,然后针对该表中的每个输入,与第二个while循环中的用户选择表进行交叉检查,在该循环中,if语句的工作顺序相反,因此进行检查,然后取消选中。

I was pointed in the way of starting from scratch last week by another user on here, although I've started going through some tutorials I kinda need to get this last part of my project nailed... 上周,另一位用户向我指出了从头开始的方式,尽管我已经开始阅读一些教程,但我还是需要弄清楚项目的最后一部分...

I would suggest changing your query strategy - use only one instead of two. 我建议您更改查询策略-仅使用一种而不是两种。 You can check the value of second member of select list - lists . 您可以检查select list- lists的第二个成员的值。 car from result of query below for null value. car从下面的查询结果中获取空值。 If it is null then checkbox is not selected otherwise it is (it is present in second table). 如果为null,则未选中其他复选框(在第二个表中存在)。

$result = mysql_query("SELECT `carlist`.`car`, `lists`.`car`  FROM `carlist` left join `lists` on (`carlist`.`car` = `lists`.`car` and `lists`.`username` = 'Palendrone') ORDER BY `carlist`.variety ASC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM