2011-12-15 13 views
1

idがf1、f2、f3、...、f20の列を持つmysqlテーブルがあります。idはproductIDとf1、... f20です。製品の機能です。各製品に応じて、すべての列、列がない列、または列のみが列挙されている列があります。php&mysql - 単一行の列をループし、値を配列に渡す

各列はIを選択する必要が#Bは#のC#、D、B、C、Dは、異なる言語での値である場合(A =英語、B =フランス語など)

等区切られた文字列を保持しています私の必要とする言語部分を取得し、その値を配列に渡して、私のプロダクト仕様ページで使用するためには、各行の値(f1、f2 ...)を '#'で爆発させてください。

フェッチされた行($ row = my_fetch_arrayを使用しています)をループして、$ specs =( 'green'、 'M'、 '100'、 '子供たち...)など? PS:わかりました、複雑ですが、今はもっと良いアイデアを思いつきません。

+0

いくつかのコードがありますか? – Toto

+0

完全な表の行の例を1つ教えてもらえますか? – noob

+0

現時点で私が持っているのは、私のmysqlテーブルと紙のスケッチの数々です! – bikey77

答えて

4

これを試してみてください:

$result = mysql_query("..."); 

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    $arr = array(); 
    foreach ($row as $k=>$v) 
    { 
     $features = explode("#", $v); 
     $value = $features[1]; // get the specific language feature 
     $arr[] = $value; 
    } 
    $specs = join(", " , $arr); 
} 
+1

これはすぐにうまくいった、ありがとう!私はジョイン機能について知りませんでした。 – bikey77

+0

こんにちは@swatkins ...あなたはこれを解決する際に私を助けてくださいhttp://stackoverflow.com/questions/28493201/how-to-find-the-total-distance-traveled-based-on-geo-points-storedデータベース内の同じ問題に関連する – Prabs

0

がわからない、これが最善の方法のトーゴですが、あなたは、どのようにするためにLANGによって

<?php 
$langs=array('eng'=>0,'fr'=>1,'ger'=>2,'geek'=>3); 


while ($row=mysql_fetch_assoc($result)) { 
    $specs=explode('#',$row['f1']); 
    $other=explode('#',$row['f2']); 
    ... 
} 
//Get lang from cookie that you could set elsewhere 
$lang=(isset($_COOKIE['lang']))?$_COOKIE['lang']:'eng'; 

echo $specs[$langs[$lang]]; 

?> 
0

私の解決策を結果にアクセスし、自分のlangsで配列を定義することができますIあなたの質問を理解してください:

// Make a MySQL Connection 

$sQuery = "SELECT f1,f2,... FROM table WHERE id = ..."; 

$oResult = mysql_query($sQuery) or die(mysql_error()); 

//Fetch assoc to use the column names. 
$aRow = mysql_fetch_assoc($oResult); 

//Prepare the product properties array 
$aProductProperties = array("English"=>array(),"French"=>array(),"Dutch"=>array()); 

//Loop over all the columns in the row 
foreach($aRow as $sColName=>$sColVal){ 
//Explde the column value 
    $aExplodedCol = explode("#",$sColVal); 
    //The code below could be nicer when turned into a looped that looped over every language, 
    //But that would make the code less readable 
    $aProductProperties['English'][$sColName] = $aExplodedCol[0]; 
    $aProductProperties['French'][$sColName] = $aExplodedCol[1]; 
    $aProductProperties['Dutch'][$sColName] = $aExplodedCol[2]; 

} 

//Done, you should now have an array with all the product properties in every language