[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Working with arrays php and mysql

I would like to look into an array and output an array that tells me the positive or negative value of each number.

e.g.

$variables = array ( -1=>1, 9=>2, 12=>3, 0=>4);

array (-=>1, +=>2, +=>3, 0=>4)

thanks for the help
0
derekstattin
Asked:
derekstattin
1 Solution
 
Sander StadCommented:
Your question is al little vage. Can you be a little more precise with what you want and what you'd like to expect as a result.
0
 
derekstattinAuthor Commented:
Not having a great handle of programing language I am not sure how exactly to ask the question. I have been working on the code below. The code looks into an array and tells me in what period there is a negative value.  What I have been unable to figure out is how to capture these values into a new variable. In this example bayparkcapital.com/IR.php the code echos (from $number is negative) 12 is negative, 12 is negative, 12 is negative. I need to capture these 12's so I can pass them into another peice of code to get the payoff amount at a set irr.
I ask this question, becasue I am not too experienced with programing and I am trying to peice together how to achieve the desired results. I thought I could figure out a way to use get the negative/positive value of the variables in the array, then use array_count_values or array_chunk ie. Split an array into chunk before the negaive value, then count the number of variables in the array. Maybe there is a much easier way?
I had some help with the last part of the code below from mankowitz. Echo-ing $number gives me 108, the total number in the $variables array.
thanks for the help
 <?php
//connect to server
$mysqli = ();

//gather the topics
$loop_sql = "SELECT topic_id, topic_title, DATE_FORMAT(topic_create_time,  '%b %e %Y at %r') aS fmt_topic_create_time, topic_owner, INV_ID,INV_U_N, MS_F_NAME, MS_ID, CONT_DIST_1_1_08, cf_1_1_00 .... cf_12_1_08 FROM invdb ORDER BY topic_create_time DESC";
$variables = array(  'cf_12_1_08'=>1,
     'cf_11_1_08'=>2, .....
         'cf_1_1_00'=>108,
);
$loop_res = mysqli_query($mysqli, $loop_sql) or die(mysqli_error($mysqli));
echo $loop_sql;
while ($row = mysqli_fetch_assoc($loop_res)) {
foreach ($variables as $letter=>$number) {
if ($row[$letter] < 0) echo "$number is negative";
}
}
 mysqli_free_result($loop_res);
 
 //close connection to mysqli
 mysqli_close($mysqli);
?>
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now