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 ','; } } ?> Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/ 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289886 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 Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289891 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289892 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? Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289893 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 Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289897 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289899 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289901 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 Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289903 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']); Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289905 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? Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289913 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289914 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. Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289916 Share on other sites More sharing options...
corillo181 Posted July 4, 2007 Author Share Posted July 4, 2007 thats good to know thank you Link to comment https://forums.phpfreaks.com/topic/58461-solved-how-i-do-this-loop/#findComment-289917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.