CodeIgniter Forums
How to separate an array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to separate an array (/showthread.php?tid=34723)

Pages: 1 2 3 4


How to separate an array - El Forum - 10-07-2010

[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



How to separate an array - El Forum - 10-07-2010

[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.


How to separate an array - El Forum - 10-07-2010

[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...



How to separate an array - El Forum - 10-07-2010

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


How to separate an array - El Forum - 10-07-2010

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

?? lmao


How to separate an array - El Forum - 10-07-2010

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


How to separate an array - El Forum - 10-07-2010

[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.


How to separate an array - El Forum - 10-07-2010

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


How to separate an array - El Forum - 10-07-2010

[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;';
        }



How to separate an array - El Forum - 10-07-2010

[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>';