Jump to content

Loop problem


charlie321

Recommended Posts

I am trying to loop through a filtered table.  

This is the code of the loop:

$result = mysqli_query($con,$sql);
$num = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$i = 0;
$prebidder = $row['buser'];
mysqli_data_seek($result,0);
while($row = mysqli_fetch_array($result)) {
$buser = $row['buser'];
$itemno = $row['biditem'];
$price = $row['bidprice'];
$qty = $row['bidquantity'];

$query = "INSERT INTO invoiceitems(invnumber,itemnum,price,quantity) VALUES('$ttinvno','$itemno','$price','$qty')";#,unix_timestamp())";
mysqli_query($con,$query);

if ($prebidder != $buser) {
     $ttinvno= $ttinvno +1;
  }
$prebidder = $buser;
 $i++;

}

?>

The filtered data has 55 rows of which 28 are unique.  I am trying to loop through so that the names ($buser) match the invoice number ($ttinvno).  I have the rows indexed on buser descending.

But the rows don't seem to want to match correctly.  The invoice numbers go in sequence correctly but for instance the first two rows have the same name.  But no matter what I have done in this format either the invoice number changes after the first row, or it changes after the third row.  I want it to change after the second row.  I have tried multiple ways including prematurely defining row and then using mysqli_data_seek($result,0); to reset the result before the loop.

Obviously there is a flaw in my code.   Can anyone help?  Thanks very much in advance!

 

Link to comment
Share on other sites

First please use the code icon (<>) for inserting your code and choose PHP. It makes it much easier to read. Second, the most important part of your code was omitted, the query string ($sql). Without that we can't tell if you are filtering or sorting correctly. Did you output the rows to make sure they contains what you expect in the order you expect?

echo "<pre>";
print_r($row);
echo "</pre>";

 

Link to comment
Share on other sites

Sorry about that.  Here is the script with the sql.....

$sql = "SELECT itemnum,title,close,quantity,bidquantity, " .
          " bidprice,buyitprice,buser,bidder,biditem " .
          " FROM Items LEFT OUTER JOIN Bids " .
          " ON Items.itemnum = Bids.biditem " .
          " WHERE buser != '' and close > '$time' and outbid = ' 0 ' ".
          #"GROUP BY buser " .
          #"HAVING bidder=$buser or buser=\'$busername\' ". 
          " ORDER by buser ASC ";

$result = mysqli_query($con,$sql);
$num = mysqli_num_rows($result);
echo $num;
echo "<br>";
$i = 0;
$prebidder = null;
while($row = mysqli_fetch_array($result)) {
$buser = $row['buser'];
$itemno = $row['biditem'];
$price = $row['bidprice'];
$qty = $row['bidquantity'];
$query = "INSERT INTO invoiceitems(invnumber,itemnum,price,quantity) VALUES('$ttinvno','$itemno','$price','$qty')";#,unix_timestamp())";
mysqli_query($con,$query);
$prebidder = $buser;
if ( $prebidder != $buser ) {
     $ttinvno= $ttinvno +1;
  }
$prebidder = $buser;
 $i++;
}
?>
 

Link to comment
Share on other sites

FYI - if you had bothered to answer my question last Monday

we could have sorted your problem days ago.

I take the view that if you won''t spare a couple of minutes to answer my question, why should I give up my time to answer yours?

Also you have totally ignored the advice given by @mac_gyver on how to tackle the problem correctly, so I am guessing he's now one of this site's experts that will think twice before assisting you next time.

Link to comment
Share on other sites

Sorry you feel that way.  I guess he will think for himself.  It's not that I am ignoring anything.  What I have done so far is working although I'm sure it isn't proper.  I guess I'm not a proper guy.  What works for me works.  I'm just learning how to do things.  I don't know arrays at all and more or less I am fumbling through this.  I don't know what question you are talking about but I'm only looking for the answers that will work for me the way I do things.  And I do realize I might turn people off for this.  But to be honest I find responses from people on this board in particular to be somewhat insulting and ignorant on their own.  Not all.  There are some cool people on here.  But there are also some pompous type people here.  There is no understanding that not everyone on this board has been doing this for a very long time.  There are people who are trying their best to get through this.  There are people who get overwhelmed by head banging problems and might miss some posts.  There are people who probably would rather not come to this board and ask questions only to be ridiculed by php gods.  The purpose of this board is to help others.  You sound like a professor I had in college who was very intelligent, but he didn't understand that he had no clue how to help others.  This I feel is a very unfriendly board with exceptions and to be honest I would rather not have to ask questions on it.  As a matter of fact earlier today I asked on a non-php board a php question because I really like that board.  The people are friendly and want to help others.  They aren't as much concerned that someone asks questions in the proper way, but they try their best to help others. 

If I ask another question on this board, (and I probably will), you have absolutely no obligation to answer me just as anyone else doesn't.  But to make a comment such as you did in your post is ridiculous.  You sound like a person I'd rather not associate with.  You probably don't even golf!

But still I wish you well in anything you pursue.  Just try to remember we are all human and not too many are perfect like you!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.