Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Understanding PHP

    I have some problems understanding things in PHP and would really appreciate some help.

    I'm not a programmer but I have a basic knowledge of programming. I can do basic Javascript/ActionScript and ASP and understand IF statements and variables but database connection, loops and arrays are beyond my comprehension.

    I made a website in flash for an animation I did (www.robinvspirate.com). The flash form sends data to a PHP script which has this in it:

    <?

    $sName=$_GET["sName"];
    $sContact=$_GET["sContact"];
    $sComments=$_GET["sComments"];

    $to .= "me@myemailaddress.com";
    $subject .= "ROBINvsPIRATE on-line contact form";

    //Main body

    $msg .= "Name: $sName\n\n";
    $msg .= "E-mail address: $sContact\n\n";
    $msg .= "Comments/message: $sComments\n\n";

    mail($to, $subject, $msg, "From: ROBINvsPIRATE.com[/email]\nReply-To: $sContact\n");
    ?>

    I tested it before making the site live and it worked. I had a form submitted on the 2nd which had undefined in all fields. I tested it today and it worked.

    Why would it work for me and not for someone else? How could it stop and start working without me touching it? I don't understand this.

    Secondly, I wanted to make a basic image viewer (www.albinal.com/squad.php) where the image and the next/previous links changed based on the selection.

    I used this code:
    <?
    $imageID=$_GET["imageID"];

    if ($imageID == "")
    {
    $imageID = 1;
    }

    echo '<p align="center"><img src="images/vector_art/small_va'. $imageID .'.gif" style="border:1px solid #000000;" alt=""></p>';


    $TotalImages = 8;
    $next = $imageID = $imageID + 1;
    $previous = $imageID = $imageID - 1;


    ?>

    The next link works fine but the previous link doesn't. Unless I swap these lines around:

    $next = $imageID = $imageID + 1;
    $previous = $imageID = $imageID - 1;

    Then the previous link will work but the next doesn't. This seems to suggest that the $imageID variable is changing based upon what the other variables do, which confuses me.

    Sorry for the long post but if someone can help with this then I think my understanding will be much better.

    Many thanks.

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    I tested it before making the site live and it worked. I had a form submitted on the 2nd which had undefined in all fields. I tested it today and it worked.

    Why would it work for me and not for someone else? How could it stop and start working without me touching it? I don't understand this.
    Is it just possible that the other person might well have submitted the form without any information entered into it?

    PHP Code:
    $next $imageID $imageID 1;
    $previous $imageID $imageID 1
    I've never seen this before, where you've got two equals symbols. That looks wrong (although it could be something obscure).

    But, to me that looks like with the first line you're adding 1 to $imageID itself, not just declaring that $next equals $imageID +1.

    Try:


    PHP Code:
    $next $imageID 1;
    $previous $imageID 1

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you submit a blank form then the fields come through blank. The only time I've ever expereinced "undefined" fields was when something wasn't working and the values weren't getting passed.

    Could something server side have gone wrong?

    The second bit of code you posted seems to work better. I was trying to increment $ImageID which was a mistake. Many thanks!

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I've never seen this before, where you've got two equals symbols. That looks wrong (although it could be something obscure).
    It's actually a little programming 'trick'.

    What PHP does when evaluating a statement is something along the lines of this:

    1. It starts with the original.
    PHP Code:
    $variable $variable2 5
    2. Evaluates the first expression it knows of (depending on certain rules).
    PHP Code:
    $variable = ($variable2 5); 
    PHP replaces the expression with the end result (now contents of $variable2):

    PHP Code:
    $variable 5
    3. Repeat 2

    PHP Code:
    ($variable 5); 
    PHP Code:
    5
    Side Note: This is why you can do:

    PHP Code:
    mysql_query("etc.") or die("error"); 
    Because PHP replaces the function with its end result (no matter whether you assign it to something or not. So if your query returns an invalid result (null, false or whichever), you get this:

    PHP Code:
    false or die("error"); 
    False obviously doesn't go anywhere, so it is forced to do the or part of it. It's basically a shortcut to:

    PHP Code:
    if (mysql_query("etc.") === FALSE) {
        die(
    "error");

    PHP Code:
    $previous $imageID $imageID 
    Can also be written as:

    PHP Code:
    $previous = --$imageID 
    Last edited by aedrin; 06-04-2007 at 04:44 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •