1

I have a web form that allows users to enter in a quantity of an item that they would like to "collect" for a game.

When they click the "Submit" button, a php file is called to process the items and the quantities. The file queries a database table that stores the links that users follow to "collect" the items.

The link table is setup similar to link_d | timestamp | item_id | link

What I'm wanting to do is check the number of available links in the table against what the user requested and see if there are enough links for the request. If there are not, then I want to change the quantity requested to the number of available links. I'm wondering what would be the best way to go about that.

Right now I'm thinking about something like

for($counter=0;$counter<count($items_name);$counter++)
{
if($items_qty > $qtyavail = "select count(links.link) from links inner join items on links.item_id = items.item_id where items.name = '".$items_name[$counter]."' ;")
    {
    $items_qty = $qtyavail
    }
}

As the number of available links will change over time, I'm needing the current number available in the table when the user clicks the Submit button.

Any thoughts/suggestions?

3
  • Why not use a min function on the number they request and what's available to them? Then just process that number as safe. Commented Jun 21, 2013 at 20:53
  • That is not how PHP interfaces with MySQL... at all. Commented Jun 21, 2013 at 20:59
  • why do you need to check available links against request? if i ask for 10 link and only 5 in database what happens? possible mysql Limit $requests maybe Commented Jun 21, 2013 at 20:59

2 Answers 2

1

You can get the lesser of the two values via the MySQL LEAST() function:

"SELECT LEAST( links, " . int($items_name[$counter]) . " ) as selected_quantity FROM ..."

This will return the number of items the user selected, or the number of links avaialble, whichever is fewer.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried changing the select query to use the Least() comparison, but it didn't seem to change the number of rows returned only the value of that column. I'm not sure that it applies to what I'm trying to accomplish. But thank you for at least offering up something to try instead of simply saying "That won't work"!
0

So what I wound up doing to solve the particular problem on how to get the number of links available that met the criteria that I wanted and compare it to the quantity that the user had requested was to do the following...

  1. Create a whole new .php file that ran the query to get the number of available links in the database.

numLinks.php

    <?php 
      require_once('phpConfig.php') ;
      require_once('myerror_handler.php') ;

      $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) ;

      $numLinks = "select count(tbl_links.link) AS Count...; " ;

      $result = $mysqli->query($linksavail) ;

      if ($result) {     
          while ($row = $result->fetch_array(MYSQLI_ASSOC)) 
          {
                $numAvail = $row['Count'] ;
          }
      }

      return $numAvail ;
    ?>
  1. Then included that file in the PHP file handling the bulk of the processing.

Process.php

    <?php
      require_once('phpConfig.php') ;
      require_once('myerror_handler.php') ;

      $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) ;

      //get all the input data from jquery on clicking submit button
      $items_name=$_POST['inputnames'];
      $items_values=$_POST['valuesitems'];

      //loop for each name
            for($counter=0;$counter<count($items_name);$counter++)
            {
              include('numLinks.php') ;

              $requested = $items_values[$counter] ;

              if ($items_values[$counter] > $numavail) {
                          $items_values[$counter] = $numavail ;
              }

              echo $items_name[$counter].' -- '.$numavail.' Available -- .$requested.' Requested' ;
            }
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.