Jump to content

Phear46

Members
  • Posts

    30
  • Joined

  • Last visited

Everything posted by Phear46

  1. something like: foreach($food as $k => $v) { if(isarray($v)) { foreach($v as $key => $val) { echo $val; } } else { echo $v; } } That should work for an array like this $array = (key => val, key2 => val2, key3 => (val 3, val4, val5, val,6)) output : val val2 val3 val4 val5 val6 if yours goes deeper youll have to add extra 'is array' tests. Thats how ive done it previously, probably not the best way but it works
  2. All good suggestions guys, gone with drop downs for now as it seems the most basic way. Here is the code im using to draw the menu: <select name="day"> <?php for ($d=01; $d<=31; $d++) {echo "<option value='$d'>$d</option>";}?> </select> <select name="month"> <?php for ($m=01; $m<=12; $m++) {echo "<option value='$m'>$m</option>";}?> </select> <select name="year"> <?php for ($y=date("Y"); $y>=1970; $y--) {echo "<option value='$y'>$y</option>";}?> </select> The only issue im having is that the values are coming up as '1, 2, 3, 4' etc etc instead of '01, 02, 03, 04' Is there a way i can force the counter to be at least two digits? I could check values when i process the form but it seems more logical to get this sorted with the actual form values that are submitted.
  3. been thinking that since i posted.... probably a much better/easier way of doing it!
  4. OK, so i want my form adds a persons details to a DB, all good so far but i want to grab a birth-date with the form then input that to my DB. mySQL only accepts yyyy-mm-dd format so i need to convert my string to that format no-matter what. But what if the person doesn't enter the date correctly? what if the date doesn't even exist? I could 'guide' the user into inputting into the form in the correct format but Ive never seen this done on line anywhere. Everywhere ive ever enter a date has always accepted: dd-mm-yy dd-mm-yyyy mm-dd-yy mm-dd-yyyy I can probably figure out formatting to mySQL format by myself but im totally confused by what i should do when the user enters a date like: 01-03-88 is this 1st March 1988 Or 3rd January 1988? Is there a setting i can grab from the browser to determine where in the world a user is?
  5. if(isset($_POST['<NAME OF SUBMIT BUTTON>'])) { $userKey = $_POST['key']; $lic = $_POST['licensekey']; } to see what has been 'sent' to your script use: print_r($_POST); if nothing is returned, your sending nothing which is why you have blank space.
  6. I want to remove the last ' AND' from my string im using to build a mySQL query. I thought using rtrim was the way to go but either im wrong or im not using it correctly: foreach ($dupArray as $k => $v) { $whereStr = $whereStr . "$k='$v' AND "; } rtrim($whereStr, "AND"); echo $whereStr; Anyone got any ideas? I swear ive seen this done else where but i cant find any examples
  7. You look like you know what youre doing so this should help, found this: http://www.w3schools.com/tags/att_input_checked.asp <form action="demo_form.asp"> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car" checked> I have a car <br><input type="submit" value="Submit"> </form> Edit: Sorry, totally read that wrong!, you already got this ^^^
  8. Thanks for the input Barand, could you explain a little more? the only reason I'm storing the values like that is because the 'class' column is of the SET data type. I appreciate that its probably not the best database design around but I've not been at this long, this is the first 'project' I've started since following a beginner tutorial. If you have any good reference websites or links to learning materials for database design that would be great. Im not sure what you mean by un-normalized design & subject_id values not names? I was thinking of setting up the table to have one column (field?) per 'subject/class' whatever and then setting true/false. Would this be better? If so why? I dont see a difference as long as i can 'select * from table where class = science' for instance. Admittedly i haven't tried this yet so can guarantee it will work >.< Thanks for reading the original post anyway, i think i got a bit of word vomit in the OP and couldn't for the life of me explain my self properly! Sorry!
  9. function my_Form_To_DB_Test($host,$user,$pass,$db,$table) { //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { $form_array[$k] = $v; } array_pop($form_array); //Remove last entry from $_POST array (should be the button name/value!) print_r($form_array); echo "<br />"; /***** CONSTRUCT QUERY ****/ //Get field names, format into string ready for mySQL Query eg $mySQL_field_names = "(field1, field2, field3)" $mySQL_field_names = ""; $tmpStr = ""; foreach($form_array as $k => $v) { if ($tmpStr == ""){ mysql_escape_string($k); $tmpStr = $k; } else { mysql_escape_string($k); $tmpStr = "$tmpStr,$k"; } } $mySQL_field_names = "(" . $tmpStr . ")"; //Get values, format into string ready for mySQL Query eg $mySQL_values = "('value1', 'value2', '3,3a,3b')" $mySQL_values = ""; $tmpStr = ""; foreach($form_array as $k => $v) { if (is_array($v)) { foreach($v as $val) { if ($tmpArrStr == ""){ mysql_escape_string($val); $tmpArrStr = $val; } else { mysql_escape_string($val); $tmpArrStr = "$tmpArrStr,$val"; } } if ($tmpStr == ""){ mysql_escape_string($v); $tmpStr = "'$tmpArrStr'"; } else { $tmpStr = "$tmpStr, '$tmpArrStr'"; } } else { if ($tmpStr == ""){ mysql_escape_string($v); $tmpStr = "'$v'"; } else { mysql_escape_string($v); $tmpStr = "$tmpStr, '$v'"; } } } $mySQL_values = "(" . $tmpStr . ")"; //Construct final query $query = "INSERT INTO $table $mySQL_field_names VALUES $mySQL_values"; echo $query; This works as intended now. I was over complicating it before, the trick I find it to be MORE lazy.... the function will write a form input to the database aslong as the input name == db field name E.G: //This POST array -> Array ( [firstname] => John [surname] => Doe [dob] => 1980-01-01 [class] => Array ( [0] => english [1] => maths [2] => science ) ) //Creates this mySQL query -> INSERT INTO students (firstname,surname,dob,class) VALUES ('John', 'Doe', '1980-01-01', 'english,maths,science') So this should work whatever form I pass to the function i think If anyone has a suggestion to make it better im all ears! i know the foreach at the very top isnt needed, ill get rid of it.
  10. Ive got a solution, gimme a sec and ill post what ive done... almost there!
  11. OK, I wrote one function to write my form to a DB, then i created another page, with a different form and wrote another function to write it to a DB. Being lazy and wanting to streamline the process for further 'form writing' I tried to be crafty and hack togther a function that would write the correct form, to the correct database no matter what. So i could come up with any number of forms with different sizes etc different field names and it would just automagically work. I thought.... Hey, if i just make sure the 'name' of the input on the $_POST is the same string as the field name in the db, then it would work no matter what. Until i thought about when my form contained 'arrays' such as a check box array. Now im stuck.... The idea was to assign the post array to a new array so i could mess with it and not screw with the original POST array. Easy - //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { $form_array[$k] = $v; } } print_r($form_array); Now what if $k is an array? I figure define another array using the string of $k something like: //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { if (is_array($k)) { //Create new array with name $k $k = array(); $k[k] = $v; } else { $form_array[$k] = $v; } } print_r($form_array); But this just calls the array '$k' instead of '$string from $k'. Then i got to thinking.... even if i know how to use the string to create the variable name, how do i call it from the rest of the script? I cant use $string because writing it now i don't know what $string will be! Now I'm confused and don't know if this is even possible. I imagine it is but may be somewhat beyond my abilities at this point! I apologize if this isn't making any sense. If you do understand and could possibly point me in the direction of some useful functions within PHP already that would be awesome! i cant find anything yet but ill keep looking!
  12. if you else if is not being entered, one of two things is happening - 1. another condition before(?) your else if is evaluating to true 2. php/html might not like a space in the button name. Not sure. ive always used _'s for spaces (using linux got me in that habbit pretty quick :/)
  13. can you give some sample data and what the expected result from your sample would be. I have an idea but if its my idea its really simple and im proably misunderstanding!
  14. //Create query's foreach ($enrollment_array_class as $a){ if ($str == ""){ mysql_escape_string($a); $str = $a; } else { mysql_escape_string($a); $str = "$str,$a"; } } $class_string = $str; $query_insert_name = "INSERT INTO $table (fname, sname, dob) VALUES ('$DBfname', '$DBsname','$DBdob')"; $query_select = "SELECT * FROM $table WHERE fname='$DBfname' AND sname='$DBsname' AND dob='$DBdob'"; //Execute querys //Enter name/sname/dob into table mysql_query($query_insert_name) or die ("Error in query: $query_insert_name.".mysql_error()); //Get ID of the row that was just enterd $result = mysql_query($query_select) or die ("Error in query: $query_select.".mysql_error()); while ($row = mysql_fetch_array($result)) { $row_id = $row['id']; } //Enter classes into the row using the row id - set from the query above $query_insert_class = "INSERT INTO $table WHERE id='$row_id' (class) VALUES ('$class_string')"; mysql_query($query_insert_class) or die ("Error in query: $query_insert_class.".mysql_error()); Thought i had this cracked but ive got a problem in that last query, cant figure it out! tried as above and also tried: $query_insert_class = "INSERT INTO $table (class) VALUES ('$class_string') WHERE id='$row_id'"; mysql_query($query_insert_class) or die ("Error in query: $query_insert_class.".mysql_error()); The problem seems to be the WHERE part but im not sure why >.< Anyone know how to fix this? I think the solution is staring me in the face but im drawing a blank here....
  15. do a query to 'select' all the rows matching your input before you add it to the table. then: if rows returned > 0 { user exists } else { process form as you already are } a query like: SELECT * FROM table WHERE user=$usrname, pass=$pass is what your looking for, although i highly doubt ive got the correct syntax there as im also a noob, one of the pros should be able to help with that!
  16. ok, so you dont need to change your function. you need to edit your page dispaying the 'user: bob | 0 unread messages' im guessing the code you currently have says something like echo "user: $username | unread_private_messages() unread messages"; change it to something like: if (unread_private_messages() > 0) { echo "User: $username | " . unread_private_messages() . " Unread messages"; } else { echo "User: $username"; } If thats not what your after i have no idea what you mean, sorry! Maybe someone more knowledgeable than me can offer a better suggestion.
  17. ok, can you show me the code on your page that displays 'user name: bob | 0 UNREAD MESSAGES' and can you show me the output of: echo unread_private_messages();
  18. is total_unread_private_messages a function? what does it return? what do you mean hide 0 when there are no messages? You only want a message displayed when there ARE new messages? otherwise display nothing? <?php if ($total_unread_private_messages == 0) { //Do nothing } else { echo "You have $total_unread_private_messages new messages!" } ?>
  19. <?php if ($total_unread_private_messages != 0) { echo "You have $total_unread_private_messages unread messages"; } ?> Where have you been learning php? Ive not seen variables defined with { } braces before but Im also new to this resetting the counter is a bit more advanced, start with the basics
  20. // Grab all info into one variable - This is unnecassary but makes it more readable (to me anyway!) $enrollment_array = array('fname' => $_POST['firstname'], 'sname' => $_POST['surname'], 'dob' => $_POST['dob']); $enrollment_array_class = $_POST['class']; //Set DB access variables $host = "localhost"; $user = "***"; $pass = "***"; $db = "userDB"; $table = "students"; //Set Array as variables //Escape for safety $DBfname = mysql_escape_string($enrollment_array['fname']); $DBsname = mysql_escape_string($enrollment_array['sname']); $DBdob = mysql_escape_string($enrollment_array['dob']); $DBclass = mysql_escape_string($enrollment_array_class); //Open Connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); //Select DB mysql_select_db($db) or die ("Unable to select db!"); //Create query $query = "INSERT INTO $table (fname, sname, dob, class) VALUES ('$DBfname', '$DBsname','$DBdob', '$DBclass')"; //Execute query mysql_query($query) or die ("Error in query: $query.".mysql_error()); echo"Added new student!<br />"; //Close connection mysql_close($connection); OK this is the script I have, This is just a small (or not so!) project of mine im using to learn the ins and outs of PHP and mySQL so please forgive me for being a noob! I have a form with 3 text input (fname, sname, dob) and an array 'class[]' of 7 checkboxes. One must be ticked but no more than three. Ive already got all the error checking sorted (mostly) for form inputs but now I'm struggling to figure out how to input my class[] array into my 'class' column in the DB. The 'class' column is of the SET data type and Ive set up the allowed strings correctly as far as i know. When i run the script it gives no errors and even adds a student to the table but the 'class' column is empty. I think I'm going wrong with these two: $DBclass = mysql_escape_string($enrollment_array_class); $query = "INSERT INTO $table (fname, sname, dob, class) VALUES ('$DBfname', '$DBsname','$DBdob', '$DBclass')"; I'm assuming that there is a mysql_escape_array function or something similar? If not i could maybe do a for-each on the array and escape each string one by one into a new array? With the query i think i might need to do something similar to this but im not sure how to write it in php: INSERT fname, sname, dob; select row from table with matching fname, sname, dob; if rows returned = 1 { add class array to class column on row matching returned ID using something like: INSERT INTO $table (class) VALUES ($DBclass); } else { ERROR - either no matching student OR duplicates exist } I know this is asking quite a bit, I'm trying to be as clear as i can. Thanks for reading! Nathan
  21. It works, perfectly actually! Thank you very much! One more question, i know this is off topic and also HIGHLY likely to be answered already in this forum ... Is there a good FREE program to help identify spelling mistakes and missed brackets etc? My programs are starting to get a little bigger and re-reading line after line after line to find one missing ) is getting quite frustrating! I'm currently using gEdit on my Ubuntu laptop. I believe there is some built in function of PHP (i could be wrong) to help with this stuff but I'm fairly new to this and have not yet gotten round to finding out how to make it work for me.
  22. Thanks Denno, thats what i wanted! was just confused by the flow of logic at this point. Ill get to it later and let you know how i got on!
  23. have you checked around on the net to see what gmt currently is? i suspect your problem is daylight savings as you say. you might just have to write bit of code to enable/disable daylight savings?
  24. Login.php <?php if (!$_SESSION['auth'] == 1) { if (!isset($_POST['login_submit'])) { //If login has NOT been submitted then { ?> <form name="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div id="username" style="width:500px;"> <div name="box1" style="text-align:right;height:27px;width:250px;float:left;"> Username: </div> <div name="box2" style="text-align:center;height:27px;width:250px;float:right;"> <input type="text" name="userlogin"> </div> </div> <div id="password" style="width:500px;"> <div name="box1" style="text-align:right;height:27px;width:250px;float:left;"> Password: </div> <div name="box2" style="text-align:center;height:27px;width:250px;float:right;"> <input type="password" name="userpass"> </div> </div> <div id="submit" style="width:500px;"> <div name="box1" style="text-align:center;height:27px;width:250px;float:left;"> </div> <div name="box2" style="text-align:center;height:27px;width:250px;float:right;"> <input type="submit" name="login_submit" value="Log In"> </div> </div> </form> <?php } else { if (empty($_POST['userlogin'])) { die ("ERROR: Please enter username!"); } if (empty($_POST['userpass'])) { die ("ERROR: Please enter password!"); } // set server access variables $host = "***"; $user = "***"; $pass = "***"; $db = "***"; $table = "***"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM $table WHERE username = '" . $_POST['userlogin'] . "' AND password = '" . $_POST['userpass'] . "'"; // execute query $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // see if any rows were returned if (mysql_num_rows($result) == 1) { // if a row was returned // authentication was successful // create session and set cookie with username session_start(); $_SESSION['auth'] = 1; setcookie("username", $_POST['userlogin'], time()+(84600*30)); echo "Access granted!"; } else { // no result // authentication failed echo "ERROR: Incorrect username or password!"; } } } else { echo "You are logged in! <a href=include/logout.php>Logout</a>"; } ?> This is a php file that is included inside a <div> on the actual webpage. It works fine but if i have an error id like to display the form with the error message below. Right now the form dissapears and leaves me with just an error message. Any ideas? I was thinking about making the 'display form' part of the code a function and then calling it + my errors when an error is returned. Not sure if this is the best way to do it or not. Thanks for reading, any relpies are much appreciated!
  25. <b>Enroll a student:</b> <div id="content" style="width:100%;"> <form name="enroll" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div name="user_info" style="background-color:#f0f0f0;height:35px;width:100%;"> <div name="first_name" style="background-color:#ff0000;height:35px;width:20%;float:left;"> First Name:<input type="text" name="firstname" size="8"> </div> <div name="surname" style="background-color:#00ff00;height:35px;width:20%;float:left;"> Surname:<input type="text" name="surname" size="8"> </div> <div name="dob" style="background-color:#0000ff;height:35px;width:20%;float:left;"> D.O.B:<input type="text" name="dob" size="8"> </div> </div> <br> <div name="class_info" style="width:100%;"> <div name="class_info_header" style="text-align:left;width:100%;"> Class: </div> <div name="class_col_1" style="height:100px;width:50%;float:left;"> <li><input type="checkbox" name="class[]" value="english">English<br> <li><input type="checkbox" name="class[]" value="maths">Maths<br> <li><input type="checkbox" name="class[]" value="science">Science<br> <li><input type="checkbox" name="class[]" value="sports">Sports<br> </div> <div name="class_col_2" style="background-color:#ff0000;height:100px;width:50%;float:left;"> <li><input type="checkbox" name="class[]" value="history">History<br> <li><input type="checkbox" name="class[]" value="geography">Geography<br> <li><input type="checkbox" name="class[]" value="ict">ICT<br> </div> <div name="submit_button" style="background-color:#ababab;text-align:left;width:100%;"> <input type="submit" name="enrollsubmit" value="Enroll!"> </div> </div> </form> </div> Can anyone tell me why this doesnt work? im making a simple php script but i just wanted to lay it out nice first. I cant get the button to center, and also i cant see why the whole class_info div is inheriting the colour of submit_button div. Im lost!
×
×
  • 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.