Hello everyone,

I stumbled upon a video about the monty hall problem, which I found very well explained, and a lot of comments below the video that were against the conclusion that it is better to switch. To clarify, I wrote this small program that simulates the game and allows everyone to test it for themselves.

I got the following results:

+ Calculation Preferences +

Enter the amount of repetitions:        3000
Do you want to switch choice? [Y/N]     y

+ Calculation Results +

Amount of repetitions:  3000
Correct choices:        2008
Wrong choices:          992
Winpercentage:          66.933333


+ Calculation Preferences +

Enter the amount of repetitions:        3000
Do you want to switch choice? [Y/N]     n

+ Calculation Results +

Amount of repetitions:  3000
Correct choices:        1013
Wrong choices:          1987
Winpercentage:          33.766667

So the conclusion is really self-explanatory :)



I don't know much about JSON format, but the replace method only works once, and you need to repeat it in order to replace all (google it).


<script type="text/javascript">

var str="Visit Microsoft! Microsoft";



Visit W3Schools! Microsoft 

So only the first one is replaced (curtisy of w3schools, I was too lazy to fire up editor)



So you have an image, a link that surrounds it and you want to record whether a visitor clicks on it?

The easiest way is to make a link to a PHP page, e.g. referer.php. This page would take a link as argument and then increment the amount of clicks for that link in a database:

[CODE]<a href='referer.php?link=http://www.mysite.com'><img src="http://www.mysite.com/banners/2-12/160x600_5.jpg" width='160' height='600' border='1' alt="Click to Visit" /></a>[/CODE]

And then in referer.php you retrieve the link using GET, increment a value in a table (something like 'referer_counter') with columns 'link' (string) and 'counter' (integer) that corresponds with the link and then automatically redirect (google how to do it) the visitor to the correct link.

PS: If it's a rotation banner, you need to change the href everytime a new image is loaded.




I've been trying to learn what pointers are and how they function, and I now finally understand most of it. However, I don't understand the assignment of strings to a char *, and what the values they have mean. I wrote a small program to learn:

[CODE]#include <stdio.h>

include <stdlib.h>

include <string.h>

int main()

int a = 60;
int * p;
p = &a;
printf("Value (with asterisk): %d\n", *p);
printf("Location (without asterisk): %d\n\n", (int) p);

char a1 = 'c';
char * p1 = &a1;
printf("Value (with asterisk): %c\n", *p1);
printf("Location (without asterisk): %d\n\n", (int) p1);

char * p2 = "Some string";
printf("Unknown (with asterisk): %d\n", *p2);
printf("Location (with ampersand): %d\n", (int) &p2);
printf("Value (without asterisk): %s\n\n", p2);

int * p3 = "47352";
printf("Unknown (with asterisk): %d\n", *p3);
printf("Location (with ampersand): %d\n", (int) &p3);
printf("Unknown (without asterisk): %d\n", (int) p3);

return 0;


I've marked the values I don't understand as 'Unknown'. It seems as if a char that has a string assigned to it, is sort of the same as a regular type (like int), because it it's value is p2 and it's location &p2. But I don't understand how. I also tryed the same trick with a int , but the results are not the same (the 'without asterisk' is not the correct value of 47352)

I've googled many tutorials, but none can explain how this really works! If someone could send me a tutorial that does, it would really help me out :)


PS: ...


I've tried adjusting the modus for file reading/writing, but it did not solve the problem: the file copy is still stopped after 705 bytes.

If I understand correctly, the difference in speed between fread/fwrite and getc/putc is the same? But isn't the getc opening the file each time it gets a character and then reopens the stream, in contrary to fread that would only need to access it one time? I think I'il try the fread/fwrite method in this case.

Assuming I find a solution to the EOF problem (probably using fseek, suggested by DeanM), will fread work correctly with really large files (e.g. 3GB +), as it would allocate an absurd amount of virtual memory if it does it all at once. Would it be better to do read/write every 50MB or so? Or should I depend it on the virtual memory available (and if so, how do I measure it with standard C functions)?

[B]Edit[/B]: gerard, if EOF is a standard value returned if the file-read has ended, why does it stop after 705 bytes?



Hi everyone,

I'm currently working on a cross-platform project to copy data from the computer onto a USB drive quickly.

[B]Main problem: EOF in the middle of the file[/B]

It's going good so far, but I stumbled across a problem in my copy_file function:

[CODE]int copy_file(char source_path, char destination_path) {

FILE *input;
FILE *output;

char temp1[MAX_PATH] = {0};
strcpy(temp1, source_path);

char temp2[MAX_PATH] = {0};
strcpy(temp2, destination_path);

if ( (input = fopen(temp1, "r")) != NULL ) {

    if ( (output = fopen(temp2, "w+")) != NULL ) {

        /* Copying source to destination: */
        char c;
        while ( (c = getc(input)) != EOF ) {
            putc(c, output);

    } else {

        /* If the output file can't be opened: */
        return 1;

} else {

    /* If the input file can't be opened: */
    return 1;


return 0;


It works fine for regular formatted files such as WORD-documents and text files, but with movies it stops after about 705 bytes, although the file is 42MB. I think it's because there is a EOF character in the middle of the file which breaks up the while loop. Does anyone know how to solve this?

[B]Secondary issue: speed[/B]

In regards to speed, I need to write code that gets the job done as fast as possible. This was the simplest copy function I could think of, but I don't know whether it is the fastest. What is faster, fread/fwrite or getc/putc ? Or is there another standard function that ...


You have it figured out pretty good by yourself, but to help you in the right direction, think of something like this:

[CODE]$filename = "D:/some/folder/example.txt";
$text = "<a href='delete.php?file=".$file."'>Delete ".$file."</a>";[/CODE]

And apply that to your own script (thus making a new page called 'delete.php' that retrieves the filename from the URL using GET and deletes it)




A few things you might consider:

First debug your code and find the source of your problem, then post the [B]small[/B] piece of code that is flawed so that we do not have to spit through all your code (which I, for one, am not going to)
If you are a novice in JavaScript, you might consider starting with easier things instead of adapting difficult code.
If you did not write the script, we can not help you fix it. Seek help at the author of the script.



When I meant 'indenting & spacing', I meant it a bit differently than you did it (see the code). Also you can't compare dates using the strings, you first need to convert them to UNIX timestamps (see [url]http://php.net/manual/en/function.strtotime.php[/url] for more explaination).

/ The date limit when the page needs to be redirected: /
$timelimit = '2012-02-10 00:00:00';

/ The current date: /
$timenow = date('Y-m-d H:i:s');

/ Comparing the two dates: /
if ( strtotime($timenow) > strtotime($timelimit) ) {

/* Redirecting user to another page: */
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( "Location: http://www.redirectsite.com" );

} else {

/* Showing page content: */
echo "http://www.contestpage.com"; 



As for the redirecting, I think it is either this or a JavaScript redirect (which is client side). Try googling it.



That is right, the code I provided needs to be implemented before it works.

If you are not the author of this code, and bought it from someone (or just copied it?), you can ask him to rewrite the code. The purpose of this forum is to help people to fix their problems by giving clues and advice, not to fix the problems for them.



[QUOTE]Wouldnt that require 5 new fields in members ?[/QUOTE]
No, only 1 field: permission. E.g.

Username Password Permission

Dude secret 2
Guy cube 0

And so on.

[QUOTE]And then how would i assign a user either 0, 1, 2, 3, 4 or 5? [/QUOTE]
When you insert an user to the database, for example when he registers, he is standard assigned member (0) permission. You could write a script that allows users to assign permission to users below its own permission (e.g. an superadmin can make a member admin, but can't make a member superadmin or president).

You can make it as complex as you want.



You could also just extend the members table and add a new column named 'permission' that is an integer. The integer corresponds with the level of authentication:

0 :: Member
1 :: Admin
2 :: Superadmin
3 :: President
4 :: Superman

Something like that. And in the code you can test that value: [ICODE]if ($permission > 2) { ....... }[/ICODE]



Your query is a total mess. Using sprintf() is handy in various languages, but as PHP is very flexible with strings, it is completely useless in SQL in my opinion. And if you are going to use it, use it correctly, consult the PHP manual (php.net).

Make it something more nicely put together, and add some comments ;)


$new_password :: The new password
$old_password :: The old password
$email :: The email address (used as username)

[CODE]$query = "UPDATE members
SET password='".$new_password."'
WHERE email='".$email."' AND password = '".$old_password."'";[/CODE]



Hmm... I really thought the problem would lay there.

If that isn't the issue, I can't really give you a straight solution, you will first need to debug the code and check whether everything is received correctly and compared correctly:

$user = mysql_real_escape_string(stripslashes($_POST['username']));
$pass = mysql_real_escape_string(stripslashes($_POST['password']));

echo "Received form values: user='".$user."', pass='".$pass."'<br />";

// You retrieved the form values already, no need to retrieve them again: use the existing variables
$select = "SELECT * FROM users where username='".$user."' AND password='".md5($pass)."'";

echo "Query: '".$select."'<br />";

$msq = mysql_query($select) or die(mysql_error());


echo "Total: '".$total."'<br />";

This HAS to provide you the solution, as there isn't anything obvious wrong with the script (except some neglected comments and white-spacing).

Make sure that the user and the MD5-HASHED password correspond with the entry in the database (now I come to think of it, have you entered the md5 hash of the password into the row?)



Your code isn't comparing correctly. You want to see whether text needs to be shown using the 'if' condition, but you are comparing a date? You are also using the assign operator '=' instead of the comparing operator '==':

Line 4-10 should be:
[CODE]if ($showtxt == false) {
} else {
echo "Page content";

I'm not sure whether you can compare dates like that, but I guess you can find that out by yourself. You also might consider indenting & commenting your code!



The problem isn't that your database isn't connected, but that the database does not have the same data as your local one.

For example there is a user 'jack' in the local database, but not in the hosted database. Or the passwords are not the same.

There is nothing wrong with the script.



Script looks fine. If I have difficulties when I put a script online it is mostly because of these two reasons:

The database is not the same as the local one (this is the most probable cause in your case)
I forgot to upload another essential file
There is an unknown absolute path

Hope this helps :) I think your database doesn't have a record of user '****' with password hash "jdioafu928749820480fa890'(or something like that).



The errors you describe are all caused because you messed up the ; and quotes. I spotted one when I was looking through it:

Line 195 >> The query must be encapsed in quotes, e.g. [ICODE]$query = "SELECT * FROM my_table WHERE a_column='".$search_string."'";[/ICODE]

So when your code stops giving errors such as 'missing ; statement' or 'unexpected T_STRING', your syntax is incorrect :)



In my opinion, the three javascript function initForm and showHideBox are unnessecary. When the page reloads, the option that is chosen is already selected (so initForm is obsolete) and using a simple if condition, you can render showHideBox obsolete too!

I'il explain with some code:

// As you have not given all your code I assume a few things:
// > $diag is the ID of the selected diagnostic ($_GET['selectedDiag'])
// > The query $quer2 retrieves all the diagnostic dropdown values
// > The query $quer retrieves all causes WHERE diagnosticId = $diag (and same with $mediquery)

/ Opening form: /
echo "<form method='post' name='f1' action=''>";

/ Opening diagnostic dropdown with default option: /
echo "
<select id='selectedDiag' name='selectedDiag' onchange='reload(this.form)'>
<option value=''>Select diagnosis</option>

while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['diagnosisId'] == $diag) {
echo "<option selected='selected' value='".$noticia2['diagnosisId']."'>".$noticia2['diagnosisName']."</option>";
} else {
echo "<option value='".$noticia2['diagnosisId']."'>".$noticia2['diagnosisName']."</option>";

/ Closing diagnostic dropdown with 'other' option: /
echo "<option value='other'>Other</option>

/ Only if the diagnostic is set: /
if (isset($diag)) {


  • I'il let you correct this part yourself:
    echo "<select name='cause'>
    <option value=''>Select cause</option>";
    while($noticia = mysql_fetch_array($quer)) {
    echo "<option value='$noticia[causeId]'>$noticia[cause]</option>";
    echo "</select>";

    ////////// Starting of third drop downlist /////////
    echo "<select name='medicine'>
    <option value=''>Select medicine</option>";
    while($noticiamed = mysql_fetch_array($mediquery)) {
    echo "<option value='$noticiamed[medicineId]'>$noticiamed[medicineName]</option>";
    echo "</select>";



/ Showing submit button: /
echo "<input type='submit' name='submit_button' value='Submit'>";

/ Closing form: /
echo "</form>";[/CODE]

I think this will help you make the four dropdown work.




I've read that you are just starting to learn PHP! Good :)

It makes it easier to read your code if you have a few coding guidelines:

Indent and comment your code, keeping statements and function calles seperated and clarified:

/ Doing something: /

/ Doing something else: /

/ Indicates whether something needs to be shown: /
$shown = true;

/ Only if it needs to be shown: /
if ($shown == true) {

 /* Showing something: */
 echo "Something";


/ Doing a loop: /
while ($a == 0) {

 //.... Code block ....


// I hope you get the picture by now,
// because typing this isn't really helping you at all with your problem...

Also, you might want to read up on how to properly write HTML (watch the use of quotes and ';'):

[CODE]<img src='myimage.png' border='0' onclick='doSomething("StringArgument");' />[/CODE]

And as a final check, you can always go to the guys that design HTML: [url]http://validator.w3.org/[/url]

Oh and one last thing, don't use old syntax for your HTML:

[CODE]<script type='text/javascript'>
function reload(form)
var val = form.selectedDiag.options[form.selectedDiag.options.selectedIndex].value;
self.location='updateMedicalRecords.php?selectedDiag=' + val ;

I think you will find your answer when you've cleaned up your code.



Alright... I haven't taken a look at the thread for a few hours now and see EvolutionFallen helping :) Well OK.

Some small remarks:

You should still read through my previous reply and clean the values you collect with POST, as this makes your SQL vunerable for attacks!

If you check the following bit of code EvolutionFallen provided:

[CODE] $sql = "INSERT INTO comments (page_id, comment) VALUES ($myid, '$comment')" or die(mysql_error());
mysql_query( $sql, $mymysql );[/CODE]

You should notice that you declare an string to an variable and if THAT doesn't work, you let mysql print its last error??? Pretty strange coding to me! Take a look at this:

[CODE]$query = "THIS is MY query";
$result = mysql_query($query) or die("Could not execute query");[/CODE]

It prevents the visitor from seeing errors if the occur AND they tell you that the query is wrong. Ofcourse, if you are testing and want to know why a query fails, you put [ICODE]mysql_error()[/ICODE] as parameter for [ICODE]die()[/ICODE]. But remember to remove it when putting it on a live site ;).

Also, regarding your form and show page:

[CODE]while ($row = mysql_fetch_array($sql)) {
$comment = $row['comment'];

Is a correct loop, but you are not echo'ing anything! Try this:

[CODE]while ($row = mysql_fetch_array($sql)) {
$comment = $row['comment'];
echo $comment;

Also the returning reply "Still not working" doesn't really help clarify what's going wrong (although more experienced programmers can spot it easier).

You might want to take a look in a good PHP book (I ...