svgmx5 Posted June 17, 2011 Share Posted June 17, 2011 Does anyone how i can seperate multiple values that are retrieve from the DB with a ',(comma)' with in a while loop? What i have a database in which values are stored, but in one table i have say building names and each building can have as many types of colors, etc. So if one building has 4 colors then each of those colors need to displayed with a ' , '. Like is aid i have a buildings table and a colors table, the colors for each of the buildings are stored in that table. What i've been trying to do is to add all the values in an array and then implode it : $getArchitects = mysql_query("SELECT * FROM building_architects WHERE point_id='$pointID'") or die(mysql_error()); $numArchitects = mysql_num_rows($getArchitects); if($numArchitects!=0){ while($pointArchitects = mysql_fetch_assoc($getArchitects)){ $architectID = $pointArchitects['architect_id']; $getArchitectName = mysql_query("SELECT * FROM architects WHERE id='$architectID'") or die(mysql_error()); $architectName = mysql_fetch_assoc($getArchitectName); $pointArchArr = '<a href="architects/'.stringForUrl($architectName['full_name']).'">'.$architectName['first_name'].''.$architectName['last_name'].'</a> '; } echo implode(', ',$pointArchArr); } The problem with that is that it just keeps adding the value of each previous row... so row one has one value, row two as the previous rows value and its own value, row 3 has the previous 2 rows values and its own and so and son . Does anyone know how i can fix this? Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/ Share on other sites More sharing options...
blacknight Posted June 17, 2011 Share Posted June 17, 2011 if i understand you want this <a href="architects/'AAAAA">aaaaa AAAAA</a> <a href="architects/'BBBBB">BBBBB BBBBB</a> <a href="architects/'CCCCC">CCCCC CCCCC</a> <a href="architects/'DDDDD">DDDDD DDDDD</a> if so try this .. add $pointArchArr = ''; befor wheree clause change $pointArchArr = '<a href="architects to $pointArchArr .= '<a href="architects this will peep adding new values to the string $pointArchArr and stop overwriting them if this is wrong post back with more info Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231271 Share on other sites More sharing options...
svgmx5 Posted June 18, 2011 Author Share Posted June 18, 2011 Alright, that didn't seem to work....i got this error Warning: implode() [function.implode]: Invalid arguments Also i forgot to post some code that i think is important as well.... $getLocations = mysql_query("SELECT * FROM locations") or die(mysql_error()); while($locations = mysql_fetch_assoc($getLocations)){ $getArchitects = mysql_query("SELECT * FROM building_architects WHERE point_id='$pointID'") or die(mysql_error()); $pointArch[] = ''; while($pointArchitects = mysql_fetch_assoc($getArchitects)){ $architectID = $pointArchitects['architect_id']; $getArchitectName = mysql_query("SELECT * FROM architects WHERE id='$architectID'") or die(mysql_error()); $architectName = mysql_fetch_assoc($getArchitectName); $pointArch.= '<a href="architects/'.stringForUrl($architectName['full_name']).'">'.$architectName['first_name'].''.$architectName['last_name'].'</a> '; echo implode(', ',$pointArch); } } Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231352 Share on other sites More sharing options...
svgmx5 Posted June 18, 2011 Author Share Posted June 18, 2011 Also, i realize the code that i just coded is different, i changed it a little but it still the same Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231354 Share on other sites More sharing options...
mikesta707 Posted June 18, 2011 Share Posted June 18, 2011 You can only use implode on an array. Implode takes an array and returns a string with all the elements put into a string separated by the delimiter you pass on. You are passing in a string, this why you are getting your error. Now, as for your problem. I don't quite understand what you are trying to do. Could you perhaps explain your problem more clearly Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231357 Share on other sites More sharing options...
svgmx5 Posted June 18, 2011 Author Share Posted June 18, 2011 What i'm trying to do is, is to add a ',' in between each result ie: 1, 2, 3,4 and so on. Like i have on the code example above, i'm getting the results from a main table called locations...from there i grab an id name architectID and i get all the architects from a table named buildingArchitects matching each location, (each location can have more than 1 architect) i then grab another ID from the buildingArchitects table to get that persons/architecs actual information..the end result would as follow: location name | architect name(if multiple seperated with a ',') location name | architect name(if multiple seperated with a ',') location name | architect name(if multiple seperated with a ',') I hope this helps you guys better. Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231359 Share on other sites More sharing options...
mikesta707 Posted June 18, 2011 Share Posted June 18, 2011 How are your tables structured? When you say each location can have more than 1 architect, how do you store the multiple architects? Are they each in their own row with matching locations? Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231363 Share on other sites More sharing options...
svgmx5 Posted June 18, 2011 Author Share Posted June 18, 2011 the tables are structred as follow: Locations Table: id | name buildingArchitects Table: This table stores the architect ID and Location ID by adding a new row every time an architect is added or if there is multiple values so if there are 2 architects named 'b' under location 'c' then there are two rows with the location_id being the same. (location_id and location_id are both unique match each other) (id is a primary ID and it auto increments, but its not related to anything and used solely to number the rows) id | location_id | architect_id Finally the architects table just holds the architects information (id and architect_id are both unique and math each other) id | full_name | first_name | last_name That makes sense? Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1231365 Share on other sites More sharing options...
svgmx5 Posted June 20, 2011 Author Share Posted June 20, 2011 So...can anyone help me out here? Quote Link to comment https://forums.phpfreaks.com/topic/239666-help-with-array-and-mysql-while-loop/#findComment-1232249 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.