Author Topic: How to skip a row inside of a $result?  (Read 215 times)

0 Members and 1 Guest are viewing this topic.

Offline scott.stephanTopic starter

  • Enthusiast
    • View Profile
How to skip a row inside of a $result?
« on: June 27, 2009, 05:58:31 PM »
Dudes, thank you so much for all your help. I have asked an insane number of dumb/obvious questions and everyone has been 100% helpful. In that spirit, here's another goofy Q:

I nab all the results from a table based on a condition. No problem:

Code: [Select]
$query_lots="SELECT * FROM lots_new WHERE lot_sku_id='$curr_key'";
$lots_result=mysql_query($query_lots) or die(mysql_error());

Now, that $lots_result could hold between 1 and maybe 5 rows. To find out, I count:

Code: [Select]
$num_lots=mysql_num_rows($lots_result);
Okay, so 1,2,3 rows, whatever. Here's the problem- These results are displayed in a table. IF there's just one record, then we display those results at the END of the first row of the table:

Item ItemDesc Lot LotQTY
Wh   Desc        5      15

However, if there are TWO or MORE results, we need to create a new row for them:

Item ItemDesc Lot LotQTY
Wh   Desc        5      15
                    6       10 '

So, I wrote some code:

Code: [Select]
while($lots_row=mysql_fetch_array($lots_result)){
echo "<td class='boltd'>$lots_row[lot] </td> <td class='boltd'>$lots_row[lot_qty] $bol_row[sau]</td>
</tr>";
if($num_lots > 1){
echo "<tr class'hint'><td class='boltd'> </td> <td class='boltd'>  </td> <td class='boltd'>$lots_row[lot] </td> <td class='boltd'>$lots_row[lot_qty] $bol_row[sau] </td>
</tr>";
}
}

And I'm sure you can see the problem. If there's just ONE ROW, we're okay! We finish up the row and go home. If there is more than one, we have a problem- It outputs every row.

So, basically what I need to do is say

Code: [Select]
if($num_lots>1){
for( $c=1; c < $num_lots; c==){
START FROM THE C ROW of $LOTS_RESULT!!!
}
START FROM THE SECOND RESULT
}


How can I tell $lots_result, the result of the SQL query, to start from the x row of it's retrieved results and not to spill it's guts everytime? Do I need to use a different call, like fetch_array()?

Thanks, I hope that wasn't too confusing.

Offline scott.stephanTopic starter

  • Enthusiast
    • View Profile
Re: How to skip a row inside of a $result?
« Reply #1 on: June 27, 2009, 07:01:54 PM »
I guess a simpler question is:

How can I tell the result of mysql_fetch_array , to start from the second row it fetches?

Offline scott.stephanTopic starter

  • Enthusiast
    • View Profile
Re: How to skip a row inside of a $result?
« Reply #2 on: June 27, 2009, 07:38:56 PM »
I feel like this has to be simple, but I can't seem to find anyone else who has ever had this problem. Hmm.

Offline scott.stephanTopic starter

  • Enthusiast
    • View Profile
Re: How to skip a row inside of a $result?
« Reply #3 on: June 27, 2009, 09:22:46 PM »
I ended up using this, suggested over at AskMetafilter:

Code: [Select]
$lots_result = ...
$first_row_values = mysql_fetch_array($lots_result))

while ($row = mysql_fetch_array($lots_result)) {
... 2onwards}

So it rips off the first result, I always display that one and if there are others remaining we deal with them

Offline Ken2k7

  • Freak!
    • View Profile
Re: How to skip a row inside of a $result?
« Reply #4 on: June 27, 2009, 11:50:28 PM »
After the if statement do another call to mysql_fetch_array(), so $row = mysql_fetch_array($lots_result) after the if ($num_lots > 1) statement. Don't misread and put that inside the if statement. Put it AFTER the entire if statement.
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.


Offline sasa

  • Guru
  • Addict
  • *
  • Gender: Male
    • View Profile
Re: How to skip a row inside of a $result?
« Reply #5 on: June 28, 2009, 05:47:13 AM »
try
Code: [Select]
$x = '';
while($lots_row=mysql_fetch_array($lots_result)){
echo $x, "<td class='boltd'>$lots_row[lot] </td> <td class='boltd'>$lots_row[lot_qty] $bol_row[sau]</td></tr>";
$x = "<tr class'hint'><td class='boltd'> </td> <td class='boltd'>  </td> ";
}

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.