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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.