Help with PHP code (converting strings to ints)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kaspir
Forum Newbie
Posts: 3
Joined: Tue Aug 10, 2010 10:37 am

Help with PHP code (converting strings to ints)

Post by kaspir »

I'm new to php. I'm writing a script to read the multiple inputs (drop down menus) from a form, convert them to int, add them together and display the price. I'm having trouble with the converting part though. I'm not sure exactly how to do it. I tried writing if statements for each variable, but that just displayed syntax errors, so I dropped that. So I'm kinda at a lost at what to do now.

Code: Select all

      
      <section id="quote">
        <header>
          <h1>Free Price Quote Generator</h1>
        </header>
        <p>This utility is used to help you find the package that's right for you. Answer the questions below to help us determine which package you should choose. All fields are required.</p>
	<?php
	if(isset($_POST['submit'])){
	    $logo   = $_POST['logo'];
	    $content   = $_POST['content'];
            $pages   = $_POST['pages'];
	    $maint   = $_POST['maint'];
	    $drop   = $_POST['drop'];
	    $forms   = $_POST['forms'];
            // Convert values into ints
            function convertToInt($_POST['submit']){
            foreach {
                // Where I'm stuck
                }
	    }
        } 
	?>
	<form method="post" action="quote.php">
          Do you have a finished logo?<br />
          <select name="logo" value="<?php echo $logo; ?>">
            <option>Yes</option>
            <option>No</option>
          </select><br />
          Do you have finished content?(images)<br />
          <select name="content" value="<?php echo $content; ?>">
            <option>Yes</option>
            <option>No</option>
          </select><br />
          How many pages do you estimate your site to be?<br />
          <select name="pages" value="<?php echo $pages; ?>">
            <option>1 - 10</option>
            <option>10 - 20</option>
            <option>20 or More</option>
          </select><br />
          Would you like site maintenance?<br />
          <select name="maint" value="<?php echo $maint; ?>">
            <option>Yes</option>
            <option>No</option>
          </select><br />   
          Would you like drop down navigation?<br />
          <select name="drop" value="<?php echo $drop; ?>">
            <option>Yes</option>
            <option>No</option>
          </select><br />   
          Would you like any forms such as contact forms?<br />
          <select name="forms" value="<?php echo $forms; ?>">
            <option>Yes</option>
            <option>No</option>
          </select><br />     
	  <input type="submit" name="submit" value="Submit">
	</form>        
      </section>
I thought maybe I could use a foreach code to go through the variables and convert each one to replace all the if statements. But I can't figure out how to get it to work. I'm not asking for you to rewrite my code for me. Just want some help in which direction to go with it, maybe some simple examples, and tutorial links are always appreciated :D

Thanks in advance for any help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP code (converting strings to ints)

Post by AbraCadaver »

I'm not totally clear on what you're trying to do, but you can specify the value in the options. Also, the select doesn't have a value. If you want an option to be selected then you set it as selected (work out with an if statement whether it should be selected or not:

Code: Select all

<select name="logo">
   <option value="0" selected>Yes</option>
   <option value="50">No</option>
</select>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kaspir
Forum Newbie
Posts: 3
Joined: Tue Aug 10, 2010 10:37 am

Re: Help with PHP code (converting strings to ints)

Post by kaspir »

Sorry for not being clear enough. Essentially I want to take the input from the form (Do you have a finished logo?) and convert the answer (No) into a integer value (100) for pricing purposes. What your wrote...

Code: Select all

<select name="logo">
   <option value="0" selected>Yes</option>
   <option value="50">No</option>
</select>
...with this I wouldn't have to convert anything? I would just have to call the value of the option correct?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP code (converting strings to ints)

Post by AbraCadaver »

kaspir wrote:Sorry for not being clear enough. Essentially I want to take the input from the form (Do you have a finished logo?) and convert the answer (No) into a integer value (100) for pricing purposes. What your wrote...

Code: Select all

<select name="logo">
   <option value="0" selected>Yes</option>
   <option value="50">No</option>
</select>
...with this I wouldn't have to convert anything? I would just have to call the value of the option correct?
Yep. That's how the options are used.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kaspir
Forum Newbie
Posts: 3
Joined: Tue Aug 10, 2010 10:37 am

Re: Help with PHP code (converting strings to ints)

Post by kaspir »

Alright thanks a lot :D
Post Reply