Jump to content

[SOLVED] how i do this loop?


corillo181

Recommended Posts

i just want to insert a coma after every name and none on the last name i know how to do this with normal numbers and arrays, but not with queries

 

<?php

$date=date("Y-m-d",mktime(0,0,0,$month,$day,$year));
$getPart=mysql_query("SELECT party_id,user_id FROM tra_party WHERE DATE_FORMAT(date,'%Y-%m-%d')='$date'")or die(mysql_query());
$count=mysql_num_rows($getPart);



while($fetch_id=mysql_fetch_array($getPart)){

for($i=1;$i<=$count;$i++){

echo username($fetch_id['user_id']);
echo ',';

}
}

?>

Link to comment
Share on other sites

Insert this before the while statement:

$fetch_id=mysql_fetch_array($getPart);
echo username($fetch_id['user_id']).',';

 

Then remove the line that echo's the comma before the previous line:

echo ','.username($fetch_id['user_id']);

 

EDIT: Edited last echo statement.

Link to comment
Share on other sites

<?php
$date=date("Y-m-d",mktime(0,0,0,$month,$day,$year));
$getPart=mysql_query("SELECT party_id,user_id FROM tra_party WHERE DATE_FORMAT(date,'%Y-%m-%d')='$date'")or die(mysql_query());
$fetch=mysql_fetch_assoc($getPart);
echo $fetch['user_id'];
while ($fetch_id=mysql_fetch_array($getPart)) {
  echo ','.username($fetch_id['user_id']);
}
?>

 

I don't think you had any need for that FOR loop.  Try that and see how it works.

Link to comment
Share on other sites

that code echos one id and one username i want all the username wiht out the id.. the username() function search for usernames with that given id.. so i just want the username and commas to separate the names if more than one..

 

jay,john,last

Link to comment
Share on other sites

this code is fine

<?php

$date=date("Y-m-d",mktime(0,0,0,$month,$day,$year));
$getPart=mysql_query("SELECT party_id,user_id FROM tra_party WHERE DATE_FORMAT(date,'%Y-%m-%d')='$date'")or die(mysql_query());


while ($fetch_id=mysql_fetch_array($getPart)) {
  echo username($fetch_id['user_id']);
}
?>

 

it prints

 

usernameusernameusername as many username as it finds

 

i need to separate them

 

username,username,username,username

Link to comment
Share on other sites

Fisrt you are defining the query with mysql_query()

 

If you call it in a loop sure enough, it'll cycle through the rows it pulls from the database.

 

If you add one mysql_fetch_assoc() before the loop you can pull the first row and show that.

 

Here's the smart part. If there is more than one row returned the loop will kick in and every result that gets displayed will show a comma before it. If there is only one row the while loop will be ignored and no commas will be shown giving you just the name.

Link to comment
Share on other sites

Just read that back and I don't think it makes great sense - sorry, its very late!

 

Say you have 5 rows about to be pulled from the database.

 

The first mysql_fetch_assoc() outside of the loop pulls the first row and it gets displayed with no comma.

 

As there are another 4 rows to extract the loop becomes TRUE and the remaining rows are pulled one by one. By echo'ing a comma follwoed by the row you're sure to get the results without that annoying leading comma on the end.

 

If there was only one row to be pulled then the while loop would become instantly FALSE and no code inside the braces would be executed.

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.