Results 1 to 7 of 7

Thread: Newbie Question - Variables in PHP

  1. #1
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default Newbie Question - Variables in PHP

    Hi guys...

    Simple question for most of u PHP geniuses out there...

    I'm trying to do a very PHP intensive site. I figure it's the only way to really learn.

    So, I've been trying to master some of the basics but can't seem to grasp them.

    Here is the PHP declaration at the top of my page:

    PHP Code:
    <?php
    include 'php/config.php';
    include 
    'php/opendb.php';

    $query  "SELECT * FROM mission where section='1'";
    $result mysql_query($query);

    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        echo 
    "{$row['content']}";
        
    $c="{$row['content']}";
        
    }

    ?>
    the "echo" works fine. it echoes what i want it to.

    Now, i'm trying to put the same thing that echoes into a variable so that i can use it elsewhere on the page. The data from the MySQL has to be integrated into the page, not just @ the top of the page.

    I know there are better techniques then just echoing everything out.

    How can I use the info from the content row and insert it into a textarea further down the page?

    Also, what if I have another textarea to display info from the same MySQL table but "where section='2'". How would I go about coding that?

    Thanks!!!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    With each loop, the variable $row is reset; as is, the variable $c is also reset in the same way. You can access $row or $c after this, but they will only have the value from the last rotation.

    You will probably want to setup an array for this:

    PHP Code:
    $i 0;
    while (
    $row mysql_fetch_assoc($stuff)) {
    $out[$i] = $row['stuff'];
    $i $i+i;
    }

    //and a cleaner way, but more complex:
    for ($i=0;$row=mysql_fetch_assoc($stuff);$i++) {
    $out[$i] = $row['stuff'];

    However, rarely is this really needed.

    Just going through the while loop at the right point should be enough in most cases. You should be able to place the while loop exactly where you need to output the text of each repetition.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Oct 2006
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Use globals.

    Instead of using $c, declare (earlier in the script):
    Code:
     global $c;

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Globals aren't related to this. A global variable is available within any scope (meaning it works in functions, basically), but it's value would still be overridden any time you reset it's value.

    PHP Code:
    global $c;
    $c 1;
    $c 2;
    //value of 1 is now lost.
    //same as:
    $c 1;
    $c 2
    The solution is as I posted-- place the loop in an appropriate location on the page to generate the output, or use arrays.

    One other method would be to create a string in the loop, like:
    PHP Code:
    $string '';
    while () {
    $string .= $row['addthis'];

    That would add the value of $row['addthis'] with every loop, so if your first row returned was 1, the second was 3 and the third was 5, you would end up with a $string=='135'.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    forgive me...but i'm still not understanding how to do this?

    Here is the page as I have it:

    PHP Code:
    <?php
    include 'php/config.php';
    include 
    'php/opendb.php';

    $query  "SELECT * FROM mission where section='1'";
    $result mysql_query($query);

    $i 0;
    while (
    $row mysql_fetch_assoc($result)) {
    $out[$i] = $row['content'];
    $i $i+i;
    }
    ?>

    <html>
    <head><title>TestPage</title></head>
    <body>

    <textarea><? echo $out[i?></textarea>

    </body>
    </html>

    this isn't working so i'm doing something wrong. what is it?

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Medyman View Post
    <textarea><? echo $out[i] ?></textarea>
    should be

    PHP Code:
    <?php 
    foreach($out as $o) {
    ?>
    <textarea><?php echo $o?></textarea>
    <?php
    }
    ?>
    however if all your want is the content, there is no sense in pulling the rest of the data from the table.
    PHP Code:

    $query  
    "SELECT content FROM mission WHERE section='1'";
    $result mysql_query($query);

    $i 0;
    while (
    $row mysql_fetch_assoc($result)) {
    $out[$i] = $row['content'];
    $i += 1;


  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are many ways to do this. Those posted above will work, or perhaps this will make more sense.

    The placement of PHP is easy-- put it where you want it to go. So,
    PHP Code:
    <?php
    include 'php/config.php';
    include 
    'php/opendb.php';

    $query  "SELECT * FROM mission where section='1'";
    $result mysql_query($query);
    ?>

    <html>
    <head><title>TestPage</title></head>
    <body>

    <textarea><?php
    while ($row mysql_fetch_assoc($result)) {
    echo 
    $row['content'];
    }
    ?></textarea>

    </body>
    </html>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •