Jump to content

[SOLVED] Array and MySQL help


prkarpi

Recommended Posts

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

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. :)

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
)
*/
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.