Jump to content

newb needs help please -trying to make a calculator


1acid

Recommended Posts

Ok so Im trying to make a calculator and IM kinda stuck.

 

I have a standard keypad with buttons 0-9 and then +-/* plus a few other things i will figure out later

 

I want to be able to let the user enter in a string of numbers and display that in the text box which I will later let the user input a number into, but for now im trying to get the the value from the button that was pushed and add it to a string which would have been stored with any previous numbers that the user pushed. So at the moment it will display a single digit in the textbox but it wont remeber that digit and then display the next digit pushed ie if the user wants to enter 12 he first pushes 1 then it needs to hold one and display the second value entered on the right hand side of that digit, and so forth.

 

LEt me know what im doing wrong. I was thinking of maybe writing a function to check what was pushed but im not 100% sure how to do that and dont just want to get lost, so if someone could maybe with the theory of behind how im going to store the digits, I dont want it to be super calulated just able to maybe do like 134 + 26 = 160 simple math for now.

 

Thanks for any help, just let me know where im  making mistakes, and how i can do things better. thanks again

 

So i have my basic HTML layout like this:

 

<html>

    <head>

        <title>My Calc™</title>

    <link type="text/css" rel="stylesheet" href="styles.css"/>

    <?php    ?>

    </head>

 

    <body>

    <div id=wrapper>

        <h2>Craigs Calculater™</h2>

        <h3>Go on, make a calculation</h3>

 

            <form  method="POST" id="form1">

 

                <table name="table1" id="table">

                    <tr>

                        <td><input type="submit" name="value" value="1"/></td>

                        <td><input type="submit" name="value" value="2"/></td>

                        <td><input type="submit" name="value" value="3"/></td>

                        <td><input type="submit" name="calc" value="  +/- "/></td>

                    </tr>

                    <tr>

                        <td><input type="submit" name="value" value="4"/></td>

                        <td><input type="submit" name="value" value="5"/></td>

                        <td><input type="submit" name="value" value="6"/></td>

                        <td><input type="submit" name="value" value="0"/></td>

                    </tr>

                    <tr>

                        <td><input type="submit" name="value" value="7"/></td>

                        <td><input type="submit" name="value" value="8"/></td>

                        <td><input type="submit" name="value" value="9"/></td>

                        <td><input type="submit" name="calc" value=" clr  "/></td>

                    </tr>

            <tr>

                        <td><input type="submit" name="calc" value="+"/></td>

                        <td><input type="submit" name="calc" value="-"/></td>

            <td><input type="submit" name="calc" value="X"/></td>

            <td><input type="submit" name="calc" value="/"/></td>

                     

                 

                </tr>

        </table>

        <input type="text" name="box" value="<?php global $storeNum; echo $storeNum;?>" placeholder="      Wazzup Homie?"/>

        <br/>

        <br/>

        <input type="submit" name="calc" value="Calculate"/>

        <br/>

        <br/>

         

     

        </form>

<div>

    </body>

 

</html>

 

--------------

OK and here's the stylesheet:

 

#form1{background:#ccccff;width:400px;margin-left:270px;border: solid #3366ff;border-radius:15px;box-shadow:3px 3px 1px #333;}

 

body{background:#99ccff;}

 

h2{text-shadow: black 0.1em 0.1em 0.2em; color:#330033}

 

h3{text-shadow: 0 0 0.2em #87F, 0 0 0.2em #00ccff;,

        0 0 0.2em #ffff00; color:#330033;}

 

#wrapper{width:960px; background:#9999ff;text-align:center; float:center; margin-left:150px; padding:10px;margin-top:5px;border: solid 2px #6600cc;height:600px;border-radius:15px;box-shadow:3px 3px 1px #333;}

 

#table{float:center;border:#6600cc;padding:5px;margin-left:125px; }

 

input{background:#6699cc; color:#000;border-radius:7px;box-shadow:2px 2px 1px #666;}

 

input:hover{background:#660066; color:#fff;}

 

---------

 

and then this is the switch statement i was using to get the value of the submitted button.

 

switch($_POST['value'])

    {

       

        case '1':

            global $storeNum;

            $temp = $storeNum;

            $newNum = $temp.'1';

            $storeNum = $newNum;

       

            break;

        case '2':

            global $storeNum;

            $temp = $storeNum;

            $newNum = $temp.'2';

            $storeNum = $newNum;

            break;

        case '3':

            global $storeNum;

            $temp = $storeNum;

            $newNum = $temp.'3';

            $storeNum = $newNum;

            break;

        case '4':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'4';

            $storeNum = $newNum;

            break;

        case '5':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'5';

            $storeNum = $newNum;

            break;

        case '6':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'6';

            $storeNum = $newNum;

            break;

        case '7':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'7';

            $storeNum = $newNum;

            break;

        case '8':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'8';

            $storeNum = $newNum;

            break;

        case '9':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'9';

            $storeNum = $newNum;

            break;

        case '0':

            global $storeNum;

            $temp = $storeNum;

            $newNum = "$temp".'0';

            $storeNum = $newNum;

            break;

       

    }

 

Link to comment
Share on other sites

Ummm is there anyway to do this with just normal HTML or is Javascript the only option? Surely with PHP each time a button is pushed it can update the textbox and remeber the value?  I really havnt studied any javascript and ive just finished a PHP course so I was just trying to practice some of the simpler stuff I had learn't and implement it in a basic calculator... anyways thanks for the help guys, any other tips would be greatly appreciated.

 

Thanks again!

Link to comment
Share on other sites

ok so when u say the page needs to reload thats fine, this is just a test and im tryna just practise some stuff and just now ill be adding a sign up page and log in page just for practise. So.. How would I make it add to my string when i push a number for the second time?

 

Thanks again

Link to comment
Share on other sites

ok so i need to be using this sessions thing to hold the data when the pge reoads? okim gonna try fiddle with that abit.

 

I have written a insert qry that is meant to let a user sign up, but the data is not being inserted into the database ,could you guys perhaps look at my query and let me know where its going wrong.

 

The table is called calcuser the fields are name, surname, username and password here is the code, 1st im validating that the fields are complete and that the passwords match and then it should insert it into my DB but it not, its getting into the final if statement as it is unsetting the data and redirecting to my index page but the user data is not in the DB.

 

Any hep again would be greatly appreaciated.

 

Thanks

 

 

here's the code

[

$error=array();

   

    if(isset($_POST['submit']))

    {

        if(empty($_POST['name']))

        {

            $error['name'] = "Please enter a name";

        }

        if(empty($_POST['surname']))

        {

            $error['surname'] = "Do you have a surname?";

        }

        if(empty($_POST['username']))

        {

            $error['username'] = "Please enter a username";

        }

        if(empty($_POST['password']))

        {

            $error['password'] = "Please enter a password";

        }

        else if(!preg_match("/^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=).*$/", $_POST['password'])){

          $error['password'] = "Password not valid!";

        }

        if(empty($_POST['password2']))

        {

            $error['password2'] = "Please confirm your password";

        }

        elseif($_POST['password']!=$_POST['password2'])

        {

            $error['password'] = "Your Passwords do not match";

        }

        if(count($error)==0)

        {

            $cleanData = sanitize($_POST);

            $encrypted = md5($_POST['password']);

           

            $sqlStr = "INSERT INTO calcuser(name, surname, username, password)".

                    "VALUES('{$cleanData['name']}','{$cleanData['surname']}','{$cleanData['username']}','{$encrypted}')";

                   

                    mysql_query($sqlStr);

                    unset($_POST);

                    redirect("index.php");

        }

       

    }

]

error() is a function i made to display the errors :

 

it looks like this and works on other pages i made:

 

function error($name){ //this is checking if any value was assinged to the error

    global $error;

    if(isset($error[$name])){//checks if there is a value set in the array of error

        echo $error[$name]; //if there is a value set in the array it will display the error msg

    }

   

}

 

Thanks again

Link to comment
Share on other sites

BTW sanitize is another function i created thats just cleaning the input: looks like this, also tried and tested without issues.

 

function sanitize($value){ //this will take in a string clean it using the mysql_real_escape_String and return the cleaned string

    return mysql_real_escape_string($value);

   

}

 

function sanitize_array($array)//this cleans an array

{

    foreach($array as $key=>$value)

    {

        $array[$key] = sanitize($value);

    }

    return $array;

}

I jsut realised i wasnt using the array one, and changed it in my code...

Link to comment
Share on other sites

If you are placing code on the forum please place them between the correct tags. You will get more joy from people that way.

 

Try echoing your SQL statement. I think you will see the problem when you do that.

 

echo $sqlStr;

Link to comment
Share on other sites

ok sorry about the

 thing will try do better from here on in  

heres what it said when i echo'd the sqlStr out:

INSERT INTO calcuser(name, surname, username, password)VALUES('xyz','abc','user1','c53e479b03b3220d3d56da88c4cace20') 

That looks correct to me?

am i missing something?

Thanks

Link to comment
Share on other sites

Thanks for the help guys, it was the fact that i never set up a DB connection anywhere. It inserted a user into the DB now, so getting kinda late here, will call it a night and work on my sign in page tomorw. Thanks again for all the help!!

 

Peace

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.