一次查询大量数据并用视图页面中的数据填充复选框
我有 365 条查询,如下所示。我很久以前就用原始 PHP 创建了这个,但由于我现在使用 codeigniter,我想将查询内容保留在我的模型中。
实际上,我创建的是一个带有复选框功能的日历,用于将日期插入数据库,当我用这些数据填充复选框时,我使用了以下脚本。
现在我不确定如何在 Codeigniter 中执行完全相同的操作,因为它遵循 MVC 结构。您能否告诉我,如果我想使用 codeigniter 实现同样的目标,我的模型和视图会是什么样子?
<--Query 1 january 01 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-01 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-01' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 01
<--Query 2 January 02 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-02 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-02' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 02
<--Query 3 January 03 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-03 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-03' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 03
当我显示日历时,它看起来如下所示。谢谢:)
现在编辑
我正在尝试实现同样的目标...
<input type="checkbox" name="date[]" value="<? echo"2011-05-18"; ?>" <?
if (in_array("2011-05-18", $date))
{
echo "checked=\"checked\"";
} ?> > Jan 03
现在这里 $日期..我必须通过控制器从我的模型中获取...
I have 365 numbers of queries like following. I had created this in raw PHP long time ago, but since I am using codeigniter now I want to keep the query thing inside my model.
Actually what I created was a calender with checkbox function to insert date into database and when I populated my checkboxes with those data I used the following script.
Now I am not sure how to do the exact same thing in Codeigniter as it follows the MVC structure. Could you please tell me if I want to achieve the same thing using codeigniter how my model and view would look like?
<--Query 1 january 01 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-01 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-01' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 01
<--Query 2 January 02 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-02 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-02' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 02
<--Query 3 January 03 -->
<input type="checkbox" name="date[]" value="<? echo"$year-01-03 "; ?>" <? include('connect.php');
$check = mysql_query("SELECT wdt FROM workingdayteacher WHERE date='$year-01-03' AND teachername='$teacherid'");
if(mysql_num_rows($check))
{
echo "checked=\"checked\"";
} ?> > Jan 03
When I display the calender it looks like following. Thanks :)
Edit
Now I am trying to achieve the same thing like this...
<input type="checkbox" name="date[]" value="<? echo"2011-05-18"; ?>" <?
if (in_array("2011-05-18", $date))
{
echo "checked=\"checked\"";
} ?> > Jan 03
Now here this $date .. I have to get from my model via controller...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeIgniter是MVC,这意味着您应该将查询数据的代码放在模型中,并将显示内容的代码放在视图中。
因此,您应该做的第一件事是提出一个包含视图中所需的所有信息的结构。在这种情况下,一个表示年份的数组包含表示日期的数组就可以了。这样,您就可以在视图中检查测试
$YOUR_ARRAY[$month][$day]
内容的日期。在您的模型中,您调用填充此数组所需的查询。
在Controller中,您调用Model来获取数组,并将其传递给视图。
您在视图中的代码将如下所示(根据需要进行编辑):
在您的模型中,您可能只需要一个查询即可完成,如下所示:
希望它有帮助!
CodeIgniter is MVC, that means you should put the querying data code in the Model, and the code to display things in the View.
So, first thing you should do is to come up with a structure with all the information you need in the view. In this case, an
array
representing year containingarrays
representing days should do. That way, in the view you could check a date testing the contents of$YOUR_ARRAY[$month][$day]
.In your Model, you call the queries needed to populate this array.
In the Controller, you call the Model to obtain the array, and pass it to the view.
Your code in the view would look like (edit as appropriated):
And in your model, you could probably be done with one query only, something like this:
Hope it helps!
在代码中,为什么不进行 2 个 foreach 循环,一个用于月份,一个用于日期,并继续检查循环的月份和日期是否与表中的任何内容匹配,并显示它们,而不是这种乏味的情况。
On the code, instead of this tediousness, why not go through 2 foreach loops, one for the months and one for the days, and keep checking whether the looping month and date matches any from the table, and show them.