Jump to content

Help with Array and MySql While Loop


svgmx5

Recommended Posts

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}
}

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

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.