Jump to content

Explode


wemustdesign

Recommended Posts

Hi,

 

I am creating a webiste about places to visit. Now each place can have different attractions, and I have stored these attractions as follows:

 

Location

Las Vegas

 

Attractions

3 | 6 | 7 | 10

 

The attractions data is the reference to the id of a certain attraction.

 

What I am looking to do is create a total of all all of the locations that have a Casino (id: 3) for example

 

I know I have to explode the Attractions but then how do I query for id=3 for every row?

 

If someone could just point me in the right direction i would be over the moon!

Link to comment
https://forums.phpfreaks.com/topic/242436-explode/
Share on other sites

you would want something like this perhaps..

 

$attractions = "3 | 6 | 7 | 10";
$explode = explode(" | ", $attractions);
foreach($explode as $value){ //separate each id
  // do whatever with each id
}

 

I'm not 100% following you on this one, but the code above will split each id into a value of the $explode array, then will isolate each id with a foreach loop so you can do what you want with each...

Link to comment
https://forums.phpfreaks.com/topic/242436-explode/#findComment-1245145
Share on other sites

Hi,

 

Thanks for the reply.

 

The only problem is that there are multiple rows in the MySQL table so need to explode all of the values from the attractions column for every row and then add this all up.

 

Las Vegas 3|67|7|89

New York  3|67|89|43

LA              2|67|77|8

 

So this would some up

 

(id  3)Casinos (2)

(id 67)Cinemas (3)

 

I just can't get my head round how I would go through each row and add all these values up. Is the method I am using storing them as 56|78|3|4 the best way to do this do you think?

Link to comment
https://forums.phpfreaks.com/topic/242436-explode/#findComment-1245171
Share on other sites

Hi,

 

Thanks for the reply.

 

The only problem is that there are multiple rows in the MySQL table so need to explode all of the values from the attractions column for every row and then add this all up.

 

Las Vegas 3|67|7|89

New York  3|67|89|43

LA              2|67|77|8

 

So this would some up

 

(id  3)Casinos (2)

(id 67)Cinemas (3)

 

I just can't get my head round how I would go through each row and add all these values up. Is the method I am using storing them as 56|78|3|4 the best way to do this do you think?

 

Stop now and don't do it this way.  You need a table that stores each attraction in a separate row with the location.  Then its much easier:

 

Locations
id   name
1    Las Vegas

Attractions
id   name
1    Casino
2    Brothel

Location_Attractions
location_id    attraction_id
1                   1
1                   2

 

SELECT Locations.name FROM Locations, Locations_Attractions WHERE Locations_Attractions.attraction_id = 2

 

Link to comment
https://forums.phpfreaks.com/topic/242436-explode/#findComment-1245182
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.