Web Analytics Made Easy -
StatCounter find biggest number - CodingForum

Announcement

Collapse
No announcement yet.

find biggest number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • find biggest number

    I think I sort of know whats causing this problem, but i don't know a way around it, heres my code:
    PHP Code:
    $threadSql mysql_query("SELECT * FROM `thread` WHERE `boardid` = '$id'");
        while (
    $thread mysql_fetch_array($threadSqlMYSQL_ASSOC))
        {
        
    $newestpost $thread["newestpost"];
        
        
    $blank .= $newestpost.", ";
        }

        
    $blankc substr($blank0, -2);
        
    $top max($blankc);
        echo(
    '<p>'.$top.''); 
    Matt Davenport - Freelance Web Developer

  • #2
    Originally posted by tribalmaniac View Post
    I think I sort of know whats causing this problem, but i don't know a way around it, heres my code:
    What problem? Explain to us in detail what is happening. We aren't psychic.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

    Comment


    • #3
      oops sorry i thought my title almost described it

      i'm trying to find the biggest number out of all the entries in the database in field "newestpost"
      Matt Davenport - Freelance Web Developer

      Comment


      • #4
        Right but are you getting any errors, what is the order being returned? What is the setup of your newestpost entries?
        ||||If you are getting paid to do a job, don't ask for help on it!||||

        Comment


        • #5
          newestpost is just a seies of numbers as in one may be 1 another 2 another 3 and so on

          they come out as 1, 2, 3, 4

          and the error i get is

          Warning: Wrong parameter count for max() in /home/.mala/tribalmaniac/theflashplace.com/thread.php on line 271
          Matt Davenport - Freelance Web Developer

          Comment


          • #6
            max takes at least two arguments, you're passing it a single string:
            max(1,2,3);
            isn't the same as
            max('1,2,3');

            Now, you can do what you want in SQL:
            SELECT newestpost FROM thread WHERE boardid=$id ORDER BY newestpost DESC LIMIT 1;

            or, rather than concatenating to a string, build up an array (which max will understand)
            $blank[]=$newestThread;

            [edit] for completeness,
            if max didn't take an array, and you needed to pass it a number of parameters, then you could use php's call_user_func_array function to do what you were trying to do:
            an example:
            PHP Code:
            $numbers=array(1,5,8,4,2);
            $max=call_user_func('max',$numbers);
            //gives the same result as 
            $max($numbers);  //because max() can deal with arrays
            //but is actually doing:
            $max=max(1,5,8,4,2); 
            Last edited by GJay; Oct 3, 2006, 05:30 PM.
            My thoughts on some things: http://codemeetsmusic.com
            And my scrapbook of cool things: http://gjones.tumblr.com

            Comment


            • #7
              sorry if i sound really dumb, but i now have this:

              PHP Code:
              $blankarray[i]=$newestpost;
              echo 
              $blankarray[i];
              $i $i+1;

              in the SQL statement and then this afterwards:

              PHP Code:
              $top max($blankarray);
              echo(
              '<p>'.$top.''); 
              the outcome is giving me:

              312

              2


              when it should be:

              312

              3
              Last edited by tribalmaniac; Oct 3, 2006, 06:16 PM.
              Matt Davenport - Freelance Web Developer

              Comment


              • #8
                why change what I suggested? try:
                PHP Code:
                $blankarray[]=$newestpost
                the square brackets with no index just mean it gets added as the next element in the array.
                (the SQL method is simpler and quicker)
                My thoughts on some things: http://codemeetsmusic.com
                And my scrapbook of cool things: http://gjones.tumblr.com

                Comment


                • #9
                  Originally posted by GJay View Post
                  why change what I suggested? try:
                  PHP Code:
                  $blankarray[]=$newestpost
                  the square brackets with no index just mean it gets added as the next element in the array.
                  (the SQL method is simpler and quicker)
                  ahhh sorry, I mainly use flash and the 'i' method is my usual approach

                  that sorted it though, thanks so much
                  Matt Davenport - Freelance Web Developer

                  Comment


                  • #10
                    You say you are getting the data from a database. Do you mean the series of numbers you refer to are in a column, one number per row of that column and you want the row returned with every column where boardid has the highest value? or are you passing it that outside string of numbers (1,2,3,4) etc?

                    I'm asking because if you are using the database you should let the database do the calculation for you, there is no need to do it outside the database.

                    If the numbers you are searching are already in the database column boardid then do this:

                    Code:
                    select
                    foo,
                    bar,
                    qux,
                    othercolumnanmes
                    from yourtable
                    where boardid = (select max(boardid) from yourtable)

                    For the other method let us say you have boardid numbers of
                    1,2,3,4,7,8,9 in your database, but you don't know those numbers. You want to pass a string of numbers, let us say that string is 2,3,4,6 and you want the row from your table containing the largest value for boardid, in this case 4, (because a row with boardid of 6 doesn't exist) then revise the query to:

                    Code:
                    select
                    foo,
                    bar,
                    qux,
                    othercolumnanmes
                    from yourtable
                    where boardid = 
                    (select max(boardid) from yourtable where boardid IN (2,3,4,6))

                    Comment


                    • #11
                      If you just want to get the highest number in newestpost then

                      PHP Code:
                      $sql "SELECT MAX(newestpost) as top FROM `thread` WHERE `boardid` = '$id'";
                      if(
                      $result mysql_query($sql)
                      {
                         if(
                      $row mysql_fetch_assoc($result)
                           {
                              echo(
                      '<p>'.$row['top'].'');
                            }
                       }
                      else
                      {
                         die(
                      mysql_error());

                      should do the trick
                      http://www.hazelryan.co.uk

                      Comment

                      Working...
                      X