Jump to content
funbinod

getting max value in php

Recommended Posts

hello all!

i was trying to get max value from a mysql query derived values

but i could not succeed. please guide what i made mistake.

 

below is the code i tried. in this after fetching data from mysql table, i made some calculations and derived data like this

////////////////// 7474.776.89578.3 //////////////////////////

which actually contains 5 values ( 74,74.7,76.8,95,78.3 )

what i want is to select the number 95.

what is the proper method?

thank u in advance.

$cid = 2;
$subid = 10;
$clas = 'PG';
$highmq = $mysqli->query("SELECT marktheory, markpractical FROM marksheet WHERE studentid IN (SELECT fullid FROM students WHERE class='$clas' AND cid='$cid') AND (marktheory LIKE '%#$subid#%' OR markpractical LIKE '%#$subid#%') AND cid='$cid'");
$highmarkth = array();
for ($i = 0; $i < $highmq->num_rows; $i++) {
	$rows = $highmq->fetch_assoc();
	if ($rows['marktheory'] != "") {
		$highmarkth = explode("#", $rows['marktheory'])[2];
		$highmarkpr = 0;
	}
	if ($rows['markpractical'] != "") {
		$highmarkpr = explode("#", $rows['markpractical'])[2];
		$highmarkth = 0;
	}
	$highmark = ($highmarkth + $highmarkpr);
	echo max($highmark);
  }

 

Share this post


Link to post
Share on other sites

I don't know where the numbers are being produced. If all you had was a string like this "7474.776.89578.3" it would be impossible to separate the numbers since nobody knows where one number ends and the next begins.

Share this post


Link to post
Share on other sites

thank u @Ingolme for the reply!

the exactly is my problem. the result i want is derived as string not as an array. as i mentioned above the result i got "7474.776.89578.3" contains FIVE values "74, 74.7, 76.8, 95, 78.3". i wish them as array. and then, i think, the "max()" function will do the rest.

Share this post


Link to post
Share on other sites

Where did that string come from? Information has been lost and is irrecoverable, you have to get the missing information from the original source that created the string.

  • Like 1

Share this post


Link to post
Share on other sites
7 hours ago, Ingolme said:

Where did that string come from? Information has been lost and is irrecoverable, you have to get the missing information from the original source that created the string.

the all i did is the same what i've kept in my question above.

i'm putting the code again here.

from the query i get values from columns "marktheory" and "markpractical", then calculate them and try to get highest value. in the mentioned code, i expect the values to be in array in "$highmark". when i echo it, i get the value altogether without commas (as i explained above).

$cid = 2;
$subid = 10;
$clas = 'PG';
$highmq = $mysqli->query("SELECT marktheory, markpractical FROM marksheet WHERE studentid IN (SELECT fullid FROM students WHERE class='$clas' AND cid='$cid') AND (marktheory LIKE '%#$subid#%' OR markpractical LIKE '%#$subid#%') AND cid='$cid'");
$highmarkth = array();
for ($i = 0; $i < $highmq->num_rows; $i++) {
	$rows = $highmq->fetch_assoc();
	if ($rows['marktheory'] != "") {
		$highmarkth = explode("#", $rows['marktheory'])[2];
		$highmarkpr = 0;
	}
	if ($rows['markpractical'] != "") {
		$highmarkpr = explode("#", $rows['markpractical'])[2];
		$highmarkth = 0;
	}
	$highmark = ($highmarkth + $highmarkpr);
	echo max($highmark); /////////// gives error as the $highmark is not an array
    echo $highmark //////////////// gives "7474.776.89578.3" /////// i wish this to b as "74,74.7,76.8,95,78.3"
  }

 

thank you.

Share this post


Link to post
Share on other sites

Can you provide an example of what is in the marktheory and markpractical fields?

Share this post


Link to post
Share on other sites
4 minutes ago, Ingolme said:

Can you provide an example of what is in the marktheory and markpractical fields?

they are varchar field and contain 3 types of data on them. the datas are saperated by a hash (#) key. for example "ET001#10#74.5". From this i explode the value and get the last offset for calculation

 

thank you

Share this post


Link to post
Share on other sites

Your code is kind of inconsistent.  This looks problematic:

	if ($rows['marktheory'] != "") {
		$highmarkth = explode("#", $rows['marktheory'])[2];
		$highmarkpr = 0;
	}
	if ($rows['markpractical'] != "") {
		$highmarkpr = explode("#", $rows['markpractical'])[2];
		$highmarkth = 0;
	}
	$highmark = ($highmarkth + $highmarkpr);

For example, you don't define those 2 variables outside of those if statements, so they will only be defined if either of those if statements match.  Then, you set one of them to an array with explode, and the other variable to a number.  Then you try to add an array and number.  What do you think that's going to do, when you add an array and a number?  In fact, what do you expect to happen when you use the + on an array at all?  The data type of those variables isn't consistent, if it goes in the first if statement then $highmarkth will be an array, but if it goes in the second one then it will be a number.  So after those 2 if statements either of those variables could be either an array or a number, or they could both be undefined.  Then you try to add them.  Also, if it goes in the second if statement then both variables get set, so that will overwrite anything you did in the first if statement.  So if markpractical is not empty then those 2 variables get set right there regardless of what marktheory is set to.  If both marktheory and markpractical are set then the values that were set in the first if block will get overwritten by the second block.

  • Like 1

Share this post


Link to post
Share on other sites

It seems that in some cases, the variable is being passed over from the previous loop iteration because one of the fields is an empty string and you're not initializing it inside the loop. You also are initializing $highmarkth as an array but the converting it to a number.

Perhaps this may suit your requirements better:

$highmark = [];
for ($i = 0; $i < $highmq->num_rows; $i++) {
  $rows = $highmq->fetch_assoc();

  // Initialize theory and practical marks
  $highmarkpr = 0;
  $highmarkth = 0;
  
  $data = explode("#", $rows['marktheory']);
  if(isset($data[2])) {
    $highmarkth = (float) $data[2];
  }

  $data = explode("#", $rows['markpractical']);
  if(isset($data[2])) {
    $highmarkpr = (float) $data[2];
  }

  // Sum the two and add the result to the list of high marks
  $highmark[] = $highmarkth + $highmarkpr;
}

echo implode(',', $highmark);

For security you should be using prepared statements if you have variables in the query.

 

Share this post


Link to post
Share on other sites
9 hours ago, justsomeguy said:

Your code is kind of inconsistent.  This looks problematic:

For example, you don't define those 2 variables outside of those if statements, so they will only be defined if either of those if statements match.  Then, you set one of them to an array with explode, and the other variable to a number.  Then you try to add an array and number.  What do you think that's going to do, when you add an array and a number?  In fact, what do you expect to happen when you use the + on an array at all?  The data type of those variables isn't consistent, if it goes in the first if statement then $highmarkth will be an array, but if it goes in the second one then it will be a number.  So after those 2 if statements either of those variables could be either an array or a number, or they could both be undefined.  Then you try to add them.  Also, if it goes in the second if statement then both variables get set, so that will overwrite anything you did in the first if statement.  So if markpractical is not empty then those 2 variables get set right there regardless of what marktheory is set to.  If both marktheory and markpractical are set then the values that were set in the first if block will get overwritten by the second block.

thank you @justsomeguy for the reply. your reply helped me to understand some new things. it is fruitful.

i want to explain some more about my mysql columns. in the table marksheet, i insert data to only one column at one time. this means in each row, if it has data on marktheory then markpractical is surely empty. and if markpractical has some data in it, then marktheory is surely blank. so now do you think it would be ok if i set $highmarkth and $highmarkpr outside the if statements and set $higmark as an array outside the for loop?

 

9 hours ago, Ingolme said:

It seems that in some cases, the variable is being passed over from the previous loop iteration because one of the fields is an empty string and you're not initializing it inside the loop. You also are initializing $highmarkth as an array but the converting it to a number.

Perhaps this may suit your requirements better:


$highmark = [];
for ($i = 0; $i < $highmq->num_rows; $i++) {
  $rows = $highmq->fetch_assoc();

  // Initialize theory and practical marks
  $highmarkpr = 0;
  $highmarkth = 0;
  
  $data = explode("#", $rows['marktheory']);
  if(isset($data[2])) {
    $highmarkth = (float) $data[2];
  }

  $data = explode("#", $rows['markpractical']);
  if(isset($data[2])) {
    $highmarkpr = (float) $data[2];
  }

  // Sum the two and add the result to the list of high marks
  $highmark[] = $highmarkth + $highmarkpr;
}

echo implode(',', $highmark);

For security you should be using prepared statements if you have variables in the query.

and thank you too @Ingolme . your suggested code guided me very nicely. it gave the data with commas in it. but one problem more occurred. when i use max() function, it says it is not an array (Warning: max(): When only one parameter is given, it must be an array in J:\xampp\htdocs\is-cool\test.php on line 23). and i used just mysqli->query in stead of prepared statement because it is executed internally. it has no user submitted data. wouldn't it be ok?

Share this post


Link to post
Share on other sites

Where did you use the max() function?

Share this post


Link to post
Share on other sites
4 hours ago, Ingolme said:

Where did you use the max() function?

...........
...........
for ($i = 0; $i < $highmq->num_rows; $i++) {
  ..........
  ..........
  ..........
  $highmarks[] = ($highmarkth + $highmarkpr);
}
$highmark = implode(',', $highmarks);
echo max($highmark);

here!

Share this post


Link to post
Share on other sites

$highmark is a string. Do not implode $highmarks.

  • Like 1

Share this post


Link to post
Share on other sites
15 hours ago, Ingolme said:

$highmark is a string. Do not implode $highmarks.

great! i got the point and this solved my problem. thank you.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...