...

View Full Version : Help! What is wrong with this loop!



mwm
08-23-2006, 03:15 PM
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>

mlseim
08-23-2006, 03:25 PM
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] ??


.

mwm
08-23-2006, 03:27 PM
Yes it go to viewdisplay.php at which time I'm just trying to echo out the id number.

mwm
08-23-2006, 05:03 PM
Anyone?

Fumigator
08-23-2006, 05:37 PM
I've put my comments in the 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().
***************/
$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'];


/***************
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 ($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>

/***************
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>

mlseim
08-23-2006, 05:45 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum