#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    I apologize if this makes absolutely NO sense.
    Ok I got a little problem, which I have a solution for but I would like to know if the is a better way.


    I am making query to a mysql DB like so:

    $query="Select * from DEMOTABLE";
    $thisquery=mysql_query($query);
    $demoinfo=mysql_fetch_array($thisquery);

    Now I would like to make a table in HTML like
    so:

    <TABLE>
    <?
    while($demoinfo=mysql_fetch_array($thisquery);{
    ?>
    <TR>
    <?php
    while($key=key($demoinfo)){
    ?>
    <TD>
    <?echo $demoinfo($key);?>
    </TD>
    <?
    next($demoinfo);
    }?>
    </TR>
    <?}?>

    This does not work.

    i get output as such.

    column1 column1 column2 column2 column3 column3

    becuase the keys for the $demoinfo array
    are 'KEY0' 0 'KEY1' 1 'KEY2' 2

    The keys are both the numeric and the alphenumeric. Doubling my output.

    Now my stupid work around is to
    add 2 next($demoinfo) commands. So I skip evry other column.

    This works but there has to be another way.

    Any help would be greate.

    Thanks.


    ps
    the above code does not acutally prodce duoble output. becase it will stop here
    while($key=key($demoinfo)){

    on the second pass becuase the second pass
    the key=0, the numeric key, which is false.

    meaning I actually only get 1 column printed.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    2
    Rep Power
    0
    I think your problem lies in this row

    while($key=key($demoinfo))

    I think you should use

    while (list($n, $val) = each($demoinfo))

    Then the variable $val will contain the value you want to print and $n the number of the column.

    Anyway try to look the php3 docs and search for the "each()" function.

    =KL=
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    OK I tried the suggestion. and the problem is not how I access the array.

    the problem is this.

    This Command

    $demoinfo=mysql_fetch_array($thisquery);

    The index keys for the $demoinfo are doubled.

    Example.

    The actual table in mysql is this

    DEMO TABLE

    | col1 | col2 | col3 | col4 |
    +------+------+------+------|
    | 1 | 10 | 109 | 45 |
    |------+------+------+------+


    so making the call:

    $demoinfo=mysql_fetch_array($thisquery);

    will return an array as such:

    $demoinfo[0]==1
    $demoinfo[1]==10
    $demoinfo[2]==109
    $demoinfo[3]==45

    or you could access the same data with these keys

    $demoinfo['col1']==1
    $demoinfo['col2']==10
    $demoinfo['col3']==109
    $demoinfo['col4']==45

    Using the numeric index or the aplha(words) index is the same.

    The problem is that the actuall keys for the array have both.

    using
    while (list($n, $val) = each($userinfo)){
    echo "$n $val <br>";
    }

    you get the output of such

    col1 1
    0 1
    col2 10
    1 10
    col3 109
    2 109
    col4 45
    3 45

    the actual key data for the array is doubled
    so if you make on call for the key and its value and increment the index pointer
    and then make another call you get the SAME
    DATA you are just accessing it with the
    'col1' NAME the first time and then the '0' the numeric index the second.

    Basicly if you use

    mysql_fetch_array it creates an array in which the index has both the aplenumeric 'col1' and the numeric '0'


    ANyway I think I'll look through the mysql section in PHP but I have several times but maybe I'm just over looking something.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    if the little table above is confusing I apologize here is a better version.
    <br>

    <pre>
    DEMO TABLE

    | col1 | col2 | col3 | col4 | +------+------+------+------|
    | 1 | 10 | 109 | 45 | |------+------+------+------+

    </pre>

    I hope that turned out better.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    DOh!!!

    OK forget the TABLE I think you can probably figure it out between the two poorly rendered ones.

    Anyway thanks for any amount of patience you might be spending with me and my tables.

  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    OK I got it.

    I once again RTFM and found that
    mysql_fetch_array does create an index including the associative alphanumeric names
    and the numeric indices for the columns.

    However mysql_fetch_row will only return the numeric indices which solves my problem since I do not have to use the associative names anyway.

    Thanks for the help.

    And sorry for anyone who had to read through all that.

    Thanks.

Similar Threads

  1. giving the array matrix a variable?
    By andy3109 in forum C Programming
    Replies: 33
    Last Post: March 9th, 2005, 05:43 AM
  2. array containing arrays pointer incrementing?
    By balance in forum C Programming
    Replies: 17
    Last Post: May 27th, 2003, 05:43 AM
  3. Recursively implode arrays to a string
    By pther in forum PHP Development
    Replies: 2
    Last Post: February 11th, 2003, 08:18 AM
  4. Converting arrays from flash, into text files?
    By frankbutcher28 in forum PHP Development
    Replies: 1
    Last Post: March 25th, 2002, 09:15 AM
  5. comparing arrays
    By NoXcuz in forum PHP Development
    Replies: 11
    Last Post: November 25th, 2001, 12:26 PM

IMN logo majestic logo threadwatch logo seochat tools logo