Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[PHP]While Loop causes Row Skip

Options
  • 15-10-2007 6:51pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    Related to This Thread

    [PHP]print_r($row);
    while($row=mysql_fetch_array($result)){
    print_r($row);[/PHP]

    My first print returns what I want, $row[0] but once it enters the while loop it jumps to $row[1] meaning it doesn't display row[0]. I've tried using prev() and reset() but neither work.

    How can I stop it from skipping when it enters the loop?

    Thanks,
    S.


Comments

  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    If you can't help with the previous question can somebody tell me how to set internal pointer of an array to a specific key?

    E.g. The example above says that the key is at array[1] when I want it at array[0]. How do I specicfy it to array[0] (e.g. 'array_set($array,0);'?).

    Thanks,
    S.

    P.S. Sorry for double post.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Seachmall wrote: »
    [php]print_r($row);
    while($row=mysql_fetch_array($result)){
    print_r($row);[/php]My first print returns what I want, $row[0] but once it enters the while loop it jumps to $row[1] meaning it doesn't display row[0]. I've tried using prev() and reset() but neither work.

    How can I stop it from skipping when it enters the loop?

    OK first of all why are you printing off row, before you enter the loop? As far as I can see there's no need for it, you need one print statement, as the mysql_fetch_array() function will keep looping through the table returning rows until there's none left, and at that point the while loop will quit.

    So since you didn't pass in any second argument to the mysql_fetch_array() function it will assume MYSQL_BOTH, so you can use as a numeric array or an associative array (hash array). So row[0] is your first field of every row returned. Spec of function here

    So firstly is row[0] always not printed out at every iteration of the loop?

    If so then I'd say that maybe that element is undefined, or a null string and so you're seeing nothing. If this is the case, then I'd consider revising your SQL (which you haven't posted, so I can't comment any further).


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    silas wrote: »
    OK first of all why are you printing off row, before you enter the loop? As far as I can see there's no need for it, you need one print statement, as the mysql_fetch_array() function will keep looping through the table returning rows until there's none left, and at that point the while loop will quit.

    The print function before the loop is just for example purposes, just to show that I was checking before and after the loop began. In actually fact there is no print function in the script at all. They're all echos.
    silas wrote: »
    So since you didn't pass in any second argument to the mysql_fetch_array() function it will assume MYSQL_BOTH, so you can use as a numeric array or an associative array (hash array). So row[0] is your first field of every row returned. Spec of function here

    So firstly is row[0] always not printed out at every iteration of the loop?

    Correct, row[0] is not shown at all.
    silas wrote: »
    If so then I'd say that maybe that element is undefined, or a null string and so you're seeing nothing. If this is the case, then I'd consider revising your SQL (which you haven't posted, so I can't comment any further).

    There is definetly something in that row because when I print the array before it enters the loop it displays row[0] with all the info I want. The problem is once it enters the loop row[1] is the starting row, and I want to start with row[0]. Is there a way of specifing the pointer to row[0] or even a way to show all row keys (e.g row[0],row[1],row[2] etc.?) just to make sure that row[0] exists in the loop, that its just being skipped and not erased (which would explain why reset() and prev() don't work)?

    Thanks,
    S.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Seachmall wrote: »
    There is definetly something in that row because when I print the array before it enters the loop it displays row[0] with all the info I want. The problem is once it enters the loop row[1] is the starting row, and I want to start with row[0]. Is there a way of specifing the pointer to row[0] or even a way to show all row keys (e.g row[0],row[1],row[2] etc.?) just to make sure that row[0] exists in the loop, that its just being skipped and not erased (which would explain why reset() and prev() don't work)?

    Ok so when you say row[0] do you mean the complete 1st row of your query result, or the first field of the current row your looking at after php executes a call to mysql_fetch_array()??

    If its the former one thats the problem, then you need to look at your code, why is it reading after the 1st row? Are you making any api calls that would cause the internal pointer that functions like mysql_fetch_array() uses, to advance?

    Here if that does nothing for you then this looks promising, but you shouldn't have to use it for such a simple operation.

    http://ie2.php.net/manual/en/function.mysql-data-seek.php


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    silas wrote: »
    Ok so when you say row[0] do you mean the complete 1st row of your query result, or the first field of the current row your looking at after php executes a call to mysql_fetch_array()??

    If its the former one thats the problem, then you need to look at your code, why is it reading after the 1st row? Are you making any api calls that would cause the internal pointer that functions like mysql_fetch_array() uses, to advance?

    Here if that does nothing for you then this looks promising, but you shouldn't have to use it for such a simple operation.

    http://ie2.php.net/manual/en/function.mysql-data-seek.php

    I tried the mysql_data_seek (thanks for the link) but it didn't work, would me using multi-dimensional arrays have anything to do with that?.

    Is there a way to display all the keys in the array with displaying the info just to make sure that the row[0] exists?

    Thanks again.


  • Advertisement
  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    It's a pity you haven't posted the full code.

    It would be worth looking to see if the code in the loop's condition is evaluated, and again evaluated on dropping inside the loop.

    e.g.:
    $row=mysql_fetch_row(...);
    
    while($row=mysql_fetch_row(...)){
       //do stuff...
    }
    


    You should do the following instead:
    $row=mysql_fetch_row(...);
    while($row){
       //do stuff...
       $row=mysql_fetch_row(...);
    }
    


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Cantab. wrote: »
    It's a pity you haven't posted the full code.

    It would be worth looking to see if the code in the loop's condition is evaluated, and again evaluated on dropping inside the loop.

    e.g.:
    $row=mysql_fetch_row(...);
    
    while($row=mysql_fetch_row(...)){
       //do stuff...
    }
    


    You should do the following instead:
    $row=mysql_fetch_row(...);
    while($row){
       //do stuff...
       $row=mysql_fetch_row(...);
    }
    

    [PHP]<html>
    <head>
    <title>qwerty</title>
    </head>
    <body>
    //Main Details

    $new = mysql_query("SELECT CallsMain.*, CallsMainLink.* FROM CallsMain, CallsMainLink WHERE CallsMain.IdNo = CallsMainLink.IdNo AND CallsMainLink.Status = 'New' ORDER BY CallsMain.yy, CallsMain.mm, CallsMain.dd, CallsMain.idNo");
    $new_row=mysql_fetch_array($new);

    $parts = mysql_query("SELECT CallsMain.*, CallsMainLink.* FROM CallsMain, CallsMainLink WHERE CallsMain.IdNo = CallsMainLink.IdNo AND CallsMainLink.Status = 'Parts' ORDER BY CallsMain.yy, CallsMain.mm, CallsMain.dd, CallsMain.idNo");
    $parts_row=mysql_fetch_array($parts);

    $pending = mysql_query("SELECT CallsMain.*, CallsMainLink.* FROM CallsMain, CallsMainLink WHERE CallsMain.IdNo = CallsMainLink.IdNo AND CallsMainLink.Status = 'Pending' ORDER BY CallsMain.yy, CallsMain.mm, CallsMain.dd, CallsMain.idNo");
    $pending_row=mysql_fetch_array($pending);


    function display_days($row,$result){
    global $new;
    global $new_row;
    global $parts;
    global $parts_row;
    global $pending;
    global $pending_row;
    $currDay = "";

    while($row=mysql_fetch_array($result)){

    if($currDay!=$row){
    ?>
    <table><caption>
    <form method="POST" action="anotherpage.php" id="details">
    <?PHP echo $row;?>
    <input type="Hidden" value="<?PHP echo $row; ?>" name="day"/>
    <input type="submit" value="View Details" /></form>
    </caption>
    <tr><th>ID Number</th><th>
    Surname</th><th>
    First name</th><th>
    <?PHP
    $currDay = $row;
    }
    ?>
    <tr>
    <td> <?PHP echo $row; ?> </td>
    <td> <?PHP echo $row; ?> </td>
    <td> <?PHP echo $row; ?> </td>
    </tr>
    <?PHP

    }
    }
    ?>
    <div id='menu'>
    <H2>Menu</H2>
    <ul>
    <li><a href=''>Home</a></li>
    <li><a href=''>About</a></li>
    </ul>
    </div>
    <div id='boxwrap'>
    <h2>New</h2>
    <div id='new'>
    <?PHP display_days($new_row,$new);?>
    </table>
    </div>
    </div>
    <div id='boxwrap'>
    <h2>Parts</h2>
    <div id='parts'>
    <?PHP display_days($parts_row,$parts);
    ?>
    </table>


    </div>
    </div>

    <?PHP
    mysql_close($con);
    ?>
    </body>
    </html>[/PHP]


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    [php]$parts = mysql_query("SELECT CallsMain.*, CallsMainLink.* FROM
    CallsMain, CallsMainLink WHERE CallsMain.IdNo = CallsMainLink.IdNo AND
    CallsMainLink.Status = 'Parts' ORDER BY CallsMain.yy, CallsMain.mm,
    CallsMain.dd, CallsMain.idNo");
    $parts_row=mysql_fetch_array($parts); [/php]OK heres your first problem your calling "mysql_fetch_array()" this will move the internal row pointer of your query so its now pointing to the second row.

    [php]<?PHP display_days($parts_row,$parts);[/php]Then you call the method the display_days(), with the globals (which are a bad idea).

    [php]while($row=mysql_fetch_array($result)){
    if($currDay!=$row){ //... [/php]So now since you're still using globals the internal pointer points to the second row (this hasn't changed, thanks to the use of globals). And each call to "mysql_fetch_array()" gets each subsequent row starting at row 2.

    Hint: Don't use globals!! Its too easy to trip yourself up.

    Another point you have all your global declarations in the "display_days()" function which should make them only visible inside that function. Apparantly php does not have any strong type checking for this, so this might have undefined behavour, I don't fully know (I'm a C programmer), just a general observation more than anything.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    silas wrote: »
    OK heres your first problem your calling "mysql_fetch_array()" this will move the internal row pointer of your query so its now pointing to the second row.

    What do I use instead? I need to store the rows in an array and thats how I leared to do it.
    silas wrote: »
    Then you call the method the display_days(), with the globals (which are a bad idea).
    I don't understand, how else would I call the function?
    silas wrote: »
    So now since you're still using globals the internal pointer points to the second row (this hasn't changed, thanks to the use of globals). And each call to "mysql_fetch_array()" gets each subsequent row starting at row 2.

    Hint: Don't use globals!! Its too easy to trip yourself up.
    I got rid of the globals and moved what they were calling inside the function but now I can't call it. Will I have to create a function for all of the tables I want to display(or learn OOP)?
    silas wrote: »
    Another point you have all your global declarations in the "display_days()" function which should make them only visible inside that function. Apparantly php does not have any strong type checking for this, so this might have undefined behavour, I don't fully know (I'm a C programmer), just a general observation more than anything.
    I only need them within that function.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    OK forget my ranting about globals, completely forgot how php creates stuff variables as it goes.

    What I was going to suggest is to pass a string into display days that calls another function that essentially compares the strings and decides which query to run, and returning the resulting query.

    [php]
    function query_db(string query_type)
    {
    if (query_string == "parts")
    return mysql_query("..");
    else if (query_string == "new")
    return mysql_query("..");
    else if ...
    }


    function display_days (string query_type)
    {
    $query_result = query_db(query_type);

    // same as before
    while ($row = ...)

    }[/php]Anyways just take out the "mysql_fetch_array()" calls after the queries and you should be fine.


  • Advertisement
  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    silas wrote: »
    [php]
    function query_db(string query_type)
    {
    if (query_string == "parts")
    return mysql_query("..");
    else if (query_string == "new")
    return mysql_query("..");
    else if ...
    }

    Whats up with '(string query_type)' and then calling '(query_string)' within the function? How does that work?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    ignore string, should be just $query_string, and the one in query_db should be named differently to the display_days function. Also missing $ on loads of variables too.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    OK, so...
    [php]
    function query_db($query_string)
    {
    if ($query_string == "parts")
    return mysql_query("..");
    else if ($query_string == "new")
    return mysql_query("..");
    else if ...
    }


    function display_days ($query_type)
    {
    $query_result = query_db(query_type);

    // same as before
    while ($row = ...)

    }[/php]

    I'm still lost. I'll try googling.

    Thanks for all your help anyway :).


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    [php]
    function query_db(string query_type)
    {
    if (query_string == "parts")
    return mysql_query("..");
    else if (query_string == "new")
    return mysql_query("..");
    else if ...
    }


    function display_days (string query_type)
    {
    $query_result = query_db(query_type);

    // same as before
    while ($row = ...)

    }[/php]

    Theres one thing I'm confused about,
    If I use the above function to get the row where does $row come from to compare it in the while loop?

    Thanks.


Advertisement