SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Styling Database Array Output

    I'm fairly experienced at displaying data from database tables and styling it, inserting breaks, parentheses, links, etc. I'd like to learn how to do the same thing with a database ARRAY display.

    Below is a script that displays the children and grandchildren of the mammal order Carnivora:

    PHP Code:
    function getChild($current) {
      
    $children = Array();

      foreach(
    $current AS $value) {
        
    $query "SELECT * FROM gz_mammals WHERE Parent = '$value'";
        
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());

        while (
    $myrow mysql_fetch_array($results)) {
          
    array_push($children$myrow['Name']);
        }
      }
      return 
    $children;
    }

    // ECHO VARIABLES...
    $people = array('Carnivora');
    $children getChild($people);
    $GC getChild(getChild(array('Carnivora')));

    // DISPLAYS...
    print_r($children);
    print_r($GC); 
    The children display looks like this:

    Code:
    Array ( [0] => Canidae [1] => Felidae [2] => (etc.) )
    I have two specific questions:

    1) How can I change the display to this?:

    PHP Code:
    <a href="Canidae" title="Canidae">Canidae</a><br>
    <
    a href="Felidae" title="Felidae">Felidae</a><br
    2) Is there a way to utilize the numerals from the original display (e.g. [1]), making them display BEFORE each name and bumping each one up one digit, so the first one begins with 1, rather than 0?

    Code:
    1) <a href="Canidae" title="Canidae">Canidae</a><br>
    2) <a href="Felidae" title="Felidae">Felidae</a><br>
    
    OR
    
    1) Canidae<br>
    2) Felidae<br>
    Thanks.

  2. #2
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try a loop like this...

    PHP Code:
    foreach ($children as $num => $name)
    {
        echo 
    "$num) <a href=\"$name\">$name</a><br/>";

    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $links 
    = array('Canidae''Felidae');

    foreach (
    $links as $key => $link) {
        print 
    $key ') <a href="' $link '" title="' $link '">' $link '</a>';
    }
    Oh was just too slow >.>
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cholmon's right about the foreach, but for numbering why not use an <ol>?
    PHP Code:
    echo '<ol>';
    foreach (
    $children as $num => $name

        echo 
    "<li><a href=\"$name\">$name</a></li>"
    }
    echo 
    '</ol>'

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    1:
    Code:
    function output($array) {
    	foreach($array as $string) {
    		printf("<a href="%s" title="%s">%s</a><br />", $string, $string, $string);
    	}
    }
    2:
    if the arrays are always 0, 1, 2, 3, ... then:
    Code:
    function output($array) {
    	echo '<ol start="0">';
    	foreach($array as $string) {
    		printf("<li><a href="%s" title="%s">%s</a></li>", $string, $string, $string);
    	echo '</ol>';
    	}
    }
    which doesn't actually make use of the index values directly themselves (hence why i said "if the arrays are always 0, 1, 2, 3, ...").

    if the arrays aren't always 0,1,2,.. :
    Code:
    function output($array) {
    	foreach($array as $key => $string) {
    		printf("%d <a href="%s" title="%s">%s</a><br />", $key, $string, $string, $string);
    	}
    }

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks for all the tips. Cholmon's script is almost perfect. I easily figured out how to use it to display data with or without numerals. The only problem is the numerals begin with numerals, but Mudshark's revision fixed that.

    I hardly ever use lists and wasn't even familiar with the <ol> tag, but it looks like a good solution for situations where I want to add numbers.

    I'll check out the other tips, too. I need to learn lots of ways to manipulate this data.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •