Jump to content

Recommended Posts

I have a string sored in my database like this 2,1|2,5|3,6

 

I have exploded this to get the following:

 

2,1

2,5

3,6

 

I now need to explode this again and as I need to query the database with the numbers. For example:

 

2,1 needs to be exploded as this Represents 2 Parts Vodka (The 1 is the id for vodka)

 

Does anyone know how I could do this. See http://www.wemustdesign.com/cocktail/ to see what I have done so far.

 

$query="SELECT * FROM cocktails";
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$ingredients=mysql_result($result,$i,"ingredients");
echo "id: $id | name: $name<br><br />
";

$ic=0;
$ingredients_explode=explode("|",$ingredients);
$ingredients_count=count ($ingredients_explode);
echo"count: $ingredients_count<br />";
while ($ic<$ingredients_count){

echo"$ingredients_explode[$ic]<br />";

$ic++;
}


$i++;
}

 

 

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

You were on the right lines, but ive cleaned up the code you had to make it a bit shorter and easier to follow (hopefully!)

 

<?php
$query="SELECT * FROM cocktails";
$result=mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo "id: $row[id] | name: $row[name]<br><br />";
$ingredients_explode=explode("|",$row[ingredients]);
foreach($ingredients_explode AS $ingredient){
	$ingredient_parts = explode(',',$ingredient);
	$parts = $ingredient_parts[0];
	$ingredient_id = $ingredient_parts[1];
	//Im not sure how youre getting that vodka is id 1 etc im guessing a database table - in which case, select from there where your id is $ingredient_id

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273795
Share on other sites

I am creating a database of cocktails. In the database I have 2 tables, 'Ingredients' and 'Cocktails'.

 

Cocktail X's ingredients will be stored as:

 

2,1|2,5|1,3

 

This explodes to:

 

2,1

2,5

1,3

 

and then again to:

 

2 parts orange

2 parts lemonade

1 part vodka

 

How do I create a search for this database as you are not able to just simply query the database to see which cocktails contain vodka for example?

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273821
Share on other sites

I think I'd be tempted to change the way you're storing the ingredients in the database. Maybe something like:

 

2=orange,2=lemonade,1=vodka

 

That way you can do a search like this:

SELECT * FROM cocktails WHERE ingredients LIKE '%vodka%'

 

To explode this first use:

$ingredients=explode(",",$row['ingredients']);

 

Then explode this further:

$measure=explode("=",$ingredients[$index]);
echo $measure[0].' parts '.$measure[1];

Where $index could be a loop to display the ingredients of each cocktail.

 

Just a thought...

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273824
Share on other sites

The list() is very handy in combination with explode().

It can simplefy this code from:

<?php
$ingredient_parts = explode(',',$ingredient);
$parts = $ingredient_parts[0];
$ingredient_id = $ingredient_parts[1];
?>

 

to:

<?php
list($parts, $ingredient_id) = explode(',', $ingredient);
?>

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273927
Share on other sites

Yes I have read the article. I know that it would be best to have seperate tables which I done from the stsrt. The only problem I found was linking up the 'amounts' with the ingredients.

 

If I have all of the ingredients in one table how do I store the 'amount' that is needed to make the cocktail in the second table. This is why I started using 2,3 where the first number is the number of shots needed and the second number is the id of the ingredient from another.

 

Got this working ok but just didnt know how I would create a search for the database.

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273930
Share on other sites

This is why I started using 2,3 where the first number is the number of shots needed and the second number is the id of the ingredient from another.

 

Why not make these two seperate fields? eg;

 

CREATE TABLE ingredients (
  id INT PRIMARY KEY AUTO INCREMENT,
  description VARCHAR(80)
);

CREATE TABLE  cocktails (
  id INT PRIMARY KEY AUTO INCREMENT,
  description VARCHAR(80)
);

CREATE TABLE cocktail_ingredients (
  id INT PRIMARY KEY AUTO INCREMENT,
  cocktail_id INT, # relates to the cocktails table.
  ingredient_id INT, # relates to the ingredients table
  parts INT
);

 

Then.. to create cocktail X, firstly we'd need to define the ingredients.

 

INSERT INTO ingredients (description) VALUES ('orange');
INSERT INTO ingredients (description) VALUES ('lemonade');
INSERT INTO ingredients (description) VALUES ('vodka');

 

These are now in the database and can also be used in other cocktails. next we need to define the name of our cocktail.

 

INSERT INTO cocktails (description) VALUES ('x');

 

Then all we need to do is insert the ingredients and parts related to our cocktail.

 

INSERT INTO cocktail_ingredients (cocktail_id, ingredient_id, part) VALUES (1,1,2);
INSERT INTO cocktail_ingredients (cocktail_id, ingredient_id, part) VALUES (1,2,2);
INSERT INTO cocktail_ingredients (cocktail_id, ingredient_id, part) VALUES (1,3,1);

 

Now... to find the name of any cocktail containing vodka.

 

SELECT cocktail.description AS cocktail_name, cocktail_ingredients.ingredient_id
FROM cocktail, cocktail_ingredients
WHERE cocktail_ingredients.ingredient_id = 3;

 

My syntax may be a little off as its been a long time since Ive used MySql, but hopefully this will give you the idea. There alot more you can do with this sort of design as well.

Link to comment
https://forums.phpfreaks.com/topic/55402-explode-question/#findComment-273978
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.