eliktris Posted April 28, 2009 Share Posted April 28, 2009 Hi there, i'm very new at PHP and although i'm trying real hard to understand these online tutorials i'm slower than a snail trying to run the race.. anyhow - i have a client i've volunteered to help out and i've been doing manual updates to a site for each registration that comes in. it is getting rather tedious and well i know there has to be a script for it somewhere.. basically i need help in creating the script, i know its with math!! what i'm hoping to have done, is that with each time the registration is "submitted" have the counter (on the home page) increase by 1.. can anyone help me out? i really appreciate this more than you would know.. if anything it helps me to see if i am on the right track.. i'm thinking its a operator or something and would go like $submit= $counter+1 or something.. maybe i'm probably far off base thanks for any advice or help Cheers Eliktris Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 Thats fairly simple =) Basically you want to, as you stated keep a placeholder or variable for this counter and every time the button is clicked, this counter gets updated. <?php $counter = 0; if (isset($_POST['my_button'])) { $counter += 1; echo $counter; } ?> However all this is doing is storing the number for the lifetime of the page. Once the page refreshes $counter will be reset. If you want to keep an uptodate counter, you would probably need database interaction. Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 28, 2009 Author Share Posted April 28, 2009 ohhh yay - ok that looks really really easy.. and excuse me for being stupid -please can you spell this out to me like i'm in kindergarten..lol and i apologize. i assume, i would put this in my form before/near the submit button, and i would have to put the place holder in the page that holds the counter (its 2 separate pages) how do i link the 2 pages together in the php script? thanks again sooo much - you rock! Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 to link two pages together use a include <?php include('myPhPpage'); ?> this needs to go at the start of the page. With your counter, currently my way will only store the number for that session, meaning the second the page is closed, or refreshed the number will be reset to 0. Im guessing you want this number stored so it will grow over time? I believe thats done with Javascript, else you could store the number in a database, pull the number everytime the form is submitted, and update the number with +1. I think the Javascript method might be a little more simple, however you'll have to go knock on the Javascript section and ask those guys... Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 yes it would need to increment with each new registration.. hmm ok so i need to put the <?php $counter = 0; if (isset($_POST['my_button'])) { $counter += 1; echo $counter; } ?> into the form.. and the <?php include('myPhPpage'); ?> onto the page that has the counter... the 'myPhPpage' is that the page name that the counter is on? its not in php does that matter? the whole site is xhtml/css right now except for the minor php used for the form. thanks again for helping me Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 29, 2009 Share Posted April 29, 2009 ok. Well first thing is first, if you want to do a sort of counter in php you will need to save the counter to a database, as since once the page gets refreshed, your updated counter variable will be set, rendering the variable completely useless. some background information tho. I know your completely new to php. what exactly do you know how to do? do you know anything about mysql or databases? do you know how to query a database to get information or update a database with information? ok sorry for the tough questions =P now on to your questions onto the page that has the counter... the 'myPhPpage' is that the page name that the counter is on? its not in php does that matter? the whole site is xhtml/css right now except for the minor php used for the form. if you are going to go the PHP route (IE storing your counter on a database) then you are going to want to put the php that updates the counter in the page the processes the form. Now the page that actually shows the counter can go on whatever page you want it to. Actually, you can make a completely seperate page that only gets the counter from the database, and echos it (as somone above had mentioned) and just include it. Now i assume you are manually updating a random table on a database. To be perfectly honest, if you wanted to get the total numbers of entries on a database just do some simple php, IE $sql = mysql_query("SELECT * FROM tablename");//tablename is the table that your data is entered in, in your database if (!$sql){//this simply checks if the mysql query was valid. if not then stop the scrpt echo mysql_error(); exit() } $num = mysql_num_rows($sql);//this is your counter the above sends a mysql query which selects every row on the database. th mysql_num_rows tells how many rows were returned with the query, or in other words how many entries are in the database. I hope this is what you wanted, and hope this helps Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 Hi Mikesta - well, i'm a xhtml/css coder - and very very beginner in php /mySQL as well - but i do know how to create a database!(i think) i've been trying to learn php and MySQL for a while and slowly starting to see a flicker of hope.. but not enough to get me going.. anyhow i can medal around in existing code somewhat to tweak what i need- and i can create a database... but thats about the extent of it.. i guess i need to fully explain myself... and the problem i have.. basically right now - the client emails me (practically 2/3 times a day) with each new # of registrations he gets(each registration is emailed to him).. and i add his total to the current displayed total on his website. the registration is for the number of jeeps getting registered into an event, and also how many people will be riding in the jeep. example- it says: there are currently "20" jeeps registered and 56 participants for this event! i'm hoping to have these adjust themselves with each time the registration is submitted.... so i'm not pestered just to update it by 1 myself 3 times a day...lol ok, so am i understanding this right - i need to : 1. create a database to "save" the current number (say 20) only question i have is - will it only need to be one field? so create database named "jeeps" and have it have a field of 1? 2. then, i need a script to query that database for that number when someone submits their registration - and then it increase that number by 1 - "now 21" (this is another page?) 3. and then that page will post the "21" on the page where the counter is? thanks again guys for speaking in simple terms!!! Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted April 29, 2009 Share Posted April 29, 2009 You definitely have the correct idea in mind! 1. Create a database 2. Within the database have at least one table that will hold your count value. 3. Create a script that will basically: a. After submit is pressed connect to the database b. Once your connected, get the table with your count value and update it with a + 1 value. c. process anything else if need be. 4. That should be it Of course this is vague and there are a ton of minor things that need to get done, but this should start you in the right direction Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 oh boy - ok so now that i know what i have to do, can anyone help me figure out how write the script? in the mean time - i'll go create the database Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 29, 2009 Share Posted April 29, 2009 oh boy - ok so now that i know what i have to do, can anyone help me figure out how write the script? in the mean time - i'll go create the database Well, mikesta already did it. <?php $sql = mysql_query("SELECT * FROM tablename");//tablename is the table that your data is entered in, in your database if (!$sql){//this simply checks if the mysql query was valid. if not then stop the scrpt echo mysql_error(); exit() } $num = mysql_num_rows($sql);//this is your counter ?> Sorry, but we're not here to write every line of code for you. Go to google, find some good tutorials on the subjects of "basic PHP and MYSQL", and get started from there. Then, once you get errors, post em here, and we'll spot them for ya. Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 ok, so created database... and think i might have figured out the right code - sorta - does it sound right? i got <?php if ($HTTP_POST_VARS['submit']) { mysql_connect("server","username","password"); mysql_select_db("jeeps"); $query ="INSERT INTO jeeps (total)"; $query.=" VALUES ('+1')"; ?> that goes on the same page as the registration form... and then... <?php mysql_connect("server","username","password"); mysql_select_db("dbname"); $result=mysql_query($query); mysql_fetch_row($result)) { echo "$result"; } ?> going on the actual page where the counter is... and sorry for being "dumb" and not seeming like i'm not trying to figure this out myself, as you can clearly see i am trying.. just thought this would be a friendly place to help an absolute beginner-php-programmer.. sorry if my request has offended anyone.. but i'm sure i'm not the only person who has started at this level and asked for help.. thanks for those who have been helping it really is great to have your advice. i assure you i'm not profiting from this other than expanding my knowledge and to me, that is great in itself! cheers eliktris Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 29, 2009 Share Posted April 29, 2009 ok, a couple of things, but you are on the right track. by the way, when you want to put code, either wrap them in php tags or code tags, IE "["php"]" "["/php"]" without the quotes of course. now for your first script <?php if ($HTTP_POST_VARS['submit']) { mysql_connect("server","username","password"); mysql_select_db("jeeps"); $query ="INSERT INTO jeeps (total)"; $query.=" VALUES ('+1')"; ?> For one, whenever I want to connect to a database, i put the connect script into a file called connect.php (although you could call it what you want). and just include that script in all, or most of my pages. Whether or not this is good practice someone else can say (as i'm not an expert), but in my opinion doing this is fine now for your query. Its entirely wrong (not to sound like an A-hole hahaha). Its close, but incorrect. I would suggest that you read a few tutorials on MySQL before you start doing queries, or you will have a headache! now first thing you have to understand is the difference between databases and tables. A table is a section of a database that has different rows and columns, and a database is something that holds multiple tables. When you access information from a database, you are actually accessing a table in that database. Get it? So its not enough to just make a database. you also have to make a table Now the structure of the table is up to what you need. You could only have one field that tells how many entries there are, but assuming that your entries are in a different table, you could really just do the code I gave you above using that table name I hope that helps! good luck Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 Good Morning... - ok, so this is what i got now... i've got my connect.php file created (question, and sorry for being dumb... but this will realize when someone hits submit on a misc page in the website? i dont see it pointing to any one location other than the submit.. wouldnt this work better if it was in the same code as the actual registration page? sorry for being skeptic.. i'm just confused.. i feel like im missing something.. "php" <?php if ($HTTP_POST_VARS['submit']) { mysql_connect("jkja3172_jester","jkjam31","jester3172"); mysql_select_db("jeeps"); $query ="INSERT INTO jeeps (total)"; $query.=" VALUES ('+1')"; ?> "/php" and then i think i got the right query string coming along that would go on the page where it is being counted... "php" $query="SELECT * FROM jeeps"; $result=mysql_query($query); echo "<b>there are $jeeps registered"; } "/php" ?> am i getting there? Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted April 29, 2009 Share Posted April 29, 2009 Morning, First off with your tags, your close use ' ' ' ' Without ' Secondly not the best idea to put your database username and password... but we're a trusting bunch I suppose. Thirdly your getting there. To show the total amount you could also use a count. $query="SELECT count(*) FROM jeeps"; $result=@mysql_query($query); echo "<b>there are $result registered"; Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 hehe - opps.. i am new!! i guess i will go and change those now thanks for pointing it out..lol Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 Morning, First off with your tags, your close use ' ' ' ' Without ' Secondly not the best idea to put your database username and password... but we're a trusting bunch I suppose. Thirdly your getting there. To show the total amount you could also use a count. $query="SELECT count(*) FROM jeeps"; $result=@mysql_query($query); echo "<b>there are $result registered"; i think i'm lost....and terribly confused.. sorry ??? :'( previous to this "thing" i'm trying to do now, there is no database end doing anything, the form has a simple "mailto" on submit currently and the filled out form gets emailed to the client. so everything i'm doing in php/mysql is fresh from scratch here.. along with my lack of php/mysql knowldge.. anyhow.. what does the "count" function do - besides the obvious.. would it "count" each time someone submits a registration? i really wish i can find a local school or someone who would sit with me for a few hours and chisel this stuff into my head Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 29, 2009 Author Share Posted April 29, 2009 For one, whenever I want to connect to a database, i put the connect script into a file called connect.php (although you could call it what you want). and just include that script in all, or most of my pages. Whether or not this is good practice someone else can say (as i'm not an expert), but in my opinion doing this is fine what do you mean by that?? can you please explain - i'm thinking you make a connect.php file - and its on your server - how does each html page connect to it/or it connect to each html page... or do i just put that connect.php script into each page that it is required? sorry if its a dumb question to ask... cheers eliktris Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 29, 2009 Share Posted April 29, 2009 For one, whenever I want to connect to a database, i put the connect script into a file called connect.php (although you could call it what you want). and just include that script in all, or most of my pages. Whether or not this is good practice someone else can say (as i'm not an expert), but in my opinion doing this is fine what do you mean by that?? can you please explain - i'm thinking you make a connect.php file - and its on your server - how does each html page connect to it/or it connect to each html page... or do i just put that connect.php script into each page that it is required? sorry if its a dumb question to ask... cheers eliktris What i mean by that is instead of writing your connect php everytime you want to connect IE mysql_connect(stuff, stuff, stuff); //etc. You just write that once in a file you call connect.php (or whatever) and you include it whenever you would have written it. IE include('connect.php'); Its just a little simpler. i think i'm lost....and terribly confused.. sorry ??? :'( previous to this "thing" i'm trying to do now, there is no database end doing anything, the form has a simple "mailto" on submit currently and the filled out form gets emailed to the client. so everything i'm doing in php/mysql is fresh from scratch here.. along with my lack of php/mysql knowldge.. anyhow.. what does the "count" function do - besides the obvious.. would it "count" each time someone submits a registration? i really wish i can find a local school or someone who would sit with me for a few hours and chisel this stuff into my head Ahh I was unaware of this. i previously thought that the form added the jeeps info to the database. So let me see if I understand correctly now. All you want to do with the database is just put how many jeeps there are right? No other information about the jeeps? In that case just make a table with 1 row, called count or whatever, and make that row an interger. everytime someone submits the form do something like $sql = mysql_query("UPDATE jeeps SET count=count+1"); that should work for incrementing the count. when you want to display it just do something like $sql = mysql_query("SELECT * FROM jeeps"); $row = mysql_fetch_assoc($sql); $count = $row['count']; echo $count; that should do what you want it to hope that helps! Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 30, 2009 Author Share Posted April 30, 2009 All you want to do with the database is just put how many jeeps there are right? No other information about the jeeps? yes/no - there is another "total" i need to have also count and then display in the counter jeeps and passengers.. so i guess i will have to figure out how to have that go to the database and that total will vary from vehicle to vehicle) for now - these are the only 2 main peices of info required to go to the database and be retreived.. cheers Christine Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 30, 2009 Author Share Posted April 30, 2009 ok, so just when i think i got this going right... its not displaying in the spot at all.. maybe someone can tell me what i've got wrong now :s </h4>There are currently <?php include('connect.php'); $sql = mysql_query("UPDATE database name SET field name=field name+1"); $sql = mysql_query("SELECT * database name"); $row(i dont know what this is for) = mysql_fetch_assoc($sql); $field name= $row['field name']; echo $field name;?><span class="c1"> JK's Registered and 54 Attendee's registered<br /> oh.. how do i set an interger or "number" in the database.. so it starts at a specific number? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 30, 2009 Share Posted April 30, 2009 You need to set error erporting to all...run this at the top of the script, and upload it... ini_set('display_errors',1); error_reporting(E_ALL); error reporting is obviously not on b/c you have a TON of syntax errors in there. Also, you CANNOT have spaces in variable names in php or mysql.... for instance... "$field name" isn't valid, it must be $field_name try: </h4>There are currently <?php include('connect.php'); mysql_query("UPDATE database name SET field_name=field_name+1"); $sql = mysql_query("SELECT * FROM database name LIMIT 1");//you forgot the FROM clause $row= mysql_fetch_array($sql); $field_name= $row['field_name']; echo $field_name; ?> <span class="c1"> JK's Registered and 54 Attendee's registered<br /> also, you don't use the Database name, you use the Table name...there;s quite a differnce there Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 30, 2009 Author Share Posted April 30, 2009 sorry.. i guess i should have mentioned - i took out the actual data b/c last time i left it all in there and i was told i shouldnt.. .. hehe - i'm batting 0 here..lol whats a from clause? $sql = mysql_query("SELECT * FROM database name LIMIT 1");//you forgot the FROM clause is the teal not the from clause? i just didnt put in my database name - is it supposed to be database name - or is it the table name or something else? ............. thinking its just gonna be easier to just keep updating the darn thing hourly than figuring this out..lol.. its taking me days!! ??? ........must.... not...give... up!! Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 30, 2009 Share Posted April 30, 2009 don't give up, its very easy once you learn it. i meant the "FROM" clause. You have to have "FROM table name" ater every SELECT, UPDATE or DELETE statement to tell mysql what table to look into.... Have you turned on all errors yet? Quote Link to comment Share on other sites More sharing options...
eliktris Posted April 30, 2009 Author Share Posted April 30, 2009 lol- now try not to laugh too hard.. but i dont know how to even do that... lol i'm gonna try finding something later (a tutorial somewhere) that might show me where to put it in my code.. i think i may even have it placed wrong.. lol.. as soon as my kids are all calm and not bugging the heck out of me i can concentrate on it.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.