SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    Holland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm, this will probably sound very newbie.

    When I include a php document that defines a variable, can I use that variable in another included document? I tryed but the result was rather disapointing, perhaps I'm doing something wrong here. (I'm actually going through the php tutorial of kevin yank.. wich is very usefull!!)

    Oh, while we're at it. I can't really figure out the mysql_fetch_array funtion out. There is this code that i should use:

    while ( $row = mysql_fetch_array($result) ) { echo ("<p>" . $row["there u go"] . "</p>"); (it works, but i don't really get it)

    Now my question.. if i get this right, the while function will process the echo ("") (placing my dbase in paragraphs) until..... what? until $row is the same as the mysql_fetch_array result?

    Can i control this?

    can someone enlighten me? thanx in advance..

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hoi Joob,
    welcome to SP
    question to your answer is in http://www.php.net/manual/en/functio...etch-array.php
    but I will quota from there to here
    array mysql_fetch_array (int result [, int result_type])

    Returns an array that corresponds to the fetched row, or false if there are no more rows.
    So in while ()
    $row gets the value of result from mysql_fetch_array($result) function and if it is false while loop breaks otherwise it keeps going on.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The include() function/construct just substitutes the file for the include() statement. So, if you included a file that had this in it:

    $var=10;

    and then later you included a file with this in it:

    echo $var;

    The output would be: 10

    Now for your second question: you need to learn the difference between the assignment operator(=) and the equality operator(==). The assignment operator puts a value in a variable. The equality operator checks to see if two things are equal without changing either of them. Here is an example:
    Code:
    $x=1;
    while ($x = 10)
    {
        if ($x == 1)
        { 
           echo "Goodbye";
        }
    
        $x=1;
        
    }
    The loop not only gets executed, it is infinite and Goodbye never gets displayed. Here is what is happening: $x starts off being assigned the value 1. Then in the while condition two things happen: $x is assigned the value 10, and then $x is evaluated as true or false(0=false and anything else is true). So, the while loop is entered. Since $x was assigned the value 10, the if condition fails and Goodbye doesn't get displayed. Then $x is assigned the value 1, and the loop starts over. The while condition is tested again: $x is first assigned 10, and then $x is evaluated as true because it is non-zero, and the whole process starts again.

    The same thing is happening in this statement:

    while ( $row = mysql_fetch_array($result) )

    First, it assigns mysql_fetch_array($result) to $row, and then it evaluates $row as either true or false. $result comes from this statement:

    $result=mysql_query("SELECT * FROM mytable);

    Being new, you might think you can just say:

    echo $result:

    to see the results of your query, but $result is called a "result identifier" and cannot be echoed out. You have to use mysql_fetch_array() on it to get the data, which returns one row of data(or one record) in an array with each field stored in a separate array element.

    Ok, so $row is an array with one row of data in it. As long as mysql_fetch_array() keeps finding rows of data in $result, then $row will evaluate to true after it is assigned the array. As soon as there is no more data, mysql_fetch_array() will be 0 which then gets assigned to $row, and then $row will evaluate to false and the loop ends.

    One last thing: once you have your data extracted into $row how do you echo the data? Once again you can't just say:

    echo $row;

    because $row is an array. There are two methods for echoing the elements of the array $row. If you had 3 fields/columns of data returned by your query, they would be stored in $row[0], $row[1], and $row[2], so to echo out the second field you would do this:

    echo $row[2];

    However, it is not very easy to figure out what field is represented by $row[2] for someone who is reading your code. An alternate way of echoing out the field in $row[2] is to use the field name as the array key like this:

    $row['first_name'];

    You should try to use the field names as the array keys whenever possible. Summing up, mysql_fetch_array() returns both a numeric array($row[0], $row[1], $row[2]) and an associative array($row['id'], $row['first_name'], $row['last_name']).
    Last edited by 7stud; May 16, 2001 at 03:54.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) When execution reaches an include function, PHP includes the contents of the include file as if they were actually in the code block being executed. So yes, you should be able to use any variables that have been created elsewhere in the code (provided they are in scope!). The important thing to realise, however, is that when a file is included, php drops out of parsing the contents of the included file. So if you want the content of the included file to be parsed as php code you have to use the <?php ?> tags.

    Example:
    PHP Code:
    foo.inc
    -----
    <?php
       $foo 
    'foo';
    ?>

    bar.inc
    -----
    <?php
       $bar 
    'bar';
    ?>

    foobar.php
    --------
    <?php
       
    include("foo.inc");
       include(
    "bar.inc");
       echo 
    $foo $bar;
    ?>
    2) ( $row = mysql_fetch_array($result) )

    This condition must evaluate to true for the while loop to be entered (otherwise the while loop will be exited). Breaking the condition down:

    mysql_fetch_array($result) is a function call that will return an array containing the next row in the result set or if there are no more rows it will return false.

    the return value of thecall to mysql_fetch_array($result) is assigned to the variable $row

    $row is evaluated to determine whether the while condition evaluates to true or false. If $row holds any non-zero value it evaluates to TRUE. Thus if it is holding an array of values it evaluates to TRUE. If the call to mysql_fetch_array($result) returned false, then naturally $row holds the value FALSE and thus the while condition will evaluate to false.

    Hope that helps

  5. #5
    SitePoint Member
    Join Date
    May 2001
    Location
    Holland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why thanks!!, the mysql_fetch_array makes sense to me now.. Although the concept of an array isn't clear..

    I'll be on my way finding out why the variables don't come over of my includes.. (I've put the <?php ?> tag around them).

    You're help was really appreciated..

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just to clarify a very important point that freakysid made: when php sees the include() function it puts a closing php tag in your code, then php puts the contents of the include file in your script, and follows it with an opening php tag. So, if your include file looked like this:

    $var=10;

    The result would be this:
    Code:
    <?php
    
    (beginning of your script)
    
    ?>
    
    $var=10;
    
    <?php
    
    (end of your script)
    
    ?>
    As you can see, the code in your include file is not between php tags so it will be treated as html, which is not what you intended. The result of all that is if you have php in an include file, then enclose it in php tags. One tip, don't use a .inc extension for your include files. Some systems cannot handle that extension, so it is better practice to use a .php extension or a .html extension(if your include file only has html in it).

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Arrays allow you to store and output data more easily. Let's say you wanted to store the numbers 0-100 in some variables. Can you imagine thinking up 100 variable names and then typing 100 lines of code to assign the number to the variables? What a headache! Well you can do that more easily with an array. You could do this:

    $numbers[0]=0;
    $numbers[0]=1;
    ...
    ...
    $numbers[100]=100;

    Ok, at least I didn't have to think up 100 variable names, but I still had to write 100 lines of code. Isn't there an easier way? Yes:
    PHP Code:
    <?php
    for ($i=0$i < = 100$i++)
    {
        
    $array[$i]=$i;
    }
    ?>
    Here is another example:
    PHP Code:
    <?php
    $name
    [0]='sally';
    $name[1]='bob';
    $name[2]='freakysid';
    for (
    $i=0$i<$i++)
    {
        echo 
    $name[$i];
    }
    ?>
    I have one suggestion for you. The tutorial you are studying is too advanced for newcomers to programming. You have to learn about variables, strings, arrays, and loops, which are common to all programming languages before you can understand a tutorial like that. Check out some of these resources for beginners:

    http://www.sitepointforums.com/showt...threadid=18699

    Oooops...scratch that. I looked through Kevin's tutorial and he explains the basics in the php section.
    Last edited by 7stud; May 16, 2001 at 04:39.

  8. #8
    SitePoint Member
    Join Date
    May 2001
    Location
    Holland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, Kevins tutor isn't that hard, and very basic.. if you have a little experience with loops and arrays and that sort'a'stuff.. A friend of mine has this kind of experience with C+ so i can ask him, but hi's responses are so...... "foggy?", as if he doesn't really get it also..

    ("Like that's just how it is")

    I think i'm going to read through that thread u discribed, and thanks for explaining me how a array works.

    This forum is a very supportive one, hope it will stay this way.. and maybe later it will be I who's giving advice
    Last edited by Joop; May 16, 2001 at 07:17.


Bookmarks

Posting Permissions

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