SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: while & [i]

  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while & [i]

    PHP Code:
    $sql_exp mysql_query ("select * from experience
    where ID ='
    $ID

    $i = 1;
    while ( 
    $row = mysql_fetch_array($sql_exp) )

    $expid[$i] = $expid ;
    $a = $i;

    echo (
    $expid[$i]) ;
    $i++;

    When I should get for example 658 for expid, I get 6.
    Can you help ?
    Ginie G.

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )

    $expid[$i] = $expid ;
    $a $i;

    echo (
    $expid[$i]) ;
    $i++;

    There should be an ending on teh query...

    hmmmmm


    I don't see much else wrong, that might fix it!

    YOu never use $row['whatever'], so why are you doing the while statement?

    I'm confused...

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly are you trying to do.

    Your code does not make much sence.

  4. #4
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your answer.

    Actually, what i try to do is to update a table with values like exip[i] after a form submission.

    How can I do that?
    Ginie G.

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok....


    If you want to show all the entries of this you have this:

    PHP Code:
    $sql_exp mysql_query ("select * from experience
    where ID ='
    $ID'");

    $i 1;
    while ( 
    $ROW mysql_fetch_array($sql_exp) )


    echo 
    $ROW[['expid']) ;

    If you want to show ONE of them, you woul djust add to teh query

    LIMIT 0,1


    Please explain your code more, becuase like Kaizen said, it doens't make seanse!


    That's why I was confused...

  6. #6
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmm. Me and my explanations...Sorry guys!
    Basically, I want to update my table experience (primary key :expid) with variables generated in a loop.

    Here is the code:

    PHP Code:

    <?php

    if (!isset($submitupdate)):
    ?>

    <html>
    <body>
    <form method="POST" ACTION="<?=$PHP_SELF?>" >

    <?php

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'  ");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )


    $expid $row["expid"];

    echo(
    '<input type="text" name="chron" value="'.$row["chron"].'">     <input type="text" name="dateexp"  value="'.$row["dateexp"].');

    $i++;
    }
     
     }
    ?>

    <input type="submit" value="Update " name="submitupdate">

    </body>
    </html>

    <?php
    else:

    // Update the table - This is where I'
    m stuck !!!!
    $var "update experience 
    SET chron = ???? , dateexp = ??
    Ginie G.

  7. #7
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oooops... I put an extra { in the previous code.
    Here is the corrected one:

    PHP Code:
    <?php

    if (!isset($submitupdate)):
    ?>

    <html>
    <body>
    <form method="POST" ACTION="<?=$PHP_SELF?>" >

    <?php

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'  ");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )


    $expid $row["expid"];

    echo(
    '<input type="text" name="chron" value="'.$row["chron"].'">     <input type="text" name="dateexp"  value="'.$row["dateexp"].');

    $i++;
    }
     

    ?>

    <input type="submit" value="Update " name="submitupdate">

    </body>
    </html>

    <?php
    else:

    // Update the table - This is where I'
    m stuck !!!!
    $var "update experience 
    SET chron = ???? , dateexp = ??
    Ginie G.

  8. #8
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <?php

    if (!isset($submitupdate)):
    ?>

    <html>
    <body>
    <form method="POST" ACTION="<?=$PHP_SELF?>" >

    <?php

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'  ");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )


    $expid $row["expid"];

    echo(
    '<input type="text" name="chron" value="'.$row["chron"].'">     <input type="text" name="dateexp"  value="'.$row["dateexp"].');

    $i++;
    }
     

    ?>

    <input type="submit" value="Update " name="submitupdate">

    </body>
    </html>

    <?php
    else:

    // Update the table - This is where I'
    m stuck !!!!
    $var mysql_query("UPDATE experience SET field='$variable', field='$variable'");

    Update table is this:

    UPDATE table SET field='$variable'

    and a comma in between every field='$varialbe' you have, except teh last one.


    GOod luck!

  9. #9
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One more thing..

    Can you post the WHOLE code adn what you are trying to do with it?

    Thanks,
    ~someonewhois

  10. #10
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might I also suggest:

    PHP Code:
    <?php

    if (!isset($submitupdate)):
    ?>

    <html>
    <body>
    <form method="POST" ACTION="<?=$PHP_SELF?>" >

    <?php

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'  ");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )


    $expid $row["expid"];

    ?>
    <input type="text" name="chron" value="<?=$row["chron"]?>">     <input type="text" name="dateexp"  value="<?=$row["dateexp"]?>">
    <?php
    $i
    ++;
    }
     

    ?>

    <input type="submit" value="Update " name="submitupdate">

    </body>
    </html>

    <?php
    else:

    // Update the table - This is where I'm stuck !!!!
    $var mysql_query("UPDATE experience SET field='$variable', field='$variable'");
    This is just a suggestion, you don't have to use it.

    I bolded teh part that I changed, just teh form area!

    You forgot to close the second field aswell...


    Going into HTML works jsut as well, and is easier to add things!


    Edit: The bold didn't work, it shows as code in teh php tags!

  11. #11
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OHOHOHOHOH!

    So.. update expieriece by 1 whenever they visit the site?

  12. #12
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <?php

    if (!isset($submitupdate)):
    ?>

    <html>
    <body>
    <form method="POST" ACTION="<?=$PHP_SELF?>" >

    <?php

    $sql_exp 
    mysql_query ("select * from experience
    where ID ='
    $ID'  ");

    $i 1;
    while ( 
    $row mysql_fetch_array($sql_exp) )


    $expid $row["expid"];

    ?>
    <input type="hidden" name="chron" value="<?=$row["chron"]?>">     <input type="hidden" name="dateexp"  value="<?=$row["dateexp"]?>">
    <?php
    $i
    ++;
    }
     

    ?>

    <input type="submit" value="Update " name="submitupdate">

    </body>
    </html>

    <?php
    else:

    $var mysql_query("UPDATE experience SET field='$variable', field='$variable'");
    You might wnat to make the fields HIDDEN, that way when people go to the site they don't see those fields!

    This way, they dont' even know it's tracking them!

  13. #13
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <?php

     $date 
    date("Y-m-d H:i:s");



    // create connection 
    $connection mysql_connect("$server","$user","$password"
    or die(
    "Couldn't make connection."); 

    // select database 
    $db mysql_select_db("$database"$connection
    or die(
    "Couldn't select database."); 


    $sql_exp2 mysql_query("select * from experience");

    $i 1;
    while (
    $old=mysql_fetch_array($sql_exp2))


    $chron $old['chron']+$i;
    }
    // NOTE: I don't really know how to do that adding thing, maybe somebody else does!




    // create connection 
    $connection mysql_connect("$server","$user","$password"
    or die(
    "Couldn't make connection."); 

    // select database 
    $db mysql_select_db("$database"$connection
    or die(
    "Couldn't select database."); 


    $var mysql_query("UPDATE experience SET chron='$chron', dateexp='$date', userid='$username'");
    ?>


    <html>
    <head><title>Your Site</title></head>
    <body bgcolor="ffffff">
    PUT YOUR ENTIRE PAGE HERE, ADD THE ABOVE TO THE TOP OF EACH PAGE YOU WANT EXP TO UPDATE!
    <BR><BR>
    --
    <?php
     
    // This displays it!


    // create connection 
    $connection mysql_connect("$server","$user","$password"
    or die(
    "Couldn't make connection."); 

    // select database 
    $db mysql_select_db("$database"$connection
    or die(
    "Couldn't select database."); 


    $sql_exp mysql_query("select * from experience");

    $i 1;
    while (
    $output=mysql_fetch_array($sql_exp))

    ?>
    <BR>
    Username: <?=$output['userid']?><BR>
    Exp: <?=$output['chron']?><BR>
    Last Updated: <?=$output['date']?><BR>
    <HR><BR>
    <?php
    }
    ?>
    </body>
    </html>

    Ok, I"m guess your table is like this:

    id int(11) auto_increment
    chone varchar(100)
    dateexp datetime


    I suggest oyu aadd

    userid varchar(100) default ''

    The above code needs that!

    I'm assuming that you use some sort of signup, mainly Kevin's tutorial, so I'm using the variable $username from accesscontrol.php!

    Use what you want for the usrname...


    if you want only one persons to show, you have to define a username by WHERE userid='$username' or something liek that, and if you want all to show leave it ask it is!

    Something doesn't look right there...


    P.S. Tell me if I screwed up completly on that!
    It's hard to see here... I'm editing it in here!

    Also, define $user, $password, $server, and $database!


    Edit: I found my own errors, duh!



    Good luck, tell me if it works, I might use it!
    Last edited by someonewhois; Mar 28, 2002 at 20:55.

  14. #14
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that just updates the one thing, but if you want multiple users (which I do, and expect you do too) you have to do this:

    PHP Code:

    //fill in some basic info
    $timeoutseconds = 30; 


    //get the time
    $timestamp = time(); 
    $timeout = $timestamp-$timeoutseconds; 


    $date = date("Y-m-d H:i:s");

    // create connection 
    $connection = mysql_connect("$server","$user","$password") 
    or die("Couldn't make connection."); 

    // select database 
    $db = mysql_select_db("$database", $connection) 
    or die("Couldn't select database."); 


    $sql_exp2 = mysql_query("select * from experience");

    $i = 1;
    while ($old=mysql_fetch_array($sql_exp2))


    $chron = $old['chron']+$i;
    }
    // NOTE: I don't really know how to do that adding thing, maybe somebody else does!




    // create connection 
    $connection = mysql_connect("$server","$user","$password") 
    or die("Couldn't make connection."); 

    // select database 
    $db = mysql_select_db("$database", $connection) 
    or die("Couldn't select database."); 

    $alreadyin = mysql_query("SELECT * FROM experience");

    if ($username == $alreadyin){

    $var = mysql_query("UPDATE experience SET chron='$chron', dateexp='$date' WHERE userid='$username");
    }

    if ($username != $alreadyin){
    $insert = mysql_query("INSERT into experience SET chrom='$chrom', dateexp='$date', userid='$username'");
    }

    // Thhat means, it takes all users from experience, and if it matches who it is, it updates, if it doesn't match it means it's a new user, and it will insert!
    ?>


    <html>
    <head><title>Your Site</title></head>
    <body bgcolor="ffffff">
    PUT YOUR ENTIRE PAGE HERE, ADD THE ABOVE TO THE TOP OF EACH PAGE YOU WANT EXP TO UPDATE!
    <BR><BR>
    --
    <?php
     
    // This displays it!


    // create connection 
    $connection mysql_connect("$server","$user","$password"
    or die(
    "Couldn't make connection."); 

    // select database 
    $db mysql_select_db("$database"$connection
    or die(
    "Couldn't select database."); 


    $sql_exp mysql_query("select * from experience");

    $i 1;
    while (
    $output=mysql_fetch_array($sql_exp))

    ?>
    <BR>
    Username: <?=$output['userid']?><BR>
    Exp: <?=$output['chron']?><BR>
    Last Updated: <?=$output['date']?><BR>
    <HR><BR>
    <?php
    }
    ?>
    </body>
    </html>

    Edit: another crewed...


    That if $username == $alrady in and != shoudl work, that means if username matches what's in teh SELECT query, it updates, and if it != (not equal) it inserts!

    Simple as can be, but still hard!

    I have to go for the night, if it doesn't work post back and I'll try and help more!

    Post any errors, and teh lines they are on, please!


    Good luck, good bye!

    Thanks,
    ~someonewhois


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
  •