Jump to content

Turning an array into an ul


horseatingweeds

Recommended Posts

I've been trying to figure a way to approach, but I'm still confused. I'm returning an array of arrays from a DB and I need to turn them into an ul.

 

The array is similar to this if you print_r the returned value: Array ( [article_id] => 1 [breed_name] => Spider ) Array ( [article_id] => 2 [breed_name] => Spider ) Array ( [article_id] => 3 [breed_name] => Spider ) Array ( [article_id] => 4 [breed_name] => Spider ) Array ( [article_id] => 5 [breed_name] => Chimp ) Array ( [article_id] => 6 [breed_name] => Chimp ) Array ( [article_id] => 7 [breed_name] => Desert ) Array ( [article_id] => 8 [breed_name] => Desert ) Array

 

And what I essentially want is this:

 

<ul>

  <li>

      <ul>

        <li>Spider</li>

        <li><a href='spider/1'></a></li>

        <li><a href='spider/2'></a></li>

        ...

      </ul>

  </li>

  <li>

      <ul>

        <li>Desert</li>

        <li><a href='desert/7></a></li>

        ...

      </ul>

      ...

  </li>

</ul>

 

I hope I've made this clear enough. Thanks

Link to comment
Share on other sites

Should be nested foreach loops, but I'm not sure if you array is setup right to do what your trying to do.

 

echo "<ul>";

foreach($breedname as $breeditems){
echo "<li><ul>";

echo "<li>Title</li>";

foreach($breeditems as $item){
echo "<li>$item</li>";
}

echo "</ul></li>";
}

echo "</ul>";

 

That's basically what you need to do, but I think your array is setup funny.

Link to comment
Share on other sites

Essentially I want to take an array like this:

Array ( [article_id] => 5 [breed_name] => Chimp [url] => feeding ) Array ( [article_id] => 6 [breed_name] => Chimp [url] => play ) Array ( [article_id] => 7 [breed_name] => Desert [url] => feeding-2 ) Array ( [article_id] => 8 [breed_name] => Desert [url] => history-5 ) Array ( [article_id] => 1 [breed_name] => Spider [url] => history ) Array ( [article_id] => 2 [breed_name] => Spider [url] => history-2 ) Array ( [article_id] => 3 [breed_name] => Spider [url] => history-3 ) Array ( [article_id] => 4 [breed_name] => Spider [url] => history-4 ) 

 

and turn it out into a nested array.

 

<ul>
  <li>[b]Chimp[/b]
    <ul>
      <li>feeding</li>
      <li>play</li>
    </ul>
  </li>
  <li>[b]Desert[/b]
    <ul>
      <li>feeding-2</li>

.... and so on

 

 

 

 

Link to comment
Share on other sites

OK

 

public static function GetArticleArray($db)
	{
		$select = $db->select();
		$select->from(array('a' => 'breed_articles'), array('a.article_id', 'a.breed_name', 'a.url'));
		$select->order('a.breed_name');
		$data = $db->fetchAll($select);

		foreach ($data as $d)
		{

			$listing[$d['article_id']] = $d['breed_name'];
		}

		return $data;
	}

 

I'm not sure explaining how the array is defined will help, but I'm passing the $data variable to a Smarty template.

 

		<ul>
		{foreach from=$test item=t}

			{if $categ != $t.breed_name}
			<li>{$t.breed_name}</li>

			{/if}

			<li><a href="{geturl action='preview'}?id={$t.article_id}}">{$t.breed_name}/{$t.url}</a></li>

			{assign var='categ' value=$t.breed_name}
		{/foreach}
		</ul>

 

From this controller:

 

public function indexAction()
	{
		$test = DatabaseObject_BreedArticles::getArticleArray($this->db);


		$this->view->test = $test;
	}

 

I'm using Zend Framework. I'm just trying to figure an efficient way to make a nested ul out of this array. I think the first thing I need to do is figure out what kind of array will work best for making the ul, then figure out how to make the current array into it.

Link to comment
Share on other sites

Recursive function?

 

<?php
$array = array(1,2,array(2.1,2.2,2.3,array(2.31,2.32,2.33)),3,4,array(4.1,4.2,4.2),5);
function convertToUl($array){
echo "<ul>\n";
foreach($array as $v){
	if(is_array($v)){
		convertToUl($v);
	}else{
		echo '<li>'.$v."</li>\n";
	}
}
echo "</ul>\n";
}
convertToUl($array);
?>

 

Edit: Not quite right. I'll have another go. I hate writing recursive functions. Particularly late at night.

Link to comment
Share on other sites

I came up with:

<?php

$arr[] = array( 'article_id' => 5,
                'breed_name' => 'Chimp',
                'url' => 'feeding' );

$arr[] = array( 'article_id' => 6,
                'breed_name' => 'Chimp',
                'url' => 'play' );

$arr[] = array( 'article_id' => 7,
                'breed_name' => 'Desert ',
                'url' => 'feeding-2' );

$arr[] = array( 'article_id' => 8,
                'breed_name' => 'Desert ',
                'url' => 'feeding-io' );

$arr[] = array( 'article_id' => 7,
                'breed_name' => 'Desert ',
                'url' => 'feeding-9' );

$arr[] = array( 'article_id' => 7,
                'breed_name' => 'Forest ',
                'url' => 'feeding-6' );

echo '<pre>' . print_r($arr, true) . "</pre>\n\n";

$cat = null;
$last = count($arr) - 1;
$o = false;

echo '<ul>';

foreach($arr as $k => $sArr)
{
    if($cat != $sArr['breed_name'])
    {
        if($o)
        {
            echo "    </ul>\n  </li>\n";

            $o = false;
        }

        echo "\n  <li>\n    " . $sArr['breed_name'] . "\n    <ul>\n";
        echo '      <li>' . $sArr['url'] . "</li>\n";

        $cat = $sArr['breed_name'];
        $o = true;
    }
    else
    {
        echo '      <li>' . $sArr['url'] . "</li>\n";
    }

    if($k == $last) echo "    </ul>\n  </li>\n";
}

echo '</ul>';

?>

 

Link to comment
Share on other sites

Yeah, I know what you mean. I couldn't get anywhere with this last night. Here it's half past six, and I've come up with something. It's similar to yours wildteen88 by using Smarty code - thanks.

 

			<ul>
			<li>{$test.0.breed_name}
				{assign var='categ' value=$test.0.breed_name}
				<ul>
		{foreach from=$test item=t}

			{if $categ != $t.breed_name}
				</ul>
			</li>
			<li>{$t.breed_name}
				<ul>


			{/if}

			<li><a href="{geturl action='preview'}?id={$t.article_id}}">{$t.breed_name}/{$t.url}</a></li>

			{assign var='categ' value=$t.breed_name}
		{/foreach}
				</ul>
			</li>
		</ul>

Link to comment
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.