Comparing Array Values and Returning A Certain Value

Discussion in 'PHP' started by Ipodman79, Jul 4, 2014.

  1. #1
    Hi,

    Here's what I have. Let's say an egg counter application. I have two columns in a database, current and alert.

    'current' will be the current amount the user has. 'alert' will be an amount that if the 'current' amount is less than or equal too it will echo 'something'.

    I was thinking of using two arrays and comparing them. However I'm not very good with for or while loops. Is there any way of doing this or is there a better way.

    Thanks
     
    Ipodman79, Jul 4, 2014 IP
  2. chamika weerasinghe

    chamika weerasinghe Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    use this
    SELECT * FROM table WHERE alert < current;
     
    Last edited: Jul 4, 2014
    chamika weerasinghe, Jul 4, 2014 IP
  3. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    If I have more than one entry that the current number is less than the alert number can I say,

    And then will I be able to list the egg types that are lower than the alert number?
     
    Ipodman79, Jul 4, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Lower than, or equal to, yes
     
    PoPSiCLe, Jul 4, 2014 IP
  5. chamika weerasinghe

    chamika weerasinghe Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5


    yes you can get 'egg_type' column where alert is less than or equal to current

    SELECT 'egg_type' FROM table WHERE alert <= current;
     
    chamika weerasinghe, Jul 4, 2014 IP
  6. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #6
    How do I echo the results then. I have;

    
    // Making the query for displaying 
    $q2 = "SELECT `item` FROM `main` WHERE `alert` <= `current` ORDER BY `id` ASC";
    $r2 = @mysqli_query($dbc, $q2); // Running the query
    
    // Counting the MySQL rows
    $num2 = mysqli_num_rows($r2);
    
    $row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC);
    
    // Creating the alert_echo funcion
    function alert_echo () {
        foreach ($row2 as $value) {
          echo "$value <br>";
        }
    }
    
    PHP:
    I then call the function. This however doesn't return anything.
     
    Ipodman79, Jul 4, 2014 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    $row2 does not exist in the alert_echo function. Look at this page for information on variable scope:

    http://www.php.net/manual/en/language.variables.scope.php

    You will need to pass $row2 to the function like: alert_echo($row2)

    Additionally never use the @ infront of mysqli_query, mysqli_query will return an error only if the query is invalid or connection lost and other cases which should be caught in your statement.
     
    ThePHPMaster, Jul 4, 2014 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Some advice:

    1) STOP making extra variables for nothing; waste of memory

    2) STOP fetching the entire result set. waste of memory and performance draining garbage. You need to pass it, pass the mysqli_result

    3) STOP using the procedural version, it introduces extra overhead and really should never have been allowed to exist in the first place.

    4) STOP using such needlessly cryptic variable names. Try describing things with your vars.

    5) on your output it should be just dumping out a bunch of '1' -- as you are fetching associative meaning the result is an array of arrays indexed as 'item'.

    ... and one question, why are you putting the output in a function? You use the term 'alert' -- that raises some worries about where/when you are trying to use it.

    $itemAlertResult = $dbc->query('
    	SELECT item FROM main
    	WHERE alert <= current
    	ORDER BY id ASC
    ');
    
    // no clue why you were putting the output in a function
    
    while ($value = $itemAlertResult->fetch_assoc()) {
    	echo $value['item'], '<br />';
    }
    Code (markup):
     
    deathshadow, Jul 4, 2014 IP
  9. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #9
    I'm putting it in a function because I want to call the function later on down the page.
     
    Ipodman79, Jul 5, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Then either put the query in the function, or make the result global and access it as such... I'd just go with the former... Well, unless you're showing it more than once per page. (which is usually a bad idea anyways).

    Or again, pass the result to the function or the access the result as global, instead of using ::fetchAll.
     
    deathshadow, Jul 5, 2014 IP