Jump to content

Skipping a row via while(row)


scott.stephan

Recommended Posts

I'm in a situation where IF $row[whatever] == x , then I need it to skip the next five rows.

 

Using the traditional

 

$query="SELECT * FROM whatever";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
blahblahblah
}

 

How can I tell it "SKIP THIS ROW!" or "Skip X Rows". I can't do row++, that doesn't quite make sense. I don't think there's a numeric, like, "Well, this is row 1 of 20" and I can say, "Well, go to Row 6, if Row 1 is whatever".

Link to comment
https://forums.phpfreaks.com/topic/162964-skipping-a-row-via-whilerow/
Share on other sites

To skip a single row you can use continue.

 

quick example:

while($row=mysql_fetch_array($result))
{
if($row['whatever']==1) 
{
	continue;
}
// non-skipped code
}

 

To skip X amount of rows you could build an array and then loop through the keys numerically.

 

$array = array();
while($row=mysql_fetch_array($result))
{
$array[] = $row;
}
for($i=0; $i<count($array); $i++)
{
$row = $array[$i];
if($row['whatever'] == 1)
{
	// skip this row plus 5 more
	$i += 5;
	continue;
}
// non-skipped code
}

Why do I get the feeling that this is just bad design?

 

Ha. Because it is :) There are an enormous number of weird irregularities with the data coming in, coupled with changing demands on both ends of the sending/recieveing spectrum means that this program is a lot of cobbled together little solutions meant to meet a deadline more than to be incredibly elegant. Once things are settled into place there'll have to be a long sort of reconciliation period. Keyword right now is 'working' not 'charming'.

 

Anyway, thanks for the tips! I'll play a bit and see what comes up.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.