Reputation:
I have a database with a row 'genre' this contains all types of genres for the movie seperated by a space like an example of this would be
Animation Comedy Horror
They are all different genres so they need to be pulled out of the database and put in an array based on their genres I originally began coding this:
<?
sql = "SELECT moviename,genre FROM movieHosting";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
?>
But I soon realized shortly after that every genre is going to be needed to be put in the array seperate if I did
$genreArray = array($row->genre);
But this wont work it would generate an array like
$genreArray = array("Animation Comedy Horror");
But it needs to generate
$genreArray = array("Animation","Comedy","Horror");
Upvotes: 16
Views: 648
Reputation: 1567
Try using PHP's explode()
function. This function expects 2 parameters, a delimiter, and a string. The delimiter should be what you want to look for in the string to separate items. In practice this would look like:
$genreArray = explode(' ', $row->genre);
Now $genreArray
will look something like this (output generated from var_dump($genreArray)
):
array(3) { [0]=> string(9) "Animation" [1]=> string(6) "Comedy" [2]=> string(6) "Horror" }
Upvotes: 3
Reputation: 7795
WARNING
Stop using mysql_* functions, for multiple reasons : completely removed, officially deprecated, ... and for exhaustive list you can read this thread Why shouldn't I use mysql_* functions in PHP?, instead you can use mysqli_* or PDO
I think that it is a very bad design to store genre like you do it
How you will update your data ?
How to retrieve movies by genre ?
Each simple SQL operation will be a nightmare, instead of this, spend your time to correct your desgin, for example you can simply create a dedicated table from genre
ID | type
------------
1 | Comedy
2 | Horror
3 | Mangas
4 | ...
5 | Animation
And create an Entity Table that will associate the movie with [1, N] genre
MovieId | GenreId
-----------------
2 | 1
2 | 2
2 | 5
Upvotes: 3
Reputation: 260
Try this
Through multiple arrays:
<?
$sql = "SELECT moviename,genre FROM movieHosting";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
/* fetched value */
$moviename = $row['moviename'];
$genre= $row['genre'];
?>
Through single arrays to produce result like $genreArray = array("Animation","Comedy","Horror");
<?
$sql = "SELECT moviename,genre FROM movieHosting";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
/* fetched value */
$array =array();
foreach($row as $r){
$moviename = $r['moviename'];
$genre = $r['genre'];
$array_push = array_push($array,$moviename,$genre);
print_r($array_push);
}
?>
Upvotes: -1
Reputation: 187
Here is more close to your logic.
$string = 'Animation Comedy Horror';
$genra = explode(" ", $string);
print_r($genra); //Array ( [0] => Animation [1] => Comedy [2] => Horror )
echo $genra[0]; //Animation
echo '<hr>'; // OR
foreach($genra as $value){
echo $value . '<br>';
}
// Output
Animation
Comedy
Horror
Hope our answers brings more clarification to your problem.
Upvotes: 5
Reputation: 1156
$result = preg_split("/[\s,]+/", "Animation Comedy Horror");
print_r($result );
print_r(implode(",",$result));
OUT PUT
Array ( [0] => Animation [1] => Comedy [2] => Horror )
Animation,Comedy,Horror
Upvotes: 3
Reputation: 34914
Understand these and use accordingly.
<?php
$str = "Animation Comedy Horror";
$str_arr = explode(' ', $str);
print_r($str_arr);
$str_arr = split(' +', $str);
print_r($str_arr);
$str_arr = preg_split('/ +/', $str);
print_r($str_arr);
?>
Check This : https://eval.in/532527
Also Understand about,
explode : http://php.net/manual/en/function.explode.php
preg-split : http://php.net/manual/en/function.preg-split.php
split : http://php.net/manual/en/function.split.php
split is depricated now.
Upvotes: 4
Reputation: 15987
$genreArray = preg_split("/[\s,]+/", "Animation Comedy Horror");
print_r($genreArray );
The output:
Array
(
[0] => Animation
[1] => Comedy
[2] => Horror
)
Upvotes: -2
Reputation: 2934
Have you tried using the explode function?
Something like:
$genreArray = explode(" ", $row->genre);
Upvotes: 28