corillo181 Posted July 4, 2007 Share Posted July 4, 2007 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 ','; } } ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 ????????????? i don't know what you mean Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 One question - I'm not sure what you're doing with username() Is this the entire script or just a part? Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 The code to pull the data from the database is definitely in a loop - check your query and make sure that is matching more than one record. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 everything works fine.. i just want to separa the usernames with a comma when there is more than one username with the given query. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 How many records get pulled with that query? What is the value of $count? Change this line: echo $fetch['user_id']; To this: echo username($fetch['user_id']); Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 this works, but my question is how? because i know the while loop is supposed to get every array instead is skipping the first one, those mysel_fetch_assoc forces the array it has to be skipped in the loop? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 thats good to know thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.