Jump to content

Checking if Variable contains a number at line 1


Modernvox

Recommended Posts

Hi Guyz,

 

I need to check the variable $itemDescription to see if the client has entered a '1' before the item description(example: 1 table)

 

Reason, is the client would like to add sales tax to all NOn-Food related items of 6.25%

 

This is for a local auction house. It needs to be simple. i,e. Don't wanna add any more form fields. Just let the user type the number 1 before all items that need to be taxed.

 

Here's my code:

 <?php
ob_start();
?>
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">

</style>
</head>

<?PHP session_start();
ob_start();
# Config settings below
# Change these to whatever you want 
#############################

$cellsPerRow = '6';

#############################
include('connect.php');

// These queries get the amount of bidders that are in the database
$getAllBidders = mysql_query("SELECT `id` FROM bidders");
$totalBidders  = mysql_num_rows($getAllBidders);


// This section is to check if the Add New Item form has been submitted
  if($_POST['addNewItem']) {
    // Retrieve Query Strings from URL
    $itemDescription = $_POST['itemDescription'];
    $itemPrice       = $_POST['itemPrice'];
    $winningBidder   = $_POST['winningBidder'];
    $totalDeals      = $_POST['totalDeals'];
    $totalPrice      = ($totalDeals*$itemPrice);

    // Check the submitted data and make sure all is correct
    if(!$itemDescription) {
      $message = 'You must enter an Item Description.';
    } else if(!$itemPrice) {
      $message = 'You must enter an Item Price.';
    } else if(!$winningBidder) {
      $message = 'You must enter a Winning Bidder ID.';
    } else if(!$totalDeals) {
      $message = 'You must enter the amount of Deals.';
    } else if(!is_numeric($winningBidder)) {
      $message = 'The Winning Bidder ID can only be numbers.';
    } else {

      // Check to see if the bidder ID already exists
      $checkBidder = mysql_query("SELECT * FROM bidders WHERE biddersId='$winningBidder' LIMIT 1");
      $checkBidder = mysql_fetch_assoc($checkBidder);

      // If the Bidder ID does not exist, we re-direct to allow us to save the Bidder ID
      if(!$checkBidder) {

        header("Location: ?action=confirmListing&iDesc=".$itemDescription."&iPrice=".$itemPrice."&wBidder=".$winningBidder."&tDeals=".$totalDeals."");

      } else { 

        // If Bidder ID exists we just insert the transaction accordingly
        mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `bidderId`, `itemQty`, `totalPrice`) VALUES ('$itemDescription', '$itemPrice', '$winningBidder', '$totalDeals', '$totalPrice');");
        $message1 = 'The transaction has been successfully added.';

mysql_query("SELECT owed From bidders WHERE biddersId='$winningBidder'") or die(mysql_error()); 
if ($row['owed']==0) {
mysql_query("UPDATE bidders SET owed='1' WHERE biddersId='$winningBidder'") or die(mysql_error());  
}
      }

    }

  }

// This section is to check if the Add Bidder to database form has been submitted
  if($_POST['confirmBidder']) {
    $itemDescription = $_POST['itemDescription'];
    $itemPrice       = $_POST['itemPrice'];
    $winningBidder   = $_POST['winningBidder'];
    $totalDeals      = $_POST['totalDeals'];
    $totalPrice      = ($itemPrice*$totalDeals);
    $addBidder       = $_POST['addBidder'];
$checkInput= preg_match([1]+*);


if ($itemDescription== $checkInput)
$itemPrice= $itemPrice * 6.25%;

    mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `bidderId`, `itemQty`, `totalPrice`) VALUES ('$itemDescription', '$itemPrice', '$winningBidder', '$totalDeals', '$totalPrice');");
    $message1 = 'The transaction has been successfully added.';

   
    if($addBidder == 'Yes') {
      mysql_query("INSERT INTO bidders (biddersId) VALUES ('$winningBidder');");
      $message1 .= '<br> The Bidder ID has also been added.';
mysql_query("SELECT owed From bidders WHERE biddersId='$winningBidder'") or die(mysql_error()); 
if ($row['owed']==0) {
mysql_query("UPDATE bidders SET owed='1' WHERE biddersId='$winningBidder'") or die(mysql_error());  
}
    }
      
if($addBidder == 'No') {
                 $message1 = '<br><font color= "red"> Bidder has NOT been logged.</font>';
               }

   // $itemDescription = '';
   // $itemPrice       = '';
   // $winningBidder   = '';
   //  $totalDeals      = '';
   // $totalPrice      = '';
   // $addBidder       = '';
      }

        

   



?>


<?PHP
  // This line of code will check the current task we are doing
  if($_GET['action'] == 'confirmListing') { 
    $itemDescription = $_GET['iDesc'];
    $itemPrice       = $_GET['iPrice'];
    $winningBidder   = $_GET['wBidder'];
    $totalDeals      = $_GET['tDeals'];
?>

<form name="confirmBidder" method="POST" action="?">

<?PHP
// This is the hidden data from the previous form
// Better than using sessions and GET query
?>
<input type="hidden" name="itemDescription" id="itemDescription" value="<?PHP echo $itemDescription;?>">
<input type="hidden" name="itemPrice" id="itemPrice" value="<?PHP echo $itemPrice;?>">
<input type="hidden" name="winningBidder" id="winningBidder" value="<?PHP echo $winningBidder;?>">
<input type="hidden" name="totalDeals" id="totalDeals" value="1">

<table cellpadding="0" cellspacing="1" border="0" style="background: #ffffff; margin: auto;">
<tr>
  <td colspan="2" class="formHeader"> Add New Auction Item </td>
</tr>
<tr>
  <td class="formField"> Add This Bidder? </td>
  <td class="formValue"> <select name="addBidder" id="addBidder" class="input" style="width: 195px;"> <option value="Yes">Yes</option> <option value="No">No</option></select> </td>
</tr>
<tr>
  <td colspan="2" class="formButton">
<input type="submit" name="confirmBidder" id="confirmBidder" value="Confirm Bidder Action?"> 
<input type="reset" name="reset" id="reset" value="Reset Form">
  </td>
</tr>
</table>
</form>

<?PHP
  // If the action is not to confirm a listing then show add item section
  } else {
?>

<?PHP//----- Add New Item Section -----\\?>

<?PHP 
  if($message) {
    echo '<div class="Error">',$message,'</div><div style="height: 10px;"></div>';
  } else if($message1) {
    echo '<div class="Success">',$message1,'</div><div style="height: 10px;"></div>';
  }
?>

<form name="addNewItem" method="POST" action="?">
<table cellpadding="0" cellspacing="1" border="0" style="background: #ffffff; margin: auto;">
<tr>
  <td colspan="2" class="formHeader"> Add New Auction Item </td>
</tr>
<tr>
  <td class="formField"> Item Description </td>
  <td class="formValue"> <input type="text" name="itemDescription" class="formText" id="itemDescription" value="<?=$itemDescription;?>" style="width: 195px;"> </td>
</tr>
<tr>
<td class="formField"> Item Price </td>
  <td class="formValue"> <input type="text" name="itemPrice" class="formText" id="itemPrice" value="<?=$itemPrice;?>" style="width: 195px;"> </td>
</tr>
<tr>
  <td class="formField"> Winning Bidder ID </td>
  <td class="formValue"> <input type="text" name="winningBidder" class="formText" id="winningBidder" style="width: 195px;"> </td>
</tr>
<tr>
  <td class="formField"> How Many Deals? </td>
  <td class="formValue"> <input type="text" name="totalDeals" class="formText" id="totalDeals" value="1" style="width: 195px;"> </td>
</tr>
<tr>
  <td colspan="2" class="formButton">
<input type="submit" name="addNewItem" id="addNewItem" value="Add New Auction Item?"> 
<input type="reset" name="reset" id="reset" value="Reset Form">
  </td>
</tr>
</table>
</form>

<?PHP//----- Add New Item Section -----\\?>

<?PHP } ?>

 

All help appreciated to the fullest...

 

 

 

 

Link to comment
Share on other sites

I need to check the variable $itemDescription to see if the client has entered a '1' before the item description(example: 1 table)

 

<?php
if($itemDescription[0] === "1") {
    //Do whatever
}

 

Added with no luck

<?php
ob_start();
?>
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">

</style>
</head>

<?PHP session_start();
ob_start();
# Config settings below
# Change these to whatever you want 
#############################

$cellsPerRow = '6';

#############################
include('connect.php');

// These queries get the amount of bidders that are in the database
$getAllBidders = mysql_query("SELECT `id` FROM bidders");
$totalBidders  = mysql_num_rows($getAllBidders);


// This section is to check if the Add New Item form has been submitted
  if($_POST['addNewItem']) {
    // Retrieve Query Strings from URL
    $itemDescription = $_POST['itemDescription'];
    $itemPrice       = $_POST['itemPrice'];
    $winningBidder   = $_POST['winningBidder'];
    $totalDeals      = $_POST['totalDeals'];
    $totalPrice      = ($totalDeals*$itemPrice);

    // Check the submitted data and make sure all is correct
    if(!$itemDescription) {
      $message = 'You must enter an Item Description.';
    } else if(!$itemPrice) {
      $message = 'You must enter an Item Price.';
    } else if(!$winningBidder) {
      $message = 'You must enter a Winning Bidder ID.';
    } else if(!$totalDeals) {
      $message = 'You must enter the amount of Deals.';
    } else if(!is_numeric($winningBidder)) {
      $message = 'The Winning Bidder ID can only be numbers.';
    } else {

      // Check to see if the bidder ID already exists
      $checkBidder = mysql_query("SELECT * FROM bidders WHERE biddersId='$winningBidder' LIMIT 1");
      $checkBidder = mysql_fetch_assoc($checkBidder);

      // If the Bidder ID does not exist, we re-direct to allow us to save the Bidder ID
      if(!$checkBidder) {

        header("Location: ?action=confirmListing&iDesc=".$itemDescription."&iPrice=".$itemPrice."&wBidder=".$winningBidder."&tDeals=".$totalDeals."");

      } else { 
if($itemDescription[0] === "1") {
$itemPrice= '$itemPrice*6.25%';
}
        // If Bidder ID exists we just insert the transaction accordingly
        mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `bidderId`, `itemQty`, `totalPrice`) VALUES ('$itemDescription', '$itemPrice', '$winningBidder', '$totalDeals', '$totalPrice');");
        $message1 = 'The transaction has been successfully added.';

mysql_query("SELECT owed From bidders WHERE biddersId='$winningBidder'") or die(mysql_error()); 
if ($row['owed']==0) {
mysql_query("UPDATE bidders SET owed='1' WHERE biddersId='$winningBidder'") or die(mysql_error());  
}
      }

    }

  }

// This section is to check if the Add Bidder to database form has been submitted
  if($_POST['confirmBidder']) {
    $itemDescription = $_POST['itemDescription'];
    $itemPrice       = $_POST['itemPrice'];
    $winningBidder   = $_POST['winningBidder'];
    $totalDeals      = $_POST['totalDeals'];
    $totalPrice      = ($itemPrice*$totalDeals);
    $addBidder       = $_POST['addBidder'];


if($itemDescription[0] === "1") {
$itemPrice= '$itemPrice*6.25%';

    mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `bidderId`, `itemQty`, `totalPrice`) VALUES ('$itemDescription', '$itemPrice', '$winningBidder', '$totalDeals', '$totalPrice');");
    $message1 = 'The transaction has been successfully added.';
}
   
    if($addBidder == 'Yes') {
      mysql_query("INSERT INTO bidders (biddersId) VALUES ('$winningBidder');");
      $message1 .= '<br> The Bidder ID has also been added.';
mysql_query("SELECT owed From bidders WHERE biddersId='$winningBidder'") or die(mysql_error()); 
if ($row['owed']==0) {
mysql_query("UPDATE bidders SET owed='1' WHERE biddersId='$winningBidder'") or die(mysql_error());  
}
    }
      
if($addBidder == 'No') {
                 $message1 = '<br><font color= "red"> Bidder has NOT been logged.</font>';
               }

   // $itemDescription = '';
   // $itemPrice       = '';
   // $winningBidder   = '';
   //  $totalDeals      = '';
   // $totalPrice      = '';
   // $addBidder       = '';
      }

        

   



?>


<?PHP
  // This line of code will check the current task we are doing
  if($_GET['action'] == 'confirmListing') { 
    $itemDescription = $_GET['iDesc'];
    $itemPrice       = $_GET['iPrice'];
    $winningBidder   = $_GET['wBidder'];
    $totalDeals      = $_GET['tDeals'];
?>

<form name="confirmBidder" method="POST" action="?">

<?PHP
// This is the hidden data from the previous form
// Better than using sessions and GET query
?>
<input type="hidden" name="itemDescription" id="itemDescription" value="<?PHP echo $itemDescription;?>">
<input type="hidden" name="itemPrice" id="itemPrice" value="<?PHP echo $itemPrice;?>">
<input type="hidden" name="winningBidder" id="winningBidder" value="<?PHP echo $winningBidder;?>">
<input type="hidden" name="totalDeals" id="totalDeals" value="1">

<table cellpadding="0" cellspacing="1" border="0" style="background: #ffffff; margin: auto;">
<tr>
  <td colspan="2" class="formHeader"> Add New Auction Item </td>
</tr>
<tr>
  <td class="formField"> Add This Bidder? </td>
  <td class="formValue"> <select name="addBidder" id="addBidder" class="input" style="width: 195px;"> <option value="Yes">Yes</option> <option value="No">No</option></select> </td>
</tr>
<tr>
  <td colspan="2" class="formButton">
<input type="submit" name="confirmBidder" id="confirmBidder" value="Confirm Bidder Action?"> 
<input type="reset" name="reset" id="reset" value="Reset Form">
  </td>
</tr>
</table>
</form>

<?PHP
  // If the action is not to confirm a listing then show add item section
  } else {
?>

<?PHP//----- Add New Item Section -----\\?>

<?PHP 
  if($message) {
    echo '<div class="Error">',$message,'</div><div style="height: 10px;"></div>';
  } else if($message1) {
    echo '<div class="Success">',$message1,'</div><div style="height: 10px;"></div>';
  }
?>

<form name="addNewItem" method="POST" action="?">
<table cellpadding="0" cellspacing="1" border="0" style="background: #ffffff; margin: auto;">
<tr>
  <td colspan="2" class="formHeader"> Add New Auction Item </td>
</tr>
<tr>
  <td class="formField"> Item Description </td>
  <td class="formValue"> <input type="text" name="itemDescription" class="formText" id="itemDescription" value="<?=$itemDescription;?>" style="width: 195px;"> </td>
</tr>
<tr>
<td class="formField"> Item Price </td>
  <td class="formValue"> <input type="text" name="itemPrice" class="formText" id="itemPrice" value="<?=$itemPrice;?>" style="width: 195px;"> </td>
</tr>
<tr>
  <td class="formField"> Winning Bidder ID </td>
  <td class="formValue"> <input type="text" name="winningBidder" class="formText" id="winningBidder" style="width: 195px;"> </td>
</tr>
<tr>
  <td class="formField"> How Many Deals? </td>
  <td class="formValue"> <input type="text" name="totalDeals" class="formText" id="totalDeals" value="1" style="width: 195px;"> </td>
</tr>
<tr>
  <td colspan="2" class="formButton">
<input type="submit" name="addNewItem" id="addNewItem" value="Add New Auction Item?"> 
<input type="reset" name="reset" id="reset" value="Reset Form">
  </td>
</tr>
</table>
</form>

<?PHP//----- Add New Item Section -----\\?>

<?PHP } ?>

  


Link to comment
Share on other sites

I don't believe php tags are case insensitive.  So, use <?php ?> and NOT <?PHP ?>

 

Now, to the business at hand:

 

What, precisely, is $itemDescription?  Is it a string?  From the way you describe it, you'll need to obtain the number (by using explode or regEx), then do your math from there.  That said, there's a wide margin of error doing it that way since you're not guaranteed that the format you're expecting the data to come in as will be the way the user typed it in.  Example: you're expecting "1 table" as incoming data, but the user enters in something like "1 table 2 other things" or "tables: 1" or any other combination (since users can be dumb and chaotic).

 

If I were you, I'd have two fields here.  A small text field to obtain the number, and a corresponding select element with all of the possible description options.  This would rein in the incoming data, and make it easier to validate.

Link to comment
Share on other sites

I don't believe php tags are case insensitive.  So, use <?php ?> and NOT <?PHP ?>

 

Now, to the business at hand:

 

What, precisely, is $itemDescription?  Is it a string?  From the way you describe it, you'll need to obtain the number (by using explode or regEx), then do your math from there.  That said, there's a wide margin of error doing it that way since you're not guaranteed that the format you're expecting the data to come in as will be the way the user typed it in.  Example: you're expecting "1 table" as incoming data, but the user enters in something like "1 table 2 other things" or "tables: 1" or any other combination (since users can be dumb and chaotic).

 

If I were you, I'd have two fields here.  A small text field to obtain the number, and a corresponding select element with all of the possible description options.  This would rein in the incoming data, and make it easier to validate.

 

Thanks for the suggestions.

 

itemDescription is a field the client must fill with the item up for auction.

The auction is a fast paced venue where the client will be required to enter and "submit" multiple winning bidder numbers in sequence(It's fast paced, I already tested the program live and it was hectic keeping up).

That being said, I really can't add another text field. The description will change for every auction item up for bids so that doesn't work either. I'm thinking maybe having two submit buttons for this. 1 says taxed and the other non-taxed and do the math from there?

 

Your thoughts?

Link to comment
Share on other sites

try

if($itemDescription[0] === "1") {
$itemPrice *= 1.0625;//add 6.25 %

    mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `bidderId`, `itemQty`, `totalPrice`) VALUES ('$itemDescription', '$itemPrice', '$winningBidder', '$totalDeals', '$totalPrice');");
    $message1 = 'The transaction has been successfully added.';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.