SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the code below okay? It's line 3 I'm curious about...

    Code:
    $x = 0;
    while ($row = mysql_fetch_row($result)) {
    echo "$row[$x++]";   
    }
    If not - how do I fix it?

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually that's not a variable variable - they look similar though:

    Variable Variable: $$var;
    Incremented Variable: $var++;

    The second, which is what you're using, increments the variable's value by one, assuming it is an integer.

    Anyway, that won't work unless all your field names are numbers, I think - the first time through the variable echo'd out will be "$row[0]" - I'm pretty sure you need to declare variables in the loop like this:

    PHP Code:
    while ($row mysql_fetch_row($result)) {
      
    $id $row["id"];
      
    $title $row["title"];
    echo(
    $title);   

    I believe Freddy (our resident PHP guy) presented this alternative method:

    PHP Code:
    while ($row mysql_fetch_row($result)) {
    extract($result);
    echo(
    $title);   

    I'm pretty sure that's how it works - I believe it takes all the entires in the $row array and assigns them names based on their name = title is $title, id is $id, etc.

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's it going truelight. That script you are writing is a true ball buster!

    Anyway, your code is technically correct, but I don't think its doing what you want it to do (not that I know what is specifically).

    Every time the program hits the while condition it tries to read a row from the result set and assign it to the array $row. Sweet.

    So the first time it iterates it extracts row 1 from the result set. $x=0 so your echo statement will print out the first element of the first row of the result set.

    OK, now during that echo statement $x is post incrementd ($x++) so now the next time through, the while loop extracts the second row from the result set and the echo statement prints out the second element in the second row.

    The third time through the echo statement will print out the third element in the third row. ...etc, etc.

    So you will end up with

    row1element1
    row2element2
    row3element3
    etc

    Is that what you want?

  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)
    TWTCommish your thinking of the array as an associative array.

    Firstly, truelight is dealing with a plain vanilla flavoured array because he is using mysql_fetch_row() not mysql_fetch_arryay().

    Anyway, that doesn't matter because elements of an array can always be addressed by their index number.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point alos Chris you would need extract $row not $result, as $row is the new associative array. I think what he/she is after is this.
    PHP Code:
    $count mysql_num_fields($result);
    while (
    $row mysql_fetch_row($result)) {
    for(
    $i=0;$i<$count;$i++) {
    echo 
    "$row[$i]";   
    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or another way of writing what freddydoesphp wrote is
    PHP Code:
    while*$row*=*mysql_fetch_row($result) )*{
       foreach( 
    $row as $element ) {
          echo 
    $element;
       }

    caveat : requires >= php4
    Just another way to skin a cat. Oh NO! I didn't mean that literally
    Last edited by freakysid; Feb 23, 2001 at 12:40.

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ouch, did you hear what he said to you, Freddy?

    My mistake - I'm so used to using mysql_fetch_array that I don't always notice when someone is using a variation of it. Anyway, I think extract() is the best way to go.

    Good luck.

  8. #8
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang! I just messed up again - I posted without thinking. Sorry. You see, On a topic I posted before, I asked for a way to get an array numbered from 0-9, without actually changing the MySQL table. I lot of solutions came up, but there was not until some bugger posted something on geekvillage.com that I found something that would be perfect for this script I'm writing. This is what he suggested, assuming $array holds the results:

    $temparray = array_values($array);

    This was supposed to give me a numerically indexed array, and I should be able to just reference the items by using $temparray[0], $temparray[1], etc. This would be EXACTLY what I need. Of course the damn thing didn't work. Does anyone have a clue on what he meant with the above code?

    That is what I meant to use in the code posted in the above topic - I just wrote the WHILE statement to put it in context, it wasn't really neccesary. I just wanted to now if you could write a variable between the [ and the ]..

  9. #9
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't work? Are you running PHP3, perhaps?

    PHP.net's page on the array_values() function offers this code for those using a version of PHP lower than 4...

    PHP Code:
    function array_values ($arr) {
                                       
    $t = array();
                                       while (list(
    $k$v) = each ($arr)) {
                                           
    $t[] = $v;
                                           return 
    $t;
                                       }
                                   } 
    HOWEVER - a user contributed note has what he says is a correct version of that example - I think he's right. Here it is:

    PHP Code:
                             function array_values ($arr) { 
                             
    $t = array(); 
                             while (list(
    $k$v) = each ($arr)) { 
                             
    $t[] = $v
                             } 
                             return 
    $t
                             } 
    Hope that helps.

  10. #10
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, I'm pretty sure the server runs PHP3 - filthy host won't upgrade. So this is the problem. I'll try to sort this out using your method, then. Goodie.

    I have had lots of problems with this little script that does very little really. It's almost like some higher PHP force is giving it to me as a test of programming manhood or something.


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
  •