Link to home
Start Free TrialLog in
Avatar of derekstattin
derekstattin

asked on

finding the negative value in an array php mysql

$variables = array(  'cf_12_1_08', 'cf_11_1_08',  'cf_10_1_08', &&..  'cf_1_1_00',);

There is one negative INT in the cf_ array.
I want to look into this array and name a new variable equal to the negative value.

In the second scenario, there are multiple negative vaules in the array and I want to create a variable that is equal to the array. For e.g. $negative_values (-10000, -1000 ,  ...  , -1000)

Thanks for the help,


Avatar of crush83
crush83
Flag of United States of America image

Well, for the data in the array you have provided, I'm not sure how you are calculating what is negative or not? The format of the data, for example 'cf_12_1_08', appears to be some sort of non-standard data format. What are you considering a negative value?

If the values of the array are just integers, then you can use something like the following to add the negative values to a new array:
$negativeVariables = array();
foreach ($variables as $key => $value) {
     if ($value < 0) {
          $negativeVariables[] = $value;
}

Open in new window

Avatar of derekstattin
derekstattin

ASKER

'cf_12_1_08', 'cf_11_1_08',  'cf_10_1_08'
These are the names of the variables. For each of these INT, in a MYSQL database. there is a  negative or positive whole number.
I will try your suggestion and relpy shortly
thanks
 
 
ASKER CERTIFIED SOLUTION
Avatar of crush83
crush83
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used the code you posted above. I made a mistake in saying the variables are INT. They are actually decimal(10,2)
For some reason the code echos the last value in the array 0.5, even when I change <0 to < -1
Any suggestions? Thanks again
First off, negative values are anything less than 0, not -1. So, you would use if ($value < 0.0)

Second, my code has no echo statements. I need to see how you are implementing it.

<?php
 
else $mysqli = mysqli_connect();
$username = $_SESSION['loggedin'];
$months = array('12_1_08','11_1_08','10_1_08','9_1_08','8_1_08','7_1_08','6_1_08','5_1_08','4_1_08','3_1_08','2_1_08','1_1_08','12_1_07','11_1_07','10_1_07','9_1_07','8_1_07','7_1_07','6_1_07','5_1_07','4_1_07','3_1_07','2_1_07','1_1_07','12_1_06','11_1_06','10_1_06','9_1_06','8_1_06','7_1_06','6_1_06','5_1_06','4_1_06','3_1_06','2_1_06','1_1_06','12_1_05','11_1_05','10_1_05','9_1_05','8_1_05','7_1_05','6_1_05','5_1_05','4_1_05','3_1_05','2_1_05','1_1_05','12_1_04','11_1_04','10_1_04','9_1_04','8_1_04','7_1_04','6_1_04','5_1_04','4_1_04','3_1_04','2_1_04','1_1_04','12_1_03','11_1_03','10_1_03','9_1_03','8_1_03','7_1_03','6_1_03','5_1_03','4_1_03','3_1_03','2_1_03','1_1_03','12_1_02','11_1_02','10_1_02','9_1_02','8_1_02','7_1_02','6_1_02','5_1_02','4_1_02','3_1_02','2_1_02','1_1_02','12_1_01','11_1_01','10_1_01','9_1_01','8_1_01','7_1_01','6_1_01','5_1_01','4_1_01','3_1_01','2_1_01','1_1_01','12_1_00','11_1_00','10_1_00','9_1_00','8_1_00','7_1_00','6_1_00','5_1_00','4_1_00','3_1_00','2_1_00','1_1_00');
 
//gather the topics
$query = "SELECT *, DATE_FORMAT(topic_create_time,  '%b %e %Y at %r') aS fmt_topic_create_time FROM db ORDER BY topic_create_time DESC";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
 
if (mysqli_num_rows($result) < 1) {
	//there are no topics, so say so
	$html = "<p><em>No topics exist.</em></p>";
} else {
 
 
// I would like to show this as percentage as well
  echo $rate;
		$html ='
		<table cellpadding="4" cellspacing="1" border="1" border-color="black" style="font-size:10px" >
		<tr>
			<th>Cash&nbsp;Flow&nbsp;Month_Year</th>
			<th>Number</th>
			 <th>Payment Period</th>
		<th>'.
				// convert months array to string with <th> nodes, remove the _1_ [month]
				preg_replace("/_1_/", "_", implode("</th>\n<th>",$months)) 
				. '</th>
		</tr>';
								
	 while ($row = mysqli_fetch_assoc($result)) {
	 
		$topic_id = $row['topic_id'];
		$topic_title = stripslashes($row['topic_title']);
		$topic_create_time = $row['fmt_topic_create_time'];
		$topic_owner = stripslashes($row['topic_owner']);
        $filename = stripslashes($row['filename']);
		$topic_id = $row['topic_id'];
		$topic_title = stripslashes($row['topic_title']);
		$topic_create_time = $row['fmt_topic_create_time'];
		$topic_owner = stripslashes($row['topic_owner']);
        $filename = stripslashes($row['filename']);
		$INV_ID = stripslashes($row['INV_ID']);
		$INV_U_N = stripslashes($row['INV_U_N']);
		$MS_F_NAME = stripslashes($row['MS_F_NAME']);
		$MS_ID = stripslashes($row['MS_ID']);
		$CHECK = stripslashes($row['CHECK']);
		$INV_ID = stripslashes($row['INV_ID']);
		$MS_INV_ID = stripslashes($row['MS_INV_ID']);
		$ID_CONT = stripslashes($row['ID_CONT']);
		$CONT_DIST_1_1_08 = stripslashes($row['CONT_DIST_1_1_08']);
		$cf_12_1_08 = stripslashes($row['cf_12_1_08']);
		$cf_11_1_08 = stripslashes($row['cf_11_1_08']);
		$cf_10_1_08 = stripslashes($row['cf_10_1_08']);
		$cf_9_1_08 = stripslashes($row['cf_9_1_08']);
		$cf_8_1_08 = stripslashes($row['cf_8_1_08']);
		$cf_7_1_08 = stripslashes($row['cf_7_1_08']);
		$cf_6_1_08 = stripslashes($row['cf_6_1_08']);
		$cf_5_1_08 = stripslashes($row['cf_5_1_08']);
		$cf_4_1_08 = stripslashes($row['cf_4_1_08']);
		$cf_3_1_08 = stripslashes($row['cf_3_1_08']);
		$cf_2_1_08 = stripslashes($row['cf_2_1_08']);
		$cf_1_1_08 = stripslashes($row['cf_1_1_08']);
		$cf_12_1_07 = stripslashes($row['cf_12_1_07']);
		$cf_11_1_07 = stripslashes($row['cf_11_1_07']);
		$cf_10_1_07 = stripslashes($row['cf_10_1_07']);
		$cf_9_1_07 = stripslashes($row['cf_9_1_07']);
		$cf_8_1_07 = stripslashes($row['cf_8_1_07']);
		$cf_7_1_07 = stripslashes($row['cf_7_1_07']);
		$cf_6_1_07 = stripslashes($row['cf_6_1_07']);
		$cf_5_1_07 = stripslashes($row['cf_5_1_07']);
		$cf_4_1_07 = stripslashes($row['cf_4_1_07']);
		$cf_3_1_07 = stripslashes($row['cf_3_1_07']);
		$cf_2_1_07 = stripslashes($row['cf_2_1_07']);
		$cf_1_1_07 = stripslashes($row['cf_1_1_07']);
		$cf_12_1_06 = stripslashes($row['cf_12_1_06']);
		$cf_11_1_06 = stripslashes($row['cf_11_1_06']);
		$cf_10_1_06 = stripslashes($row['cf_10_1_06']);
		$cf_9_1_06 = stripslashes($row['cf_9_1_06']);
		$cf_8_1_06 = stripslashes($row['cf_8_1_06']);
		$cf_7_1_06 = stripslashes($row['cf_7_1_06']);
		$cf_6_1_06 = stripslashes($row['cf_6_1_06']);
		$cf_5_1_06 = stripslashes($row['cf_5_1_06']);
		$cf_4_1_06 = stripslashes($row['cf_4_1_06']);
		$cf_3_1_06 = stripslashes($row['cf_3_1_06']);
		$cf_2_1_06 = stripslashes($row['cf_2_1_06']);
		$cf_1_1_06 = stripslashes($row['cf_1_1_06']);
		$cf_12_1_05 = stripslashes($row['cf_12_1_05']);
		$cf_11_1_05 = stripslashes($row['cf_11_1_05']);
		$cf_10_1_05 = stripslashes($row['cf_10_1_05']);
		$cf_9_1_05 = stripslashes($row['cf_9_1_05']);
		$cf_8_1_05 = stripslashes($row['cf_8_1_05']);
		$cf_7_1_05 = stripslashes($row['cf_7_1_05']);
		$cf_6_1_05 = stripslashes($row['cf_6_1_05']);
		$cf_5_1_05 = stripslashes($row['cf_5_1_05']);
		$cf_4_1_05 = stripslashes($row['cf_4_1_05']);
		$cf_3_1_05 = stripslashes($row['cf_3_1_05']);
		$cf_2_1_05 = stripslashes($row['cf_2_1_05']);
		$cf_1_1_05 = stripslashes($row['cf_1_1_05']);
		$cf_12_1_04 = stripslashes($row['cf_12_1_04']);
		$cf_11_1_04 = stripslashes($row['cf_11_1_04']);
		$cf_10_1_04 = stripslashes($row['cf_10_1_04']);
		$cf_9_1_04 = stripslashes($row['cf_9_1_04']);
		$cf_8_1_04 = stripslashes($row['cf_8_1_04']);
		$cf_7_1_04 = stripslashes($row['cf_7_1_04']);
		$cf_6_1_04 = stripslashes($row['cf_6_1_04']);
		$cf_5_1_04 = stripslashes($row['cf_5_1_04']);
		$cf_4_1_04 = stripslashes($row['cf_4_1_04']);
		$cf_3_1_04 = stripslashes($row['cf_3_1_04']);
		$cf_2_1_04 = stripslashes($row['cf_2_1_04']);
		$cf_1_1_04 = stripslashes($row['cf_1_1_04']);
		$cf_12_1_03 = stripslashes($row['cf_12_1_03']);
		$cf_11_1_03 = stripslashes($row['cf_11_1_03']);
		$cf_10_1_03 = stripslashes($row['cf_10_1_03']);
		$cf_9_1_03 = stripslashes($row['cf_9_1_03']);
		$cf_8_1_03 = stripslashes($row['cf_8_1_03']);
		$cf_7_1_03 = stripslashes($row['cf_7_1_03']);
		$cf_6_1_03 = stripslashes($row['cf_6_1_03']);
		$cf_5_1_03 = stripslashes($row['cf_5_1_03']);
		$cf_4_1_03 = stripslashes($row['cf_4_1_03']);
		$cf_3_1_03 = stripslashes($row['cf_3_1_03']);
		$cf_2_1_03 = stripslashes($row['cf_2_1_03']);
		$cf_1_1_03 = stripslashes($row['cf_1_1_03']);
		$cf_12_1_02 = stripslashes($row['cf_12_1_02']);
		$cf_11_1_02 = stripslashes($row['cf_11_1_02']);
		$cf_10_1_02 = stripslashes($row['cf_10_1_02']);
		$cf_9_1_02 = stripslashes($row['cf_9_1_02']);
		$cf_8_1_02 = stripslashes($row['cf_8_1_02']);
		$cf_7_1_02 = stripslashes($row['cf_7_1_02']);
		$cf_6_1_02 = stripslashes($row['cf_6_1_02']);
		$cf_5_1_02 = stripslashes($row['cf_5_1_02']);
		$cf_4_1_02 = stripslashes($row['cf_4_1_02']);
		$cf_3_1_02 = stripslashes($row['cf_3_1_02']);
		$cf_2_1_02 = stripslashes($row['cf_2_1_02']);
		$cf_1_1_02 = stripslashes($row['cf_1_1_02']);
		$cf_12_1_01 = stripslashes($row['cf_12_1_01']);
		$cf_11_1_01 = stripslashes($row['cf_11_1_01']);
		$cf_10_1_01 = stripslashes($row['cf_10_1_01']);
		$cf_9_1_01 = stripslashes($row['cf_9_1_01']);
		$cf_8_1_01 = stripslashes($row['cf_8_1_01']);
		$cf_7_1_01 = stripslashes($row['cf_7_1_01']);
		$cf_6_1_01 = stripslashes($row['cf_6_1_01']);
		$cf_5_1_01 = stripslashes($row['cf_5_1_01']);
		$cf_4_1_01 = stripslashes($row['cf_4_1_01']);
		$cf_3_1_01 = stripslashes($row['cf_3_1_01']);
		$cf_2_1_01 = stripslashes($row['cf_2_1_01']);
		$cf_1_1_01 = stripslashes($row['cf_1_1_01']);
		$cf_12_1_00 = stripslashes($row['cf_12_1_00']);
		$cf_11_1_00 = stripslashes($row['cf_11_1_00']);
		$cf_10_1_00 = stripslashes($row['cf_10_1_00']);
		$cf_9_1_00 = stripslashes($row['cf_9_1_00']);
		$cf_8_1_00 = stripslashes($row['cf_8_1_00']);
		$cf_7_1_00 = stripslashes($row['cf_7_1_00']);
		$cf_6_1_00 = stripslashes($row['cf_6_1_00']);
		$cf_5_1_00 = stripslashes($row['cf_5_1_00']);
		$cf_4_1_00 = stripslashes($row['cf_4_1_00']);
		$cf_3_1_00 = stripslashes($row['cf_3_1_00']);
		$cf_2_1_00 = stripslashes($row['cf_2_1_00']);
		$cf_1_1_00 = stripslashes($row['cf_1_1_00']);
							
		$seq = array( $cf_12_1_08,
		$cf_11_1_08,
		$cf_10_1_08,
		$cf_9_1_08,
		$cf_8_1_08,
		$cf_7_1_08,
		$cf_6_1_08,
		$cf_5_1_08,
		$cf_4_1_08,
		$cf_3_1_08,
		$cf_2_1_08,
		$cf_1_1_08,
		$cf_12_1_07,
		$cf_11_1_07,
		$cf_10_1_07,
		$cf_9_1_07,
		$cf_8_1_07,
		$cf_7_1_07,
		$cf_6_1_07,
		$cf_5_1_07,
		$cf_4_1_07,
		$cf_3_1_07,
		$cf_2_1_07,
		$cf_1_1_07,
		$cf_12_1_06,
		$cf_11_1_06,
		$cf_10_1_06,
		$cf_9_1_06,
		$cf_8_1_06,
		$cf_7_1_06,
		$cf_6_1_06,
		$cf_5_1_06,
		$cf_4_1_06,
		$cf_3_1_06,
		$cf_2_1_06,
		$cf_1_1_06,
		$cf_12_1_05,
		$cf_11_1_05,
		$cf_10_1_05,
		$cf_9_1_05,
		$cf_8_1_05,
		$cf_7_1_05,
		$cf_6_1_05,
		$cf_5_1_05,
		$cf_4_1_05,
		$cf_3_1_05,
		$cf_2_1_05,
		$cf_1_1_05,
		$cf_12_1_04,
		$cf_11_1_04,
		$cf_10_1_04,
		$cf_9_1_04,
		$cf_8_1_04,
		$cf_7_1_04,
		$cf_6_1_04,
		$cf_5_1_04,
		$cf_4_1_04,
		$cf_3_1_04,
		$cf_2_1_04,
		$cf_1_1_04,
		$cf_12_1_03,
		$cf_11_1_03,
		$cf_10_1_03,
		$cf_9_1_03,
		$cf_8_1_03,
		$cf_7_1_03,
		$cf_6_1_03,
		$cf_5_1_03,
		$cf_4_1_03,
		$cf_3_1_03,
		$cf_2_1_03,
		$cf_1_1_03,
		$cf_12_1_02,
		$cf_11_1_02,
		$cf_10_1_02,
		$cf_9_1_02,
		$cf_8_1_02,
		$cf_7_1_02,
		$cf_6_1_02,
		$cf_5_1_02,
		$cf_4_1_02,
		$cf_3_1_02,
		$cf_2_1_02,
		$cf_1_1_02,
		$cf_12_1_01,
		$cf_11_1_01,
		$cf_10_1_01,
		$cf_9_1_01,
		$cf_8_1_01,
		$cf_7_1_01,
		$cf_6_1_01,
		$cf_5_1_01,
		$cf_4_1_01,
		$cf_3_1_01,
		$cf_2_1_01,
		$cf_1_1_01,
		$cf_12_1_00,
		$cf_11_1_00,
		$cf_10_1_00,
		$cf_9_1_00,
		$cf_8_1_00,
		$cf_7_1_00,
		$cf_6_1_00,
		$cf_5_1_00,
		$cf_4_1_00,
		$cf_3_1_00,
		$cf_2_1_00,
		$cf_1_1_00,
		);
 
 
 
$variables = array(  'cf_12_1_08'=>1,
				 'cf_11_1_08'=>2,
				 'cf_10_1_08'=>3,
				 'cf_9_1_08'=>4,
				 'cf_8_1_08'=>5,
				 'cf_7_1_08'=>6,
				 'cf_6_1_08'=>7,
				 'cf_5_1_08'=>8,
				 'cf_4_1_08'=>9,
				 'cf_3_1_08'=>10,
				 'cf_2_1_08'=>11,
				 'cf_1_1_08'=>12,
				 'cf_12_1_07'=>13,
				 'cf_11_1_07'=>14,
				 'cf_10_1_07'=>15,
				 'cf_9_1_07'=>16,
				 'cf_8_1_07'=>17,
				 'cf_7_1_07'=>18,
				 'cf_6_1_07'=>19,
				 'cf_5_1_07'=>20,
				 'cf_4_1_07'=>21,
				 'cf_3_1_07'=>22,
				 'cf_2_1_07'=>23,
				 'cf_1_1_07'=>24,
				 'cf_12_1_06'=>25,
				 'cf_11_1_06'=>26,
				 'cf_10_1_06'=>27,
				 'cf_9_1_06'=>28,
				 'cf_8_1_06'=>29,
				 'cf_7_1_06'=>30,
				 'cf_6_1_06'=>31,
				 'cf_5_1_06'=>32,
				 'cf_4_1_06'=>33,
				 'cf_3_1_06'=>34,
				 'cf_2_1_06'=>35,
				 'cf_1_1_06'=>36,
				 'cf_12_1_05'=>37,
				 'cf_11_1_05'=>38,
				 'cf_10_1_05'=>39,
				 'cf_9_1_05'=>40,
				 'cf_8_1_05'=>41,
				 'cf_7_1_05'=>42,
				 'cf_6_1_05'=>43,
				 'cf_5_1_05'=>44,
				 'cf_4_1_05'=>45,
				 'cf_3_1_05'=>46,
				 'cf_2_1_05'=>47,
				 'cf_1_1_05'=>48,
				 'cf_12_1_04'=>49,
				 'cf_11_1_04'=>50,
				 'cf_10_1_04'=>51,
				 'cf_9_1_04'=>52,
				 'cf_8_1_04'=>53,
				 'cf_7_1_04'=>54,
				 'cf_6_1_04'=>55,
				 'cf_5_1_04'=>56,
				 'cf_4_1_04'=>57,
				 'cf_3_1_04'=>58,
				 'cf_2_1_04'=>59,
				 'cf_1_1_04'=>60,
				 'cf_12_1_03'=>61,
				 'cf_11_1_03'=>62,
				 'cf_10_1_03'=>63,
				 'cf_9_1_03'=>64,
				 'cf_8_1_03'=>65,
				 'cf_7_1_03'=>66,
				 'cf_6_1_03'=>67,
				 'cf_5_1_03'=>68,
				 'cf_4_1_03'=>69,
				 'cf_3_1_03'=>70,
				 'cf_2_1_03'=>71,
				 'cf_1_1_03'=>72,
				 'cf_12_1_02'=>73,
				 'cf_11_1_02'=>74,
				 'cf_10_1_02'=>75,
				 'cf_9_1_02'=>76,
				 'cf_8_1_02'=>77,
				 'cf_7_1_02'=>78,
				 'cf_6_1_02'=>79,
				 'cf_5_1_02'=>80,
				 'cf_4_1_02'=>81,
				 'cf_3_1_02'=>82,
				 'cf_2_1_02'=>83,
				 'cf_1_1_02'=>84,
				 'cf_12_1_01'=>85,
				 'cf_11_1_01'=>86,
				 'cf_10_1_01'=>87,
				 'cf_9_1_01'=>88,
				 'cf_8_1_01'=>89,
				 'cf_7_1_01'=>90,
				 'cf_6_1_01'=>91,
				 'cf_5_1_01'=>92,
				 'cf_4_1_01'=>93,
				 'cf_3_1_01'=>94,
				 'cf_2_1_01'=>95,
				 'cf_1_1_01'=>96,
				 'cf_12_1_00'=>97,
				 'cf_11_1_00'=>98,
				 'cf_10_1_00'=>99,
				 'cf_9_1_00'=>100,
				 'cf_8_1_00'=>101,
				 'cf_7_1_00'=>102,
				 'cf_6_1_00'=>103,
				 'cf_5_1_00'=>104,
				 'cf_4_1_00'=>105,
				 'cf_3_1_00'=>106,
				 'cf_2_1_00'=>107,
				 'cf_1_1_00'=>108,
);
	 
	 
$negativeVariables = array();
foreach ($seq as $key => $value) {
     if ($value <= 0) {
          $negativeVariables[$key] = $value;
}
}
	 
		
		//$period_to_solve = count($months); this needs to see the period associated with the date where the value of the row is negative and verify by equaling the hard code ID_CONT in the invdb database 
		
		// reset sum to 0 for each record??
		$period_to_solve = $row['ID_CONT'];
			
		//First sum all of the period there is info for!
 
		
		$html .= "
			<tr>
			<td>".$row['topic_create_time']."</td>
			<td>".$value."</td>
			<td>".$period_to_solve."</td>";
		
		foreach($months as $month){
			$html .="<td>".$row['cf_'.$month]."</td>\n";
		}
		$html .="</tr>";
	}
	
	$html .=	"</table>";
}
 
	
?>
<html>
<head>
<meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" /><meta name="keywords" content="Members Page">
<meta name="Description" content="Members Page">
<title>Member Page</title>
<link rel="stylesheet" type="text/css" media="screen" href="/images/style_032508.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/images/style_0325081.css" />
</head>
<div id="nav_top"><a href="/">Home</a> | <a href="/about">About Us</a> | <a href="/contact">Contact</a> | <a href="/site map">Site Map</a>| <a href="/login">Log Out</a></div>
<div id="bkg" style="width:auto;">
<div id="body" style="font-size:10px; ">
<p><span class="heading" style="width:600px; padding-top:20px;">View Your Internal Rate of Return</span></p>
<?php echo $html; ?></th>
<p>
<?php foreach($months as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?> 
<?php foreach($variables as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?> 
<?php foreach($seq as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?>
</p>
</div>
</div>
</body>
</html>

Open in new window

here is the page  http://bayparkcapital.com/IRCC_10-18-08-test.php
Thanks again for the help