2016-04-03 81 views
-1

我需要一些關於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']是「選擇」。

是否有可能僅回顯「選定」的表名稱而沒有將其包含到帶選項的可選字段中?

沒有foreach構造一個標識的步驟可以通過

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

顯示,但我怎樣才能得到名稱爲這個ID?

在此先感謝!

這是輸出:

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)) 
+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; 
+0

嗨Mawia, 是的,你理解我的正確答案與你的第一個答案。但不幸的是它不起作用。 我的代碼片段給了我一個選擇/選項字段內的輸出,但我只需要打印表名。 – THColonia

+0

你如何得到這個'$ tpl ['table_arr']'? –

+0

我',一個完全PHP的Noob,我真的不知道你的意思是我如何得到這個?它是預訂系統腳本的一部分,我只想編輯這個小部分,所以它是腳本的原始部分,它顯示了表名和ID。 – THColonia