2
$query = "select Code , count(ListID) as nums  from accesstable where Cust=" . $_SESSION ['Cust'] . " and App=" . $_SESSION ['App'] . " group by Code";
$result = mysql_query ( $query );

while ($row = mysql_fetch_array ( $result )){       
    $Codes[] = $row['Code'];
    $Values[] = $row['nums'];
}

This is the structure of my code that I am trying to learn how to properly access... Here is my dilemma... I am trying to figure out how to explicitly find the associated count of nums dependent on the value of a Code.

Let me explain in better detail where my issue is....

Lets say the list of codes is

Code     nums
    1          624
    7          825
   571       450
    9          393
    2          739
    9          590

The above code does successfully allow me to separate those values strictly into keys and values but I cannot figure out how to grab the nums value if the code is = to a certain value... I have currently been trying to declare a variable above the entire snippet of code and then declare it within the while statement but cannot figure out how to get the value to bind properly.... I will repaste the above code with one of my many failures in the while statement to give a better idea.

$Answer1 = 0;

$query = "select Code , count(ListID) as nums  from accesstable where Cust=" . $_SESSION ['Cust'] . " and App=" . $_SESSION ['App'] . " group by Code";
$result = mysql_query ( $query );

while ($row = mysql_fetch_array ( $result )){ 

$Codes[] = $row['Code'];
$Values[] = $row['nums'];

($Codes == 1){
    $Answer1 = // Right Here I want to Get the value 624 related to Code 1... Dont want to embarass myself with examples of what I have tried...
}

So how do I make a condition to output the value associated with a Code? I want to explicitly define these values as the list of codes can change with each customer... Luckily there are only a certain amount of codes so its not like I need to define too many of them... I just want to make sure I can get the nums value associated with a code and display it.

Hope I did a good job explaining this. :)

1
  • How can you have two different nums (ie. 393 and 590) for the same code (ie. 9)? Commented Jun 27, 2013 at 7:21

2 Answers 2

2

I'd do:

while ($row = mysql_fetch_array ( $result )){       
    $Codes[] = $row['Code'];
    $Values[$row['Code']] = $row['nums'];
}

and, to access the value associated to a code:

$code = 1;
$value = $values[$code];
Sign up to request clarification or add additional context in comments.

Comments

0

Since they would share the same array key, something like this would work-

if ($Codes[$key] == 1){
         $Answer1 = $Values[$key];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.