2012-03-20 31 views
1

我有這個代碼,將檢查數組包含特定的字符串和輸出所需的數據,但它是在一個函數內,因此輸出是使用連接「。=」 ,我也嘗試在其基本形式 即in_array似乎並不工作,而在函數裏

$show = array(); 
$show[] = "dog"; 
$show[] = "doga"; 
$show[] = "dogasd"; 
$show[] = "cat asd"; 
print_r($show); 
if(in_array("cat asd", $show)) 
{ 
echo "found"; 
} 
else 
{ 
echo "not found"; 
} 

,它是工作in_array功能,但在這裏:

public function personal(){ 

      $sql_field = "SELECT * FROM somewhere WHERE show_field = 1"; 
    $result = mysql_query($sql_field); 
    $num = mysql_num_rows($result); 

    $show = array(); 
    for($i=0;$i<$num;$i++) 
    { 
     $row = mysql_fetch_assoc($result); 
     $show[] = $row['field_name']."<br>"; 
    } 

      $block .= "<tr>"; 
    $block .= $show[0]; 

     if(in_array("firstName", $show)) 
     { 
      $block .= "<td style=\"width: 250px;\"><div class=\"fields\"><input type=\"text\" title=\"First Name*\" value=\"{$this->detail("firstName")}\" name=\"first_name\" class=\"placeholder textbox-long\" /></div></td>"; 
     } 
     if(in_array("lastName", $show)) 
     { 
      $block .= "<td style=\"width: 250px;\"><div class=\"fields\"><input type=\"text\" title=\"Last Name*\" value=\"{$this->detail("lastName")}\" name=\"last_name\" class=\"placeholder textbox-long\" /></div></td>"; 
     } 
    $block .= "</tr>"; 

    } 

它總是輸出虛假因此不顯示任何內容,我想這與非數組變量和它的工作正常,所以我的猜測是arr ay在函數內部不起作用?糾正我,如果我在這裏錯了。我需要這個功能陣列 任何想法傢伙?

謝謝:)功能

+0

簡單的檢查是'var_dump($ show)'並確認該值存在於數組中。 – 2012-03-20 16:51:13

回答

4

in_array()作品就好了裏面,但你希望它來搜索字符串,它不這樣做。如果您要搜索firstName<br>,它會找到您要查找的內容,因爲您已將firstName<br>添加到陣列中,但firstName不在陣列中。

+0

我看到了,我的錯誤,我只是從其他頁面複製那行代碼,因爲我在那裏使用它來以有序的方式輸出它。非常感謝你非常感謝你指出我的錯誤。 – magicianiam 2012-03-20 16:58:05

3

乍一看我所能看到的是,你最終可能是.<br>找不到確切的短語。

變化:

for($i=0;$i<$num;$i++) 
{ 
    $row = mysql_fetch_assoc($result); 
    $show[] = $row['field_name']."<br>"; 
} 

到:

for($i=0;$i<$num;$i++) 
{ 
    $row = mysql_fetch_assoc($result); 
    $show[] = $row['field_name']; 
} 

那就試試吧。

1

你試過in_array('firstName<br>')?在填充你的數組時,你在這裏添加一箇中斷標記到這個值:$show[] = $row['field_name']."<br>"; ...

順便說一句:用while代替foreach,更好...誠實!

按要求

這是我會怎麼寫呢:

$sql_field = "SELECT * FROM somewhere WHERE show_field = 1";//I'd only select the fields I needed 
$result = mysql_query($sql_field); 
//$result holds the resource, not the actual array 
//That's why you can do this: 
$show = array(); 
$firstName = $lastName = '';//in_array is slow, so store the values seperatly 
while ($row = mysql_fetch_assoc($result)) 
{ 
    $show[] = $row['field_name'].'<br/>'; 
    //I'm a bit of a nitpicker, but should be <br/> :-) 
    if ($row['field_name'] === 'firstName') 
    { 
     $firstName = '<td style="width: 250px;"><div class="fields"><input type="text" title="First Name*" value="'.$this->detail("firstName").'" name="first_name" class="placeholder textbox-long" /></div></td>'; 
     //use single quotes, a little faster, and less backslashes 
    } 
    if ($row['fieldName'] ==='lastName') 
    { 
     $lastName = '<td style="width: 250px;"><div class="fields"><input type="text" title="Last Name*" value="'.$this->detail("lastName").'" name="last_name" class="placeholder textbox-long" /></div></td>'; 
    } 
}//while ends when all results have been fetched 
$block .='<tr>'.$show[0].$firstName.$lastName.'</tr>'; 

這樣,如果不返回姓和名,字符串是空的。結果是一樣的。一個更好的sollution將形成陣列$temp=array('firstName'=>'','lastName'=>'');,只是做一些像這樣

if($temp[$row['fieldName']]) 
{ 
    $temp[$row['fieldName']] = '<td style="width: 250px;"><div class="fields"><input type="text" title="'.ucfirst(substr($row['fieldName'],0,-4)).' Name*" value="'.$this->detail($row['fieldName']).'" name="'.substr($row['fieldName'],0,-4).'_name" class="placeholder textbox-long" /></div></td>'; 
} 

或者你可以使用(V)的sprintf ...您的選擇相當無限。

更多信息:

http://www.php.net/while

http://www.php.net/mysql_fetch_assoc(例子!)

在這些網頁上找到的解釋,爲什麼雖然在這方面更好地之一。

如果你覺得C風格的字符串格式化:

http://www.php.net/manual/en/function.vsprintf.php

http://www.functions-online.com/

此代碼在現在的方式進行了測試,我也不知道你想要使用像一個解決方案我給了一個,所以請不要認爲這是一個完整的,可以隨時使用的代碼。

記住:

「調試是加倍努力爲在第一時間編寫代碼。因此,如果你如此巧妙地編寫代碼,你是根據定義,不夠聰明調試。 「。 - 布賴恩Kernighan

+0

我很喜歡,但我不熟悉foreach語句。你能否給我一個例子,謝謝 – magicianiam 2012-03-20 16:58:33