Jump to content

GingerRobot

Staff Alumni
  • Posts

    4,082
  • Joined

  • Last visited

Everything posted by GingerRobot

  1. You can use the printf()/sprintf() functions to format variables for output to the browser. <?php $po = 4; $po1 = 99;//i assumed the max here would be 99 if its supposed to be two digits? printf('%.2f',rand(0,$po).'.'.rand(0,$po1));//the .2 specifies 2 decimal points ?> As for your second question, im not entirely sure what you mean. If you were wanted to sometimes use the u1 value and sometimes use the u2 value in your loop, you could always do: <?php if(rand(0,1)==0){ //use u1 }else{ //use u2 } ?> But as i say, im not entirely sure thats what you wanted.
  2. Yes, if you need to have an automated process that runs every 30 days, use a cron job. However, this might not be a necessity. For example, if you need to make something appear to happen every 30 days to a user, you can check if 30 days have passed since the event each time they log in perhaps. If 30 or more days have passed, you can perform your process. It rather depends on the situation.
  3. You're missing a where clause from your UPDATE query. Im guessing it was supposed to be: <?php $q = "UPDATE table_stories SET edition_id = '$new_edition_fotmat , created_stamp = '$edition_year-$edition_month-$edition_day' , modified_stamp = '$edition_year-$edition_month-$edition_day' , published_stamp = '$edition_year-$edition_month-$edition_day' WHERE story_id = '$storyid' "; mysql_query($q); ?>
  4. I think you just need to get the variables from the $_POST array at the top of your script: <?php $rn = $_POST['rn']; $guess = $_POST['guess']; //the rest of your script Also, for future reference, put your code in tags(without the spaces) - makes it a whole stack easier to read.
  5. I think what they are implying is that you may, have for instance, 2 constants: foo and bar. Perhaps dependant on user input, or the result of some other variables, you would sometimes want the constant that is foo and another the constant that is bar. I've found variable variables most useful when working with databases - most other times its easier to use arrays. For example, i had made a script which allowed up to 4 people to play a game of blackjack. As you can imagine, the database had fields like : player1, player2, player1_cards, player2_cards, player1_status etc. Either when accessing data from the database, or updating the database, it was much easier to use variable variables: <?php function get_status($player_no){ $field = 'player.'$player_no.'_status';//for example, if i passed in number 1 for player_no, the variable would contain player1_status return $$field;// so if i pass player_no as 1, i would, in effect, be echoing the variable $player1_status } ?> Thats just a completely rough example which was nothing like how it worked. However, i felt given a little bit of context, it might show how you could use them.
  6. I think you're after the variable variable: <?php $page_name = "Home"; $left_column_Home = "1"; $var = 'left_column_'.$page_name; if ($$var == 0) echo ""; else include_once ("left_column.php"); Edit: Or ,as frost says, you could use arrays.
  7. To be honest, im a little confused as to what you are trying to output. But you'll be wanting to use a loop something similar to: <?php for($x=0;$x<=count($FormValues);$x++){ $FormValue = $FormValues[$x]; $DBValue = $DBValues[$x]; } ?> p.s. 700th post. Woo
  8. Yeah i'd agree with thorpe that the closest approximation is toy boy. Theres actually a fairly detailed wiki article on the term and probably explains the lack of male equivalent: http://en.wikipedia.org/wiki/Mistress_%28lover%29
  9. It depends on the particular definition of mistress. If it's the title mistress, then i think the male is master. If we're talking a mistress as in a lover etc then im not sure there is an equivalent.
  10. Thats not actually true is it? Tested with: <?php $submit = $_POST['submit']; if(isset($submit)){ echo 'form has been submitted'; }else{ echo 'form has not been submited'; } ?> <form action="phpfreaks.php" method="post"> <input type="submit" name="submit" /> </form> The first time you load the page, you get the next 'form has not been submitted'. Once you submit, it displays 'form has been submitted'.
  11. To answer, the question, no, there does not need to be an id='something' statement. We are using a list of possible values, and the IN clause. It is effectively like writing id='something' or id='somethingelse' or id='somethingmore' etc, but much easier to work with. Whenever you have a query that's not working try some debugging: <?php $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; mysql_query($sql) or die(mysql_error().'<br />query'.$sql); echo '<br />.$sql.'<br />'; ?> Kind of a double check here, since we dont know if the query is failing or not as yet. If it does, we will get the mysql error and the contents of your query, if it doesn't we will just get exactly whats being passed into the query.
  12. There is a two part tutorial on this site for this kind of thing: Part 1: http://www.phpfreaks.com/tutorials/107/0.php Part 2: http://www.phpfreaks.com/tutorials/123/0.php Hope it helps.
  13. Thorpe's code is just missing a fullstop - the concatenation operator. Try: <?php $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; ?>
  14. This was annoying me so i decided to sort it out. Firstly though, there is actualy a problem with the code for the first thursday. If the first thursday happened to be the first day of the month, currently we would get the 2nd thursday, as the function passes in the first day of the current month and then asks for NEXT thursday. So, in the mktime function, we actually need to pass in last month, and the last day of the last month: $firstthurs=strtotime("next thursday",mktime(0,0,0,date("n")-1,date('t',mktime(0,0,0,date('n')-1)))); Anyways, heres what i came up with. Im pretty sure it works: <?php $curr_month = date('m'); $curr_day = date('d'); $firstthurs=strtotime("next thursday",mktime(0,0,0,date("n")-1,date('t',mktime(0,0,0,date('n')-1)))); $thirdthurs_day_no=date('d',$firstthurs+60*60*24*14);//need third thursday rather than last, so add two weeks $firstthurs_day_no = date('d',$firstthurs);//get day number if($curr_month >= 11 && $curr_day > $thirdthurs_day_no){//check to see if we are past the last meeting of the year - the 3rd thursday of november $next_meeting = strtotime('next thursday',mktime(0,0,0,12,31,date('Y'))); $next_meeting = date('d F Y',$next_meeting); }else{ if($curr_day < $firstthurs_day_no){//before first thursday of the month $next_meeting = date('d F Y',$firstthurs); }elseif($curr_day < $thirdthurs_day_no){//between 1st and 3rd thursday $next_meeting = date('d F Y',$firstthurs+60*60*24*14); }else{//after 3rd thursday of current month, so next meeting is 1st thursday of next month $next_meeting =strtotime("next Thursday",mktime(0,0,0,date("n"),date('t'))); $next_meeting =date('d F Y',$next_meeting); } } echo 'The next meeting is on '.$next_meeting; //produces The next meeting is on 19 July 2007 ?>
  15. I was meaning sending the data using POST via cURL. As far as i know, there is no character limit when using POST is there?
  16. You can get away with using the strtotime() and date() functions: <?php $timestamp = strtotime('next thursday'); $date = date('F d',$timestamp); echo 'The next meeting is on '.$date; //produces The next meeting is on July 12 ?> You'll need to add in something to miss out any dates in december though. EDIT: Whoops - i just realised i really didn't think this through, it wont work properly. Tis too late for me
  17. Ok, and are you storing the usertype using text (e.g. staff, member) or by number? And the same for the hidden field? Going back to the code, however, i would suggest that you do check your query was successful with an or die statement, just in case. You also need to be using a while loop rather than a foreach, as was pointed out earlier in the thread. The code inside the loop needs to be finished too - i only started it off for you. You need a few more if statements - for example you need to see if people are hidden as , if i remember correctly, you wanted a separete counter for that. Finally, you need to echo the variables after the while loop: <?php $mysql2 = "SELECT * FROM forumusers ORDER BY id DESC LIMIT 1"; $result2=mysql_query($mysql2) or die(mysql_error());//checking to see if the query was successful $staff = array(); $members = array(); $num_staff = 0; $num_members = 0; $num_guests = 0; $num_hidden = 0; while($row = mysql_fetch_assoc($result2)){//you need a while loop here - my mistake originally $usertype = $row['usertype']; $username = $row['username'];//also assuming you have some field for the username $hidden = $row['hidden'];//and perhaps a separate field for their hidden status if($usertype == "staff" && $hidden != 'hidden'){//this may be a number representing staff rather than the text staff $staff[] = $username; $num_staff++; }elseif($usertype == "member" && $hidden != 'hidden'){ $members[] = $username; $num_members++; } //this is unfinished } echo 'Number of staff online: '.$num_saff.' | Number of members online: '.$num_members; ?> I get the feeling you're wanting someone to do all this for you. If you are, then try the freelance forum. Most people here will help people that help themselves.
  18. Do you think we could see your code that you have? Dont forget to enclose it in tags(without the spaces) However, to answer what i think is your question, say you had the following radio button: [code] <input type='radio' name='yourfield' value='option1' /> Then to check if this option had been selected, you would use: <?php if($_POST['yourfield'] = 'option1'){ //option1 selected } ?> Of course, if your form method was get, use the $_GET array.
  19. As you hopefully noticed, i made a lot of assumptions here about the way your database was set up. Could you clarify how you are defining/storing each of the differant user types e.g. your database structure?
  20. Hello, im ben, and im an alcoholic. Oh wait, im not at my AA meeting am i? :p Anyways, continuing after my rather poor joke, i'm 17, male and live in the UK. I got started with php about 2 years ago after seeing my dad struggle to use a pre build emailer for a website he was making. After finding out about that, i set about making a text based game, which worked well although i didn't have the time to make anything of it. Currently, most of the php work i do is actually trying to sort out problems on these forums - i swear most of the php i've learnt has come from seeing questions posted and their answers. Im in the first year of 6th form and plan on going to uni after my a-levels to do a computer science degree(hopefully at oxford). For some unknown reason, i quite like running - i run up to 10k(6.2 miles for you imperial folk). I also very much like my music - mostly indie/alternative although i do like a bit of everything really.
  21. Sure: <?php $result = mysql_query($query); //echo "Number of effected rows: $sql_num_rows"; if($result === false) { header("location: /CP/errorpage_norecords.htm"); exit; } $num_record = mysql_num_rows($result); $sql_num_rows = mysql_num_rows($result); if($sql_num_rows < (1)) { header("location: /CP/errorpage_norecords.htm"); exit; } ?> The problem is that if there is an error in your sql, then the mysql_num_rows function is going to give you an error when you try it. Therefore, you either need to supress that error(the first code) or only use the function once you know there is no error in the code(the second). Incidently, why do you use the mysql_num_rows() function twice? Even if you do need two variables with the information in then this would be quicker: $num_record = mysql_num_rows($result); $sql_num_rows = $num_record; The mysql_num_rows function is not all that quick, doing it this way avoids calling the function twice.
  22. The mysql_query() function returns false if there is an error. Therefore, if you remove your or die statement, and then modify your if statement, then you can redirect: <?php $result = mysql_query($query); //echo "Number of effected rows: $sql_num_rows"; @$num_record = mysql_num_rows($result); @$sql_num_rows = mysql_num_rows($result); if($sql_num_rows < (1) || $result === false) { header("location: /CP/errorpage_norecords.htm"); exit; } ?> I have placed @ symbols before the mysql_num_rows to supress errors, because these would also produce an error if there is a problem with your query. Alternatively, you can have two if statements, the first checking for success on the query, the second checking to see if any rows are returned.
  23. When you include a file, it is parsed as if the contents of the included file was already in the file that is calling your include, and is wherever you call it. Therefore, the placement depends on what it does. For example, if it were to output some text(Copyright jim.davidson 2007 for example), then you'd obviously want to include this wherever you wanted the copyright notice displayed.
  24. Well, you can always go one step further with the building of a string, and add each name to it as you go along. You then strip off the last character which will always be the comma.
  25. You can get the next 12 months as numbers and text without using arrays, by using the date() and strototime() functions. Might save some typing : <?php $curr_month= 7; for($x=0;$x<12;$x++){ $month_num =($x+$curr_month > 12) ? $x+$curr_month -12 : $x+$curr_month; echo date('m',strtotime($month_num.'/01')); echo ' - '.date('F',strtotime($month_num.'/01')).'<br />'; } ?> Produces: 07 - July 08 - August 09 - September 10 - October 11 - November 12 - December 01 - January 02 - February 03 - March 04 - April 05 - May 06 - June
×
×
  • 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.