Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if last item in recordset

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
i have a repeat region setup and i want a divider to echo until the last item in the recordset, so the end result is like this:

user1 | user2 | user3

right now its:
user1 | user2 | user3 |

I want to get rid of that last "|". here's my code, what do i need to do to alter it?
Code:
<?php do {
    echo $row_getusers['firstname']; ?>&nbsp;|&nbsp;
    <?php } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>
 
how would i go about doing that? i use dreamweaver and this is the code i'm using to loop that area:
Code:
<?php do { ?>
    <a href="viewjobs.php?user=<?php
    echo $row_getusers['userid'];
    echo $row_getusers['firstname']; ?></a>
    &nbsp;|&nbsp;<?php } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>
 
I don't know, as I don't use DreamWeaver.

But it would be a print statement followed by a while() loop.

I wouldn't use the do...while() loop.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
i think if i was capable if figuring out which record is the first one, and i guess i would then have to find out which one is the 2nd one to start the loop at that one, then SURELY there's got to be a way to say something like:
Code:
if(this_record <> last_record){
    echo "&nbsp;|&nbsp";
    }
 
You don't have to know which is the second or subsequent.

On the first call to a mysql_fetch_...() function, you get the first item in the result set. On subsequent calls, you get the subsequent items in the result set. When there are no more items, all of the mysql_fetch_...() functions return FALSE.


Fetch a row from the result set.
Output the item from the row data.
Loop on fetching rows from the result set.
print "|"
print the item from the row data.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Code:
while ($row_getusers = mysql_fetch_assoc($getusers)){
  $outputString .= $row_getusers['userid']; 
  $outputString .= $row_getusers['firstname'];
  $outputString .= '&nbsp;|&nbsp';
}
echo substr($outputString, 0, -3);

This will run through your result set and add the text onto the output sting, then when all results are done simple chop off the final unwanted '&nbsp;|&nbsp'

 
Taking your original code:
Code:
<?php do {
    echo $row_getusers['firstname']; ?>&nbsp;|&nbsp;
    <?php } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>
change it to this:
Code:
[red]$row_getusers=mysql_fetch_assoc($getusers);
 echo $row_getusers['firstname'];[/red]
do {
?>[blue]&nbsp;|&nbsp;[/blue]<?PHP
    echo $row_getusers['firstname']; 
 } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>


This will print out the first element, and then start the loop.Printing the the rest of the items. printing the "|" before each entry. As Sleipnir mentioned msyql_fetch_assoc will get the next row every time it is called. You don't need to know which is the first or last.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Code:
$row_getusers=mysql_fetch_assoc($getusers); // 1st fetch
 echo $row_getusers['firstname'];           // 1st print
do {
    echo 'nbsp;|&nbsp;';   //second prints
    echo $row_getusers['firstname']; 
 } while ($row_getusers = mysql_fetch_assoc($getusers)); //second fetch


 
O.k, sorry, missed a fetch have to add another fetch before the loop starts.

Actually I would do away with the do while. Probably use a For loop instead with the row count, but since that is the way he had it, I tried to keep it that way for consistency.

Code:
[red]$row_getusers=mysql_fetch_assoc($getusers);
 echo $row_getusers['firstname'];[/red]
[green]$row_getusers=mysql_fetch_assco($getusers);[/green]
do {
?>[blue]&nbsp;|&nbsp;[/blue]<?PHP
    echo $row_getusers['firstname'];
 } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita:
No, you don't. Just change the do...while() loop to a while() loop. That way, the fetch happens at the beginning of the loop, not at the end.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
So there are several ways, to do it. As there are with everything. Any way it works.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Very True. Hat's off to you Sleipnir.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
well, here's the thing, neither of those codes work.

Sleipnir, i get an error on your code. It's expecting a ";" and getting an unexpected "{" or "}", i can't remember.

vacunita, yours does not pull the first name. it starts from the 2nd name and goes on, but skips over the first name.

btw, i don't know if i mentioned before, but these are all links too, so we're back to square 1 with my original code:
Code:
<?php do { ?><a href="viewjobs.php?user=<?php
		echo $row_getusers['userid']; ?>" style="color:#FF6600;"><?php echo $row_getusers['firstname']; ?></a>&nbsp;|&nbsp;<?php } while ($row_getusers = mysql_fetch_assoc($getusers)); ?>
 
ok, well, basically i did only what I know to do. My query sorts all the users by Name (ASC). SO i just created another query that sorts them by name again only descending. then I just take the first one and check the row to see if the userid of matches the one of that row. if it doesn't, than it puts the separator. if it does match, i doesn't put it. it may be more complicated than other ways, but it works.
 
blasterstudios:
One thing you must always keep in mind is that no one but you has your database, so no code posted here could possibly have been tested. Anyway, I haven't posted any code for you to run.

There is absolutely no point in doing an additional query. Particularly when you're trying to work around a parse error.

It's simple. As I said before, fetch the first record and output it:

[tt]$row_getusers=mysql_fetch_assoc($getusers);
echo $row_getusers['firstname'];[/tt]

Then loop through the rest of the rows, outputting "|" then the row:

[tt]while ($row_getusers = mysql_fetch_assoc($getusers))
{
echo '&nbsp;|&nbsp;';
echo $row_getusers['firstname'];
}[/tt]




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
everyone seems to be going round and round in circles... why not try this? maybe it's not the most efficient or elegant way but it does work (you may have to tweak the substr params a bit but otherwise it should work straight off.
Code:
while ($row_getusers = mysql_fetch_assoc($getusers)){
  $outputString .= $row_getusers['userid']. " "; 
  $outputString .= $row_getusers['firstname'];
  $outputString .= '&nbsp;|&nbsp';
}
echo substr($outputString, 0, -3);
It's a simple while loop, during each iteration it constructs the output string appending the ' | ' after each name. When the loop exits the substr function removes the final redundant ' | ' (hence the substr is set to take characters from position 0 to 3 before the end.
You end up with something like:
"21 Fred | 432 Joe | 199 Dave"
 
I have no idea why it skipped, it does work as described. unless you missed the first echo outsaide of the loop.

But if you found another way to do it, that you are confortable with, then that is okay too.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top