English 中文(简体)
来自数据库的弹出检查框列表
原标题:Populating checkboxlists from database
  • 时间:2012-05-25 11:48:46
  •  标签:
  • php

是否有可能从数据库值中弹出一个复选列表? 如果是的话, 请建议如何这样做 。

$query2 = "select * from Products where CategoryID =  $CategoryName  ";
 mysql_query($query2) or die(mysql_error());
 $array = array();

while($row = mysql_fetch_assoc($query2)){
$array[] = $row;}

foreach($array as $val)
 {
 if($val == the checkbox value )
最佳回答
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>

These are the few steps you have to do. 1.Fetch the values from database. 2. Covert the values in array as they are stored as string by (,) separated. 3. Then

foreach($values as $val)
     {
          if($val == Bike )
             {
                   $bikeflag =  1 ;
             }
            if($val == Car )
             {
                   $carflag =  1 ;
             }

    }
<form>
<input type="checkbox" name="vehicle" value="Bike"  <?php if(isset($bikeflag ) == 1 ){ ?>checked = checked <?php } ?>/> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car"   <?php if(isset($carflag) == 1 ){ ?>checked = checked <?php } ?>/> I have a car
</form>

希望这能帮到你

问题回答
  1. Get values from database with the appropriate SQL query
  2. Loop through results echoing out a checkbox element

历山山

  • Make sure each one has a unique name unless you want them to be an array. Then use array syntax for their name (e.g. name[])
  • Make sure to give each one a unique value

是的,它非常可能:

获取要装入结构的文档 :

Id and value

如上文所述,将复选框列表的数据源作为获取的结构化列表提供。

Now define the value field of the checkboxlist as "Id" and text as "value"




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签