Liam
Liam

Reputation: 9855

Explode array and define Variables

I have the following which returns

"soccertennisfootball"

$interestsquery  = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "{$row['interest']}";
} 

Can somebody please explain how I can explode the data and assign it as variables? I've been looking around at tutorials but they all seem to have some sort of delimeter?


Ive tried the following oly its acting very weird and printing out multiple times?

the following...

$interestsquery  = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
 $interests[] = $row['interest'];
 $interest1 = $interests[0];
 $interest2 = $interests[1];
 $interest3 = $interests[2];

 print $interest1 . " - " . $interest2 . " - " . $interest3;

} 

Prints out

"Tennis - Tennis - Footy - Tennis -Soccer - Footy"

Upvotes: 2

Views: 487

Answers (6)

user428517
user428517

Reputation: 4193

In each iteration of the while loop, you can only access one of the results (first 'soccer', then 'tennis', then 'football'). In your second code block, you're trying to somehow access all three inside the while loop.

Instead, inside the while loop, push that single result into an array:

$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $interests[] = $row['interest'];
    //First 'soccer' will be pushed into $interests, then 'tennis', then 'football'.
}

Now that you have all the values in the array, you can access each individually. This is done OUTSIDE the while loop:

echo $interests[0] . ' - ' . $interests[1] . ' - ' . $interests[2];

This will print "soccer - tennis - football".

Alternately, like some have said, you can loop through the $interests array and echo each value, or do whatever else you'd like with them.

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 73031

Given your clarifications (using MySQL, interest is the only column, and sample output) I would suggest using GROUP_CONCAT(). This puts it all on the MySQL side and allows you to pull the single row from $result.

Try the following sample query:

$query = "SELECT GROUP_CONCAT(interest SEPARATOR ' - ') FROM user_interests
WHERE user_id = " . $usersClass->userID() . "
GROUP BY user_id";

Upvotes: 0

Chintan
Chintan

Reputation: 136

Use below snippet to store values in array

$interestsquery  = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$result = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$result[] = $row['interest']
}
print_r($result);

Upvotes: 0

Senad Meškin
Senad Meškin

Reputation: 13756

Use extract method from php http://php.net/manual/en/function.extract.php

 $interestsquery  = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
    $result = mysql_query($interestsquery);

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
     extract($row);
     echo $interest;
    }

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799490

Put them in an array, then do as you will with it after.

$interests = array();

while (...)
{
  $interests[] = $row['interest'];
}

Upvotes: 0

Cfreak
Cfreak

Reputation: 19319

They aren't a single string, you're just echoing it out. Store it somewhere. This code stores it in a new array. It may be useful but it really depends on what you want to do with it.

$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
     $interests[] = $row['interest'];
} 

Upvotes: 2

Related Questions