0

I want to fetch information from one table and loop that until it's done, with the help of a while loop. Although, I want one column to be printed only once inside the loop.

There's two solutions I've come up with...

<?php $i = 0; // while loop start if($i == 0){ // stuff to print $i++; } // while loop end ?> 

And ofcourse, I could just make another query before the while loop.

But these methods really aren't too efficient. Is there a less messy way to do this?

Actually, I'd be okay with running another query before the while loop, if it didn't get so messy, and perhaps if I could just re-use the query intended for the loop (I fetch everything from the table). I tried experimenting with mysql_fetch_field, but I'm not sure I get how it works, or if that even helps here * embarrassed *


Currently, the code looks like so:

$fetch = mysql_query("SELECT * FROM `pms` JOIN `pm_conversations` ON pms.ConvID = pm_conversations.ID WHERE `ConvID`='".$_GET['id']."'"); $i = 0; while($print = mysql_fetch_array($fetch)){ if($i == 0){ echo $print['Subject']; $i++; } <table> <th><?=$print['From']?>, <?=$print['DateSent']?></th> <tr><td><?=$print['Message']?></td></tr> </table> } 

3 Answers 3

3

If I understand your question correctly, I think you've got the right idea. I'm guessing you want to print something extra the first iteration (like maybe column headers using the array keys)?

$cnt= 0; while($row = mysql_fetch_assoc($res)) { if(0 == $cnt++) { // first iteration only } // all iterations } 

Or, if I'm totally off a better description of what you're trying to do and the real world situation would help.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, that was pretty much the solution I showed in the first post. Anyway, to describe the real world situation: I'm grabbing all information of a table, to compose a conversation. And above the conversation, is where I want to put the subject name of the conversation, so I only want to print that once.
Actually, I added my current code. I hope that'll make it more clear.
at first i could not understand how this even works, but yeah $cnt is 0 on first enter :)
1

Have you thought about do...while? It has all of the benefits of while, plus it allows for code to conditionally happen before the first iteration.

$res = mysql_fetch_array( $resource ); // run the magical run once query/functionality/bunnies/whatever // Hey, I'm tired. Let there be bunnies. do { // put the regular while loop constructs in here; } while( $res = mysql_fetch_array( $resource ) ); 

Comments

1
$rs = mysql_query("SELECT * FROM tbl"); $row = mysql_fetch_array($rs); do { echo $row['column_name']; } while($row = mysql_fetch_array($rs)); 

I don't know if this is what you need. Hope this one helps. =]

2 Comments

Yep! It was as easy as just adding the $print variable outside the while loop first. Thanks.
My mistake. Sorry, but this doesn't execute the first PM inside the conversation. I'll go with cwallenpoole's solution instead. Thanks for your help either way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.