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. Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/ 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. Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/#findComment-647476 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. Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/#findComment-647713 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. Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/#findComment-647723 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. Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/#findComment-647791 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 ) */ ?> Link to comment https://forums.phpfreaks.com/topic/125254-solved-array-and-mysql-help/#findComment-647885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.