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

在SQL表中查找ID的名称,并通过PHP进行回显,php,sql,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

我需要一些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
}
?>
但是我怎样才能得到这个身份证的名字呢

提前感谢

这是输出:

嗨,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 ) )

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

$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>';
$get=$\u get['table\u id'];
$output=“”;
回声';

echo'----见下面我的答案。嗨,Mawia,是的,你的第一个答案正确地理解了我。但不幸的是,它不起作用。我的代码片段在一个select/option字段中给出了输出,但我只需要打印表名。你是如何得到这个
$tpl['table\u arr']
?我是一个完全的PHP Noob,我真的不知道你的意思是什么?这是预订系统脚本的一部分,我只想编辑这一小部分,使其成为脚本的原始部分,显示表格名称和ID。添加打印($tpl['table\u arr'])并在问题中显示您的输出。我已更新了我的答案,请检查。如果你有任何错误,请告诉我。
    $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>';
$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;