SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question array problems in a while loop

    Hi everyone,
    I'm having a newbie problem with an array I'm trying to work with. Basically, I'm using mysql_fetch_array but every line is the same. Essentially it's not moving to the next row after every while loop.

    I've tried doing this:

    $result = mysql_query("SELECT * FROM equipment WHERE trans_id = '$trans_id'");
    while ($row = mysql_fetch_array($result)) {
    ...
    ...
    ...
    }

    and I've tried it this way:

    $row = mysql_fetch_array($result);
    $arr_size = count($row);
    for($i = 0; $i < $arr_size; $i++){
    ...
    ...
    ...
    }

    but it keeps repeating the first line. Anyone have any ideas? I've done this several times, nothing has changed in my code, but I've never had this happen.. thanks in advance for any help

    S.

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Definitely weird. Could I have a look at what's inside the while-loop? Also, maybe you really do have replicated records?

  3. #3
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the second method won't work.

    what happened exactly that was wrong with the first method?
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  4. #4
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, let's see if I can coherently explain my madness. Basically what I'm doing is building a pdf using pdflib. you can see the output pdf here http://www.copierbrothers.com/download/test.pdf

    I'm querying a mysql database that has multiple entries of equipment in them. each row is the model and all installed accessories. Querying the mysql database at command line tells me there's definitely no replication.. you can see on the pdf linked above, that every line comes out as those same 3 components, but there is only one entry in the database that has only those three components, the first row..

    Now, I'm pretty new to this, so I probably went about doing this the hard way, but what I'm doing is, after my while loop, I get all my variables down right away.. so the first part of my while loop looks like this:

    while ($row = mysql_fetch_array($result)) {
    $model = $row["model"];
    $adf = $row["adf"];
    $finisher = $row["finisher"];
    $hole_punch = $row["hole_punch"];
    $post_inserter = $row["post_inserter"];
    $duplexer = $row["duplexer"];
    $cassette1 = $row["cassette1"];
    $cassette2 = $row["cassette2"];
    $cassette3 = $row["cassette3"];
    $pedestal = $row["pedestal"];
    $lcf = $row["lcf"];
    $controller1 = $row["controller1"];
    $controller2 = $row["controller2"];
    $controller3 = $row["controller3"];
    $controller4 = $row["controller4"];
    $controller5 = $row["controller5"];
    $memory = $row["memory"];
    $fax1 = $row["fax1"];
    $fax2 = $row["fax2"];
    $other1 = $row["other1"];
    $other2 = $row["other2"];
    $other3 = $row["other3"];
    $other4 = $row["other4"];
    $efi1 = $row["efi1"];
    $efi2 = $row["efi2"];
    $efi3 = $row["efi3"];

    after I have all my variables set, I go into a ridiculous maze of if's to get the placement right.. What I did was I have a place variable, originally set at one, which determins which row on the pdf to place the equipment on.. And then a series of ifs using a variable called $vert that I use to move the equipment down each cell of the column..

    So what it does is plugs in the model at the top:
    if ($place == 1) {
    pdf_show_xy($pdf, "$model", 160, 369);
    $vert++;

    then I have it checking whether or not the $adf variable, for instance, is null or not.. also it runs down finding which number the $vert variable is set to to find out what xy coord. to put the value at if it is set..

    if ($adf != "" and $vert == 15) {
    pdf_show_xy($pdf, "$adf", 160, 210);
    $vert++;
    }
    if ($adf != "" and $vert == 14) {
    pdf_show_xy($pdf, "$adf", 160, 221);
    $vert++;
    }

    etc.etc....

    and it does that over and over again with each possible accessory..
    I hope that makes sense.. It barely makes sense to me.

    Like I said, there's probably an easier way to do that, but based on what I know that was the solution..

  5. #5
    SitePoint Zealot itsyM's Avatar
    Join Date
    Jul 2002
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing this

    PHP Code:
    $result mysql_query("SELECT * FROM equipment WHERE trans_id = '$trans_id'"); 
    to this

    PHP Code:
    $result mysql_query("SELECT * FROM equipment WHERE trans_id = $trans_id"); 
    I assume here that the trans_id is an integer. You should not use aposthropes with numbers.

  6. #6
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No love, Dr. Jones.. I get the same output either way.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you please do a var_dump on $row, this way you'll see what's inside it, maybe that helps.

  8. #8
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright.. I wrote another php file that reads like this..

    <body>

    <?php

    $result = @mysql_query("SELECT * FROM equipment WHERE trans_id = $trans_id");
    if (!$result) {
    echo("<p>Error performing query: " . mysql_error() . "</p>");
    exit();
    }
    while ($row = mysql_fetch_array($result)) {
    $dump = var_dump ($row);
    ?>
    <?=$dump?> <BR><BR><BR>
    <?php
    }
    ?>

    </body>



    I'll spare you the full monty of output and give you the first couple groups.. It seems to be coming out correctly here..

    array(56) { [0]=> string(1) "3" ["ID"]=> string(1) "3" [1]=> string(4) "2664" ["trans_id"]=> string(4) "2664" [2]=> string(10) "estudio16s" ["model"]=> string(10) "estudio16s" [3]=> string(8) "ka1600pc" ["adf"]=> string(8) "ka1600pc" [4]=> string(0) "" ["finisher"]=> string(0) "" [5]=> string(0) "" ["hole_punch"]=> string(0) "" [6]=> string(0) "" ["post_inserter"]=> string(0) "" [7]=> string(0) "" ["duplexer"]=> string(0) "" [8]=> string(0) "" ["cassette1"]=> string(0) "" [9]=> string(0) "" ["cassette2"]=> string(0) "" [10]=> string(0) "" ["cassette3"]=> string(0) "" [11]=> string(8) "stand16t" ["pedestal"]=> string(8) "stand16t" [12]=> string(0) "" ["lcf"]=> string(0) "" [13]=> string(0) "" ["controller1"]=> string(0) "" [14]=> string(0) "" ["controller2"]=> string(0) "" [15]=> string(0) "" ["memory"]=> string(0) "" [16]=> string(0) "" ["fax1"]=> string(0) "" [17]=> string(0) "" ["fax2"]=> string(0) "" [18]=> string(0) "" ["other1"]=> string(0) "" [19]=> string(0) "" ["other2"]=> string(0) "" [20]=> string(0) "" ["other3"]=> string(0) "" [21]=> string(0) "" ["other4"]=> string(0) "" [22]=> string(0) "" ["controller3"]=> string(0) "" [23]=> string(0) "" ["controller4"]=> string(0) "" [24]=> string(0) "" ["controller5"]=> string(0) "" [25]=> string(0) "" ["efi1"]=> string(0) "" [26]=> string(0) "" ["efi2"]=> string(0) "" [27]=> string(0) "" ["efi3"]=> string(0) "" }


    array(56) { [0]=> string(1) "7" ["ID"]=> string(1) "7" [1]=> string(4) "2664" ["trans_id"]=> string(4) "2664" [2]=> string(9) "estudio28" ["model"]=> string(9) "estudio28" [3]=> string(6) "mr3012" ["adf"]=> string(6) "mr3012" [4]=> string(6) "mj1012" ["finisher"]=> string(6) "mj1012" [5]=> string(6) "mj6001" ["hole_punch"]=> string(6) "mj6001" [6]=> string(0) "" ["post_inserter"]=> string(0) "" [7]=> string(0) "" ["duplexer"]=> string(0) "" [8]=> string(6) "my1017" ["cassette1"]=> string(6) "my1017" [9]=> string(0) "" ["cassette2"]=> string(0) "" [10]=> string(0) "" ["cassette3"]=> string(0) "" [11]=> string(6) "kd1009" ["pedestal"]=> string(6) "kd1009" [12]=> string(0) "" ["lcf"]=> string(0) "" [13]=> string(6) "gl1010" ["controller1"]=> string(6) "gl1010" [14]=> string(6) "gb1060" ["controller2"]=> string(6) "gb1060" [15]=> string(6) "kr7047" ["memory"]=> string(6) "kr7047" [16]=> string(6) "gd1080" ["fax1"]=> string(6) "gd1080" [17]=> string(6) "gd1090" ["fax2"]=> string(6) "gd1090" [18]=> string(0) "" ["other1"]=> string(0) "" [19]=> string(0) "" ["other2"]=> string(0) "" [20]=> string(0) "" ["other3"]=> string(0) "" [21]=> string(0) "" ["other4"]=> string(0) "" [22]=> string(6) "gb1040" ["controller3"]=> string(6) "gb1040" [23]=> string(0) "" ["controller4"]=> string(0) "" [24]=> string(0) "" ["controller5"]=> string(0) "" [25]=> string(0) "" ["efi1"]=> string(0) "" [26]=> string(0) "" ["efi2"]=> string(0) "" [27]=> string(0) "" ["efi3"]=> string(0) "" }


    array(56) { [0]=> string(1) "8" ["ID"]=> string(1) "8" [1]=> string(4) "2664" ["trans_id"]=> string(4) "2664" [2]=> string(9) "estudio20" ["model"]=> string(9) "estudio20" [3]=> string(6) "mr3011" ["adf"]=> string(6) "mr3011" [4]=> string(6) "mj5001" ["finisher"]=> string(6) "mj5001" [5]=> string(0) "" ["hole_punch"]=> string(0) "" [6]=> string(0) "" ["post_inserter"]=> string(0) "" [7]=> string(6) "md0101" ["duplexer"]=> string(6) "md0101" [8]=> string(6) "my1015" ["cassette1"]=> string(6) "my1015" [9]=> string(0) "" ["cassette2"]=> string(0) "" [10]=> string(0) "" ["cassette3"]=> string(0) "" [11]=> string(6) "kd1009" ["pedestal"]=> string(6) "kd1009" [12]=> string(0) "" ["lcf"]=> string(0) "" [13]=> string(6) "ga1031" ["controller1"]=> string(6) "ga1031" [14]=> string(6) "gf1110" ["controller2"]=> string(6) "gf1110" [15]=> string(6) "gc1120" ["memory"]=> string(6) "gc1120" [16]=> string(6) "gd1060" ["fax1"]=> string(6) "gd1060" [17]=> string(6) "gd1070" ["fax2"]=> string(6) "gd1070" [18]=> string(6) "my1016" ["other1"]=> string(6) "my1016" [19]=> string(6) "gc1050" ["other2"]=> string(6) "gc1050" [20]=> string(6) "kk1600" ["other3"]=> string(6) "kk1600" [21]=> string(0) "" ["other4"]=> string(0) "" [22]=> string(0) "" ["controller3"]=> string(0) "" [23]=> string(0) "" ["controller4"]=> string(0) "" [24]=> string(0) "" ["controller5"]=> string(0) "" [25]=> NULL ["efi1"]=> NULL [26]=> NULL ["efi2"]=> NULL [27]=> NULL ["efi3"]=> NULL }

  9. #9
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright.. i figured it out.. and I am a complete moron.

    I wrote my $place variable code backwards.. I was checking to see if the $place variable was equal to 1, then doing my code. At the end, I did a $place++. Then, I started the next if which was whether the $place was at 2.. Which, obviously it was, since I had just incremented it to that.. I just had to reverse it to where it checked high and worked down and the while loop worked fine. Rookie mistake.. Embarrassing.. Thanks for all your time guys.


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
  •