Welcome Guest, Not a member yet? Register   Sign In
How to separate an array
#1

[eluser]JayArias[/eluser]
I am currently trying to separate these tags from my database. What happens is that every item in the catalog has a tags section where users can input tags like: Green, Apples, Awesome and I need it to come back out from the function like so

Code:
#Green, #Apples

Just keep in mind I need to separate the tags so I can send the user to /viewby/tags/apples when they click on the #apples tag.

Code:
function loadTags()
    {
        $this->db->select('tags');
        $query = $this->db->get('catalog');
        $row = $query->result_array();
        
        foreach( $row as $tag)
        {
            echo '#'.$tag['tags'];
        }
    }
result
Code:
#business,masters,music,sound,grey#green, toro, bull, mens#######exclimation, person, awesomeness, girl, white, black#fishie, grey, blue, tank, tank-top#flamingo, love, red, tank-top, tanktop, white, drip
#2

[eluser]Twisted1919[/eluser]
Code:
$tags = array();
foreach( $row as $tag)
{
$tags[] = explode(',',$tag['tags']);//gets only the word
}
//now you have the $tags array which contains arrays with your tags
$out = '';
foreach($tags AS $tag)
{
   if(is_array($tag))
   {
    foreach($tag AS $t)
    {
     $out.= '<a href="'.site_url('search/tag/'.$t).'">#'.$t.'</a>, ';
    }
   }
}
echo $out;

Should work, not sure though.
#3

[eluser]CroNiX[/eluser]
before your foreach, do a print_r($row); I have a feeling your result is because the result from the database is one long string and not separate tags like you are expecting (from your code anyway).

If thats the case, in your foreach you can
Code:
$tags = array();
foreach($row as $tagstring)
{
    $tagline = explode(',', $tagstring);
    foreach($tagline as $tag)
    {
        $tags[] = '#' . trim($tag);
    }
}
//now all of your tags are in this array the way you want
printr($tags);

foreach($tags as $tag)
{
    echo "$tag<br />";
}

//#business
//#master
//#music
//...

$tagstring = implode(', ', $tags);
echo $tagstring;
//#business, #apples, #music...
#4

[eluser]Twisted1919[/eluser]
Ha, pretty much the same ha ?Big Grin
#5

[eluser]JayArias[/eluser]
Code:
Array ( [0] => #business [1] => #masters [2] => #music [3] => #sound [4] => #grey

?? lmao
#6

[eluser]CroNiX[/eluser]
Yeah, that IS funny.
#7

[eluser]JayArias[/eluser]
Fixed that bug, but for some crazy reason it's only pulling the tags from the first db entry. I need all of them.
#8

[eluser]CroNiX[/eluser]
Post your code and your db schema. Hard to give complete answers without the complete info.
#9

[eluser]JayArias[/eluser]
Code:
$this->db->select('tags');
        $this->db->where('aprooved', 'yes');
        $query = $this->db->get('catalog');
        $this->db->limit('50');
        $row = $query->row_array();
        
        $tags = array();
        
        foreach($row as $tagstring)
        {
            $tagline = explode(', ', $tagstring);
            foreach($tagline as $tag)
            {
                $tags[] = '#' . $tag;
            }
        }        
        foreach($tags as $tag)
        {
            echo '<a href="/search/'. $tag .'" class="cyan">'. $tag .'</a>,&nbsp;';
        }
#10

[eluser]CroNiX[/eluser]
Please post the contents of the $row array.
Code:
$this->db->select('tags');
        $this->db->where('aprooved', 'yes');
        $query = $this->db->get('catalog');
        $this->db->limit('50');
        $row = $query->row_array();

echo '<pre>';print_r($row);echo '</pre>';




Theme © iAndrew 2016 - Forum software by © MyBB