Jump to content

from query results to an array


cleibesouza

Recommended Posts

I'm trying to separate the results set of a query into an array.
Below is the code:
[code]$participationQuery = "SELECT * FROM participation  WHERE users_id = '$userID'";
$participationResults = mysql_query($participationQuery, $connection) or die (mysql_error());
$partNumResults = mysql_num_rows($participationResults)

if($partNumResults){
while($row = mysql_fetch_array($participationResults)){
echo $row['activities_id'];
}//end while
}//end  if[/code]

This query gives me the results 2,10,7 without the commas. Meaning that they're all together. I'd need to get them separated. My idea was to dump the results on an array where the final result would be:
arrayName[0] = 2
arrayName[1] = 10
arrayName[2] = 7
but can't get that to work.

Any ideas/suggestions is greatly appreciated.

Thank you all.
Link to comment
Share on other sites

to create separation in the data output, you can simply add a separator to the echoing you're doing in the while() loop like so:

[code]while (stuff)
{
  echo $row['activities_id'].', ';
}[/code]

however, you will end up with a residual comma and space at the end. to circumvent this, add a counter to check if it is the first row. echo a comma and space BEFORE all but the first entry:

[code]$i = 1;
while (stuff)
{
  echo ($i > 1) ? ', '.$row['activities_id'] : $row['activities_id'];
  ++$i;
}[/code]

i've used the ternary operator here, but you can expand that out to a full-on if()-else statement if you are more comfortable with that.

alternatively, if you want to put the results into an array, you can simply go:

[code]while (stuff)
{
  $activity_ids[] = $row['activities_id'];
}[/code]

at the end of the run, $activity_ids will be an array containing all of the activity IDs found through the query.
Link to comment
Share on other sites

[!--quoteo(post=361842:date=Apr 5 2006, 12:11 AM:name=akitchin)--][div class=\'quotetop\']QUOTE(akitchin @ Apr 5 2006, 12:11 AM) [snapback]361842[/snapback][/div][div class=\'quotemain\'][!--quotec--]
to create separation in the data output, you can simply add a separator to the echoing you're doing in the while() loop like so:

[code]while (stuff)
{
  echo $row['activities_id'].', ';
}[/code]

however, you will end up with a residual comma and space at the end. to circumvent this, add a counter to check if it is the first row. echo a comma and space BEFORE all but the first entry:

[code]$i = 1;
while (stuff)
{
  echo ($i > 1) ? ', '.$row['activities_id'] : $row['activities_id'];
  ++$i;
}[/code]

i've used the ternary operator here, but you can expand that out to a full-on if()-else statement if you are more comfortable with that.

alternatively, if you want to put the results into an array, you can simply go:

[code]while (stuff)
{
  $activity_ids[] = $row['activities_id'];
}[/code]

at the end of the run, $activity_ids will be an array containing all of the activity IDs found through the query.
[/quote]

The array solution worked just fine.
Thanks.
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.