在SQL表中查找ID的名称,并通过PHP回显

时间:2016-04-03 14:30:28

标签: php sql

我需要一些PHP脚本的帮助。

在此选择字段中,foreach构造填充单个选项字段:

<select name="table_id[]">
<option value="">---</option>
<?php
foreach ($tpl['table_arr'] as $table)
{
    ?>
    <option value="<?php echo $table['id']; ?>"<?php echo isset($_GET['table_id']) ? ($_GET['table_id'] == $table['id'] ? ' selected="selected"' : null) : null;?>>
        <?php echo stripslashes($table['name']); ?>, <?php echo $table['seats'] . ' ' . ($table['seats'] > 1 ? __('lblPeople', true, false) : __('lblPerson', true, false)); ?>
    </option>
    <?php
}
?>

它将所有表格的$table['name']$table['seats']回显为单个选项字段,并突出显示['table_id'] == $table['id']是否为&#34;已选择&#34;。

是否可以只回显&#34;选择&#34;表NAME没有将其包装到带有选项的可选字段中?

没有foreach结构的单个ID可以通过

显示
<?php echo $_GET['table_id']; ?>

但是如何获取此ID的NAME?

提前致谢!!!

这是输出:

嗨Mawia,

这是输出

Array ( 
[0] => Array ( [id] => 2 [room_id] => 1 [width] => 280 [height] => 70 [top] => 43 [left] => 71 [name] => 11 [seats] => 8 [minimum] => 6 [ratio] => 4x1 [round_table] => 0 [turned_table] => none ) 
[1] => Array ( [id] => 15 [room_id] => 1 [width] => 210 [height] => 70 [top] => 141 [left] => 71 [name] => 12 [seats] => 6 [minimum] => 4 [ratio] => [round_table] => 0 [turned_table] => none ) 
[2] => Array ( [id] => 16 [room_id] => 1 [width] => 140 [height] => 70 [top] => 138 [left] => 439 [name] => 13 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[3] => Array ( [id] => 17 [room_id] => 1 [width] => 140 [height] => 70 [top] => 139 [left] => 655 [name] => 14 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[4] => Array ( [id] => 18 [room_id] => 1 [width] => 140 [height] => 70 [top] => 256 [left] => 442 [name] => 15 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[5] => Array ( [id] => 19 [room_id] => 1 [width] => 140 [height] => 70 [top] => 256 [left] => 653 [name] => 16 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[6] => Array ( [id] => 20 [room_id] => 1 [width] => 140 [height] => 70 [top] => 380 [left] => 443 [name] => 17 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[7] => Array ( [id] => 21 [room_id] => 1 [width] => 210 [height] => 70 [top] => 255 [left] => 77 [name] => 18 [seats] => 6 [minimum] => 4 [ratio] => [round_table] => 0 [turned_table] => none ) 
[8] => Array ( [id] => 22 [room_id] => 1 [width] => 210 [height] => 70 [top] => 366 [left] => 77 [name] => 19 [seats] => 6 [minimum] => 4 [ratio] => [round_table] => 0 [turned_table] => none ) 
[9] => Array ( [id] => 23 [room_id] => 1 [width] => 420 [height] => 70 [top] => 43 [left] => 404 [name] => 20 [seats] => 12 [minimum] => 8 [ratio] => [round_table] => 0 [turned_table] => none ) 
[10] => Array ( [id] => 24 [room_id] => 1 [width] => 210 [height] => 70 [top] => 42 [left] => 875 [name] => 21 [seats] => 6 [minimum] => 3 [ratio] => [round_table] => 0 [turned_table] => none ) 
[11] => Array ( [id] => 25 [room_id] => 1 [width] => 210 [height] => 70 [top] => 139 [left] => 876 [name] => 22 [seats] => 6 [minimum] => 3 [ratio] => [round_table] => 0 [turned_table] => none ) 
[12] => Array ( [id] => 26 [room_id] => 1 [width] => 140 [height] => 70 [top] => 379 [left] => 650 [name] => 23 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[13] => Array ( [id] => 35 [room_id] => 2 [width] => 70 [height] => 210 [top] => 53 [left] => 109 [name] => 1 [seats] => 6 [minimum] => 3 [ratio] => 1x3 [round_table] => 0 [turned_table] => left ) 
[14] => Array ( [id] => 36 [room_id] => 2 [width] => 140 [height] => 70 [top] => 53 [left] => 418 [name] => 2 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[15] => Array ( [id] => 37 [room_id] => 2 [width] => 140 [height] => 70 [top] => 162 [left] => 419 [name] => 3 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[16] => Array ( [id] => 38 [room_id] => 2 [width] => 210 [height] => 70 [top] => 163 [left] => 105 [name] => 4 [seats] => 6 [minimum] => 3 [ratio] => [round_table] => 0 [turned_table] => none ) 
[17] => Array ( [id] => 39 [room_id] => 2 [width] => 70 [height] => 70 [top] => 288 [left] => 613 [name] => 5 [seats] => 2 [minimum] => 1 [ratio] => [round_table] => 0 [turned_table] => none ) 
[18] => Array ( [id] => 40 [room_id] => 2 [width] => 140 [height] => 70 [top] => 290 [left] => 419 [name] => 6 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[19] => Array ( [id] => 41 [room_id] => 2 [width] => 210 [height] => 70 [top] => 293 [left] => 107 [name] => 7 [seats] => 6 [minimum] => 4 [ratio] => [round_table] => 0 [turned_table] => none ) 
[20] => Array ( [id] => 42 [room_id] => 2 [width] => 140 [height] => 70 [top] => 421 [left] => 147 [name] => 8 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[21] => Array ( [id] => 53 [room_id] => 3 [width] => 140 [height] => 140 [top] => 33 [left] => 488 [name] => 220 [seats] => 12 [minimum] => 8 [ratio] => 1x6 [round_table] => 1 [turned_table] => none ) 
[22] => Array ( [id] => 54 [room_id] => 3 [width] => 350 [height] => 70 [top] => 36 [left] => 48 [name] => 221 [seats] => 10 [minimum] => 6 [ratio] => [round_table] => 0 [turned_table] => none ) 
[23] => Array ( [id] => 55 [room_id] => 3 [width] => 140 [height] => 70 [top] => 131 [left] => 43 [name] => 222 [seats] => 4 [minimum] => 3 [ratio] => [round_table] => 0 [turned_table] => none ) 
[24] => Array ( [id] => 56 [room_id] => 3 [width] => 70 [height] => 70 [top] => 128 [left] => 210 [name] => 2220 [seats] => 2 [minimum] => 1 [ratio] => [round_table] => 0 [turned_table] => none ) 
[25] => Array ( [id] => 57 [room_id] => 3 [width] => 140 [height] => 70 [top] => 222 [left] => 43 [name] => 223 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[26] => Array ( [id] => 58 [room_id] => 3 [width] => 70 [height] => 70 [top] => 222 [left] => 208 [name] => 2230 [seats] => 2 [minimum] => 1 [ratio] => [round_table] => 0 [turned_table] => none ) 
[27] => Array ( [id] => 59 [room_id] => 3 [width] => 140 [height] => 70 [top] => 314 [left] => 45 [name] => 224 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[28] => Array ( [id] => 60 [room_id] => 3 [width] => 70 [height] => 70 [top] => 312 [left] => 206 [name] => 2240 [seats] => 2 [minimum] => 1 [ratio] => [round_table] => 0 [turned_table] => none ) 
[29] => Array ( [id] => 61 [room_id] => 3 [width] => 140 [height] => 70 [top] => 418 [left] => 42 [name] => 225 [seats] => 4 [minimum] => 2 [ratio] => [round_table] => 0 [turned_table] => none ) 
[30] => Array ( [id] => 62 [room_id] => 3 [width] => 70 [height] => 70 [top] => 418 [left] => 205 [name] => 2250 [seats] => 2 [minimum] => 1 [ratio] => [round_table] => 0 [turned_table] => none ) )

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,请打开一个空白数组:

$name=array();
foreach ($tpl['table_arr'] as $table){
     if($table==$_GET['table_id']){
        $name[] = $table;
     }
}

打印结果:

echo $name['name'];

如果你需要选择这个内部选择选项。

    $get = $_GET['table_id'];

    $output = "";
    echo '<select>';
    echo '<option value="">----</option';
    foreach ($tpl['table_arr'] as $table) {
    if (in_array($get, array($table)) == true) {    
        $selected = 'selected';     
    }elseif(in_array($get, array($table))==false){
        $selected = '';
     }
    $output .= "<option value='$table['id']' {$selected}>
                    $table['name']</option>"; 
    }
    echo $output;
    echo '</select>';

根据您的问题数据,您可以获得您提供的ID名称,如下所示:

$arr = array(
            array(
                'id' => 1, 
                'room_id' => 1,
                'width' => 280,
                'height' => 70, 
                'top' => 43, 
                'left' => 71, 
                'name' => 11, 
                'seats' => 8, 
                'minimum' => 6, 
                'ratio' => '4x1', 
                'round_table' => 0, 
                'turned_table' => 'none'
            ),
            array('id' => 15,
                  'room_id' => 1,
                  'width' => 210,
                  'height' => 70,
                  'top' => 141,
                  'left' => 71,
                  'name' => 12, 
                  'seats' => 6,
                  'minimum' => 4,
                  'ratio' => '',
                  'round_table' => 0,
                  'turned_table' => 'none'
            )
    );
$get = 12;//GET DATA
    foreach($arr as $key=>$val):
        $name[]=$val;
    endforeach;
$table=array();
for($i=0;$i<count($arr); $i++):
    if($name[$i]['name']==$get):
        $table = $name[$i]['name'];
    endif;
endfor;
echo $table;