Jump to content

Phear46

Members
  • Posts

    30
  • Joined

  • Last visited

Phear46's Achievements

Newbie

Newbie (1/5)

1

Reputation

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