Jump to content


Photo

from query results to an array


  • Please log in to reply
2 replies to this topic

#1 cleibesouza

cleibesouza
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 05 April 2006 - 04:05 AM

I'm trying to separate the results set of a query into an array.
Below is the 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

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.

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 05 April 2006 - 04:11 AM

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:

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

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:

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

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:

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

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

#3 cleibesouza

cleibesouza
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 05 April 2006 - 04:16 AM

[!--quoteo(post=361842:date=Apr 5 2006, 12:11 AM:name=akitchin)--][div class=\'quotetop\']QUOTE(akitchin @ Apr 5 2006, 12:11 AM) View Post[/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:

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

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:

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

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:

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

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users