prkarpi Posted September 22, 2008 Share Posted September 22, 2008 Hello everyone. It seems so simple, but it is the experience that I lack. I have a DB that calls various data that needs to be processed with array. In other words, I need certain DB words like "dIT_pos" to be converted into human language, so I need "dIT_pos" to be converted into "positive". I have this: $query = "SELECT something FROM $something WHERE something = '$something'"; $result = mysql_query($query); $query_data = mysql_fetch_array($result); $dIT_pos = $query_data["dIT_pos"]; $dIT_neg and so on... $NewArray = array ("$dIT_pos"=>"positive", "$dIT_neg"=>"negative", "$dIT_neu"=>"neutral", "$dIT_any"=>"anything"); If anyone could point me in the right direction so that I could use query_data variables like $dIT_pos and to put them into the array to translate them into regular words. I have looked for it on the web and could find it. Any help would be appreciated. thanks. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 are you talking about http://us3.php.net/str_replace or..... what? I don't really know what you're trying to do. Quote Link to comment Share on other sites More sharing options...
prkarpi Posted September 22, 2008 Author Share Posted September 22, 2008 are you talking about http://us3.php.net/str_replace or..... what? I don't really know what you're trying to do. I have a <form> on my site and it has <select> and <option> with values like <option value="dIT_pos">positive</option> which goes into my DB. The value 'dIT_pos' writes into DB. When DB is accessed it takes the given 'dIT_pos' value and puts onto my PHP script. I want to be able to convert 'dIT_pos' into a word, let's say 'positive'. However, it would convert only if those variables exist. Thanks again. Help is appreciated. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 like... $dIT_pos = 'somevaluefromdatabase'; if(!empty($dIT_pos)) { $dIT_pos = 'positive'; } ? if so, it's much easier to authenticate input on insertion, not retrieval. Quote Link to comment Share on other sites More sharing options...
prkarpi Posted September 22, 2008 Author Share Posted September 22, 2008 Thanks for your help. However, I'll be having quite a bunch of them (<option>) and thus it won't be efficient to write code like this for many of those. Is there a better way with ARRAY to make it work. thanks again. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 Try this... <?php $assignmentsArray = array ( 'dIT_pos' => 'positive', 'dIT_neg' => 'negative', 'dIT_neu' => 'neutral', 'dIT_any' => 'anything' ); $valuesFromDb = array( 'dIT_pos' => '', 'dIT_neg' => 'candy', 'dIT_neu' => 'a muffin', 'dIT_any' => '' ); $newArray = array(); foreach($valuesFromDb as $var => $val) { if(empty($val) || $valuesFromDb == 0) { $val = $assignmentsArray[$var]; } $newArray[$var] = $val; } print_r($newArray); /* Outputs: Array ( [dIT_pos] => positive [dIT_neg] => candy [dIT_neu] => a muffin [dIT_any] => anything ) */ ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.