Jump to content

min max in every iqid


abs0lut

Recommended Posts

<?php 
session_start();
include 'config.php';
$post = $_GET['post'];
$slected = array();
	if(isset($_POST['checkb'])){
		if(count($_POST['sa']) > 0){			
			$do2 = mysql_query("UPDATE ptable SET status='updated' WHERE id='$post'") or die(mysql_error());
			if($do2){
			echo 'updated';
			}
			foreach($_POST['sa'] as $awrd) {
        
			$result = mysql_query("UPDATE ctable SET awarded='yes' WHERE id='$awrd'") or die(mysql_error());
		   		$slected[] = $awrd;
				if($result){
				echo 'awarded'.$usersid;
				}
    			}		
		}			
	} else{
$result = mysql_query("SELECT status FROM ptable WHERE id=$post") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$status=$row['status'];
}
if($status!='updated'){
?>   
<form method="post" name="formk" action="<?= $_SERVER['PHP_SELF']. '?' . $_SERVER['QUERY_STRING'] ?>">
<?php
$result = mysql_query("SELECT cid, iqid, cost, usersid FROM ctable WHERE post=$post");
while ($row = mysql_fetch_array($result))
{
  $arr[$row['iqid']][$row['usersid']] = $row['cost'];
  $arr2[$row['iqid']][$row['usersid']] = $row['cid'];
  $uid[] = $row['usersid'];
}
$uid = array_unique($uid);
$qid = array_keys($arr);

$query = "SELECT MIN(cost), MAX(cost) FROM ctable WHERE post=$post"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$min=$row['MIN(cost)'];
$max=$row['MAX(cost)'];
}
echo '<table><tr><td></td>';
foreach($uid as $userid) echo "<td>$userid</td>";
echo '</tr>';
foreach($qid as $iqid)
{
  echo "<tr><td>$iqid</td>";
  foreach($uid as $userid)
  {
    if (isset($arr[$iqid][$userid]))
    {
        if($min==$arr[$iqid][$userid]){
	echo "<td bgcolor=\"#FFFF00\">".$arr[$iqid][$userid]."<input type='checkbox' name='sa[]' value='{$arr2[$iqid][$userid]}'></td>";
	}else if($max==$arr[$iqid][$userid]){
	echo "<td bgcolor=\"#00FF00\">".$arr[$iqid][$userid]."<input type='checkbox' name='sa[]' value='{$arr2[$iqid][$userid]}'></td>";
	}else{
	echo "<td>".$arr[$iqid][$userid]."<input type='checkbox' name='sa[]' value='{$arr2[$iqid][$userid]}'></td>";
    	}
}
else echo "<td></td>";	
  }
  echo "</tr>";
}
echo '</table>';
echo '<input type="submit" name="checkb" />';
echo'</form>';	
}
}
?>

i want the lowest cost in every iqid to have background color #FF0000

any help would be appreciated..

Link to comment
https://forums.phpfreaks.com/topic/115786-min-max-in-every-iqid/
Share on other sites

well I didn't read through your entire code, but you will need something like this (I made up the variable names)

 

if ( min( $rows ) == $current_element )  { print '<span style="color:red">' . $current_element . '</span>'; }

 

$rows would be a set of data retrieved from the db stored as an array, and $current_element would be the variable "as" in a foreach loop.

It may be easier to add another field within your array called 'lowest' (and 'highest' if you needed one). As you place entries into the array, you just keep a record of which value is lower so that (eg) if ($value < $lastvalue) { $lastvalue = $value; $lowestID = $itemID; }

 

Once the array is created, you alter the 'lowest' entry for the $itemID you collected; make the 'lowest' 1 rather than 0. Then use that definition for changing the colour of your text.

 

There is likely a more efficient way of doing things.. but see how you go with this.

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.