1.11M Members

Posts by Graphix

Re: in JavaScript / DHTML / AJAX
 
0
 

If a color needs to be transparant, you have two options:

  • Use an RGBA definition, e.g. background-color: rgba(255,0,0,0.5); , see http://www.w3schools.com/cssref/css_colors_legal.asp (but this is only supported on the newer versions of browsers)

  • Use the opacity (Netscape-based browsers) and filter (IE-based browsers) property

~G

Code Snippet: in C
 
0
 

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

and:

+ 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 :)

~G

Re: in JavaScript / DHTML / AJAX
 
0
 

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).

Example:

<script type="text/javascript">

var str="Visit Microsoft! Microsoft";
document.write(str.replace("Microsoft","W3Schools"));

</script>

Returns:

Visit W3Schools! Microsoft 

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

~G

Re: in JavaScript / DHTML / AJAX
 
0
 

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:

<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>

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.

~G

Re: in C
 
0
 

Thanks for the quick replies!

I now understand the values of pointers a bit more :)

Re: in C
 
0
 

Sure:

size->4
size->4
size->0022FF4C
size->2293580

~G

Solved in C
 
0
 

Hi,

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:

#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 :)

~G

PS: The output on my computer:

Value (with asterisk): 60
Location (without asterisk): 2293572

Value (with asterisk): c
Location (without asterisk): 2293571

Unknown (with asterisk): 83
Location (with ampersand): 2293564
Value (without asterisk): Some string

Unknown (with asterisk): 892548916
Location (with ampersand): 2293560
Unknown (without asterisk): 4210883
Re: in C
 
0
 

Thanks for the thorough replies, it really put me in the right direction for this program :)

Re: in C
 
0
 

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)?

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

~G

Solved in C
 
0
 

Hi everyone,

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

Main problem: EOF in the middle of the file

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

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: */
            fclose(input);
            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?

Secondary issue: speed

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 can copy even faster?

I'm also not sure what the bottleneck is in regards to speed, as an portable drive takes more time to write to than for example internal memory. If I write a function that compresses files before they are copied, would that increase speed? Or would it just slow the read/write process down?

Thanks in advance,
~G

Re: in PHP
 
0
 

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

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

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)

~G

Re: in JavaScript / DHTML / AJAX
 
1
 

Hi!

A few things you might consider:
>> First debug your code and find the source of your problem, then post the small 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.

~G

Re: in PHP
 
1
 

1. You put each mile in an array and each name
2. You sort the miles-array: http://php.net/manual/en/function.sort.php or an equivalent function (you can always write your own -> good for learning)
3. You run through the array echo'ing each element.

~G

Re: in PHP
 
0
 

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 http://php.net/manual/en/function.strtotime.php for more explaination).

<?php 
/* 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" );
	exit(0); 

} 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.

~G

Re: in PHP
 
0
 

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.

~G

Re: in PHP
 
0
 

Wouldnt that require 5 new fields in members ?

No, only 1 field: permission. E.g.

Username Password Permission
--------- --------- -----------
Dude secret 2
Guy cube 0

And so on.

And then how would i assign a user either 0, 1, 2, 3, 4 or 5?

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.

~G

Re: in PHP
 
0
 

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: if ($permission > 2) { ....... } ~G

Re: in PHP
 
0
 

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 ;)

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

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

~G

Tag Cloud for Graphix