Jump to content
Larry Ullman's Book Forums

Roxana

Members
  • Posts

    17
  • Joined

  • Last visited

Roxana's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. Oh, thank you. I don't know how I missed it...
  2. Can I implement the code from the book using php 5.2.17? I must have inadvertently missed this information in the introductory section of the book.
  3. I tried to use MailChimp but my newsletter has 4 options for the email (the user can pick 1,2,3 options or all of them) that makes 15 combinations. Maybe I was hasty but MailChimp doesn't seem to offer more than one combination. I used to send this email manually, and I couldn't change the format later. I will check out SendGrid though.
  4. I send a newsletter. Sometimes (maybe it depends how busy the server is?) when I send the newsletter, php will stop running and tell me that it's running time has been too long and it has to stop. Should I send the emails to fewer people in more than one run or is there a better way to do it?
  5. I have 2 sites. For the first one I created a newsletter. People register and I send them the newsletter from another webpage by using the mail() function. My problem is that 90% of users have yahoo emails. Yahoo does not accept emails from this first server. After a lot of useless discussion, I bought a second site, from which yahoo accepts emails. I thought I might move the whole site at first, but I have several databases and was afraid the difference in mysql and php versions might make some commands in php and mysql unusable. So now users are registering on site no1 into a database and are receiving email from site no2 from another database. Server1 has a 4.4.8 php version, version 4 for mysql - maybe that is why yahoo won't accept them. Server2 has a 5.2.17 php version, version 5 for mysql - I haven't been able to find a host with 6 for php yet. I never tried but could it be possible to connect to mysql on server 2 from the php on server1? db, username, password are all known so these emails would be saved in the right table at registration and I wouldn't have to import the table from time to time and keep two tables.But, even if possible with different versions, that would be like hacking my own site. Would the webhost mind? Why? Is there a better solution?
  6. I once tried and couldn't. I concluded that you probably need to create your tables so that this is not needed. But can you? Was I making some other mistake and in fact it is possible?
  7. This is the code. Since I am still using the dummy table, anybody can check out the page. For eng143 write rom143 (for instance) to get a correct answer. I checked it and double checked it, but it you find any mistakes or have any comments please let me know. I will trim spaces and decapitalize the answers soon. Also, in the end the page will have a menu so going to another page will not be a problem. http://grafitgroup.ro/dictionary11.php Thanks everyone. (I did not include the small html codes) <?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past if (isset($_POST['submit'])) { session_start(); function rezultat($rez){ echo $_SESSION['id'].".".$_SESSION['eng'][$_SESSION['crtnr']]." - ".$_SESSION['rom'][$_SESSION['crtnr']]."<br> Raspunsul tau: <i>".$_POST['rom']."</i> este ".$rez.$_SESSION['corecte'];"<br><br>"; } $_SESSION['id']=$_SESSION['id']+1; if ($_POST['rom']==$_SESSION['rom'][$_SESSION['crtnr']]){ $_SESSION['corecte']=$_SESSION['corecte']+1; rezultat('corect!'); }else{ rezultat('incorect'); } $_SESSION['crtnr']=$_SESSION['crtnr']+1; }else{ session_start(); mysql_connect("mysqldb.com", "username", "password") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $i=0; $result = mysql_query("SELECT * FROM example") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $eng[$i]=$row['eng']; $rom[$i]=$row['rom']; $i++; } for($i=0;$i<200;$i++){ $ord1[$i]=$i; } shuffle($ord1); for($i=0;$i<200;$i++){ $eng1[$i]=$eng[$ord1[$i]]; $rom1[$i]=$rom[$ord1[$i]]; } $_SESSION['eng']=$eng1; $_SESSION['rom']=$rom1; $_SESSION['crtnr']='0'; $_SESSION['corecte']='0'; $_SESSION['id']='0'; } if ($_SESSION['id']=='20'){ unset($_POST['submit']); include ('common2.html'); $_SESSION['corecte']='0'; $_SESSION['id']='0'; }else{ include ('common1.html'); } ?>
  8. Ok - I see the differences. But my SESSION problem remains: Here is the code I wrote. <?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past if (isset($_POST['submit'])) { $crtnr++;// increment the current number (counter) echo $_SESSION['array'].'<br>';//for now just show this saved variable exists. echo 'hi';//if the above echo didn't work, at least print 'hi' to show the conditional was validated. }else{ session_start(); mysql_connect("mysqldb.com", "username", "password") or die(mysql_error()); mysql_select_db("mydb") or die(mysql_error()); $i=0; $result = mysql_query("SELECT * FROM example") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $array1[$i]=$row['eng']; $i++; $_SESSION['array']=$array1[1]; //For the moment I am only storing the first entry, that will change when it works. } $page_title="Initial Method"; $php_action="dictionary0.php"; $nr=20;//number of words that will be displayed $crtnr=0; // initialize counter //RANDOMIZE: .... } include ('common1.html'); ?>
  9. Hi, Thank you for your advice to work in JS (which I can manage) but because I want to perfect my PHP (that is why I am reading this book) I will continue with php, if possible. Now, I havr the following 2 problems: 1. So far my php code has an if else conditional as explained in the book: if (isset($_POST['submit'])) { $crtnr++; echo $_SESSION['array'].; echo 'hi'; }else{ ... main code ... download table...shuffle array (must happen just once) $_SESSION['array']=$array1[1];// This is probably why you said I need to save the data, so it will exist when the if condition validates. } However, when the if validates this value is not displayed. Second Problem: 2. If I reload the page the $_POST['submit'] probably is not erased and I cannot restart the vocab. I have to open a new window. I use this: header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past but it does not help. http://grafitgroup.r...dictionary0.php
  10. I'm back and here is what I've done: I have a site that teaches people English (I'm Romanian). Once I decide on the best code for generating random words, I will make a page that tests one's vocabulary - by generating one word at a time for the user to translate. I created a dummy database containing english words and their translations. I do not know if I should use sessions - I read that sessions are used mostly to save data when changing pages, I will not have multiple pages. I might use cookies when the page is done to ask returning visitors the words they did not know last time. But apart from that, could someone explain why I would need them? Data security doesn't matter, I would think. Since there are quite a few ways of doing this and I don't know which is best, I developed them all. I came up a few years ago with a code that generates random numbers, but I never was completely pleased with it, a while in a for loop seemed too much. if (isset($_POST['submit'])) { $nr=$_POST['nr']; for ($i=0;$i<$nr;$i++){ //initialize the position array, that will hold the positions of chosen numbers $ord0[$i]=0; } for ($j=0;$j<$nr;$j++){ //the loop that finds the numbers $param=0; //flag while ($param==0){ $i=rand(1,199); //generate one random number if ($ord0[$i]!=-1){ //if generated number is new $ord1[$j]=$i; //save it in the array of randomized numbers echo $j.'-'.$array1[$i].'<br>'; $ord0[$i]=-1; //update the position array $param=1; //stop while (break) } } } } http://grafitgroup.r...dictionary0.php Then, yesterday I found the Fisher-Yates algorithm, which has just one loop, I found it here for Javascript http://sedition.com/...ascript-fy.html (I had to change the --i to i-- in php, though it says the --i is the right way for their javascript implementation.) if (isset($_POST['submit'])) { $nr=$_POST['nr']; while ($nr--) { $j = rand(0,$nr); $tempi = $array1[$nr]; $tempj = $array1[$j]; $array1[$nr] = $tempj; $array1[$j] = $tempi; echo $nr .'-'.$array1[$nr].'<br>'; } } I think this is a fantastic method because it moves the numbers found to the back of the list and shortens the randomization interval each time so that the numbers found cannot be found again. It is obviously better than my code. It would need to run all the way to 200 though and either be stopped somewhere along the way or pick the first $nr of numbers after. http://grafitgroup.r...dictionary3.php Then there is the method that Larry's book inspired me to try, using rand() $i=0; $result = mysql_query("SELECT * FROM example ORDER BY RAND()") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $array1[$i]=$row['eng']; //save the randomized numbers/words in an array $i++; } if (isset($_POST['submit'])) { $nr=$_POST['nr']; for ($i=0; $i<$nr; $i++){ echo $i .'-'.$array1[$i].'<br>'; //print array } } http://grafitgroup.r...dictionary4.php And last but not least, there is Larry's method, which is very good if I change the words on a daily basis, for instance if I have 20 Words of the Day which the user must know. I created a new table with random numbers which I joined with the words table. The random number table can be smaller than 200 rows. $result = mysql_query("SELECT example.eng, aleator.id FROM example,aleator WHERE example.example_id = aleator.numar") or die(mysql_error()); if (isset($_POST['submit'])) { $nr=$_POST['nr']; while($row = mysql_fetch_array( $result )) { echo $i .'-'.$row['eng'].'<br>'; $i++; if ($i==$nr){ break; } } } http://grafitgroup.r...dictionary5.php How do I compare these methods in terms of speed in the case of one or many simultaneous visitors?
  11. I'm back. This is how I learned to access a table a few years ago: $result = mysql_query("SELECT * FROM users") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $myvalue = $row['email'] .... } where of course $myvalue could also be an array.and all its values would still exist out of the loop. I'm reading chapter 6, and in chaper 5 the book says: SELECT email FROM users ORDER BY RAND() LIMIT 1 So then: $i=0; $result = mysql_query("SELECT email FROM users ORDER BY RAND()") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $myvalue[$i] = $row['email']; $i++; } Would this automatically randomize my data? All at once? And save it in an array? No more need for flag arrays to keep track of the selected entries? Would I still need cookies? My coding will be so much better by the time I finish this book! Up till now I would generate a random number with php and pick the primary key that had that value in the table.
  12. Yeah, you are right about cookies. I couldn't even use the new column correctly if many visitors were there at the same time. Thanks. I've worked a little with cookies before, I will keep on reading and if I need help or I want to show you my code I will be back.
  13. Whenever I randomize the values of an array, I create a second array that acts as a flag, so no value gets repeated. Whenever a value is chosen from the main array, the second array gets a one (for instance) in the corresponding place. When working with tables what should the solution be? Should I save the table entries in an array and do as I said above or should I create a temporary column that does the same thing?This randomization would restart every time somebody acceses the page, so in the end the column would be deleted. For instance a piece of advice offered by the computer each time someone presses a button.
  14. I started using php 4 years ago. Back then I read that to send data from a form to the php file that processes it, you need javascript to validate the data, and if you don't want to leave the current page, you need ajax to send the validated data to the second php file (I read this on a site that is very good for absolute beginners, so I won't name it) . I've been using this complicated technique ever since. I thought you had to use javascript just because otherwise the page would change and (thought I) the user couldn't modify the new page - except by going back.. Now, in chapter three I find out how simple this can all be done. Am I missing something here? Is Ajax ever needed?
×
×
  • Create New...