Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help! What is wrong with this loop!

    The script below loops and pulls the id and lot from the db just fine. But when i click on the link "view" it only displays the last row in the loop. I hope the makes sence.




    <?
    session_start();
    include "conn.php";

    $contractor = $_POST['contractor'];
    ?>
    <html>
    <body bgcolor="lightpink">
    <form action=".php" method="POST">
    <h1><p align=center>The Corporation</p></h1>
    <h3><p align=center>BACK CHARGE SEARCH</center></p></h3>
    <h4><p align=center><i>Search Back Charge</i></center></p></h4>

    <input type="button" value="Main" style="font-size:15" onClick="parent.location='backchargemain.php'">


    <?


    $mysql1 = "select id,lot from bc where contractor = '$contractor' ";

    $result1= mysql_query($mysql1) or die ("Can't connect");




    echo "<table border=\"0\">";
    echo "<th>id</th>";
    echo "<th>Lot Date</th>";
    echo "<th></th>";


    $j = 0;

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

    $id = $row['id'];


    for ($i= 0; $i<$result1; $i ++) {

    }

    echo ($j++%2==0) ? '<tr bgcolor="#FFFFCC">' : '<tr bgcolor="#FFFFFF">';
    echo "<td>" .($row['id']) . "</td>";
    echo "<td>" .($row[1]) . "</td>";
    //echo "<td>" .($row[2]) . "</td>";
    //echo "<td>" .($row['contractor']) . "</td>";
    //echo "<td>" .substr($row['description'],0,25) . "</td>";
    //echo "<td>" .($row['status']) . "</td>";

    print '
    <td>

    <a href="viewdisplay.php?id='.$row['id'].'">View</a>

    </td>
    ';


    }
    echo '</table>';
    echo "</center>";

    echo "<hr>";
    ?>


    </form>
    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    When you click "view", does it run itself? Meaning, is
    viewdisplay.php the name of the script you're showing
    in your post?

    Let us know that.

    Otherwise, this might be something:

    echo "<td>" .($row['id']) . "</td>";
    <a href="viewdisplay.php?id='.$row['id'].'">View</a>

    should be?? (i'm not sure) ...

    echo "<td>" .($row[$id]) . "</td>";
    <a href="viewdisplay.php?id='.$row[$id].'">View</a>

    EDIT ...

    On second thought ...

    This line:
    or ($i= 0; $i<$result1; $i ++) {

    Why have a loop with the variable $i, when $i
    is not used anywhere?

    Maybe you wanted $row[$i] ??


    .
    Last edited by mlseim; 08-23-2006 at 03:31 PM.

  • #3
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes it go to viewdisplay.php at which time I'm just trying to echo out the id number.

  • #4
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I've put my comments in the code.

    PHP Code:
    <?
    session_start
    ();
    include 
    "conn.php";

    /***************
    This $_POST variable is going to be empty when you arrive at this page
    after clicking your link below.
    ***************/
    $contractor $_POST['contractor'];
    ?>
    <html>
    <body bgcolor="lightpink">

    /***************
    Your form's action is ".php"?  What is that doing for you?
    You should always include the "name" parameter on your form tags.
    ***************/
    <form action=".php" method="POST">
    <h1><p align=center>The Corporation</p></h1>
    <h3><p align=center>BACK CHARGE SEARCH</center></p></h3>
    <h4><p align=center><i>Search Back Charge</i></center></p></h4>

    <input type="button" value="Main" style="font-size:15" onClick="parent.location='backchargemain.php'">


    <?


    $mysql1 
    "select id,lot from bc where contractor = '$contractor' ";

    /***************
    The result of a bad mysql_query isn't necessarily that you can't connect;
    any SQL error could cause a bad query.  You should improve your error
    handling to at least display or log mysql_error().
    ***************/
    $result1mysql_query($mysql1) or die ("Can't connect");




    echo 
    "<table border=\"0\">";
    echo 
    "<th>id</th>";
    echo 
    "<th>Lot Date</th>";
    echo 
    "<th></th>";


    $j 0;

    while (
    $row mysql_fetch_array($result1)) {

    $id $row['id'];


    /***************
    This for loop is testing $i < $result1; $result1 at this point will be
    true or false, not a value to test $i against.  I realize you are
    not doing anything in the loop, but for your understanding I point this out.
    You probably meant to test $i < mysql_num_rows($result1).
    ***************/
    for ($i0$i<$result1$i ++) {

    }

    echo (
    $j++%2==0) ? '<tr bgcolor="#FFFFCC">' '<tr bgcolor="#FFFFFF">';
    echo 
    "<td>" .($row['id']) . "</td>";
    echo 
    "<td>" .($row[1]) . "</td>";
    //echo "<td>" .($row[2]) . "</td>";
    //echo "<td>" .($row['contractor']) . "</td>";
    //echo "<td>" .substr($row['description'],0,25) . "</td>";
    //echo "<td>" .($row['status']) . "</td>";

    print '
    <td>

    /***************
    Clicking this link will not keep the $_POST variable alive; you will lose the
    value of $_POST['
    contractor'].  Either include $_POST['contractor'] in
    your url and then test to see if $_GET['
    contractor'] exists up top, or
    change this link to submit your form and include a hidden input field named
    contractor, so $_POST['
    contractor'] will be there for you.
    ***************/
    <a href="viewdisplay.php?id='
    .$row['id'].'">View</a>

    </td>
    '
    ;
    /***************
    You are missing a </tr> tag; your html will not vaildate.
    ***************/


    }
    echo 
    '</table>';
    echo 
    "</center>";

    echo 
    "<hr>";
    ?>


    </form>
    </body>
    </html>

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    yea .... as Fumigator says ...

    You're not grabbing any incoming variables from the form,
    or from the link w/variable assignments. So when the script
    is called by itself, how does it know what variables are coming in?

    There just seems to be several things missing. It's hard to troubleshoot
    without actually trying it out.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •