Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts

    styling php arrays?

    How would i go about breaking an array up into divs, here is an example of the array im working with.

    PHP Code:
    Array ( 

    [
    currentTQTime] => 2010-01-13 03:05:28 
    [trainingEndTime] => 2010-01-19 04:43:51
    [trainingStartTime] => 2010-01-13 02:22:49 
    [trainingTypeID] => 3380 
    [trainingStartSP] => 45255 
    [trainingDestinationSP] => 256000 
    [trainingToLevel] => 
    [skillInTraining] => 



  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What do you want you're div's to look like?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    i just want to get them into divs, i can style them from there

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, I mean what kind of structure do you want for them?
    ie:
    Code:
    <div name="currentTQTime">2010-01-13 03:05:28</div>
    <div name="trainingEndTime">2010-01-19 04:43:51</div>
    Something like that?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    oh yes sorry, yes that is pretty much how i want them

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Then I'd say an array_walk is the best approach:
    PHP Code:
    function arrayToDivs($val$key)
    {
        
    sprintf('<div name="%s">%s</div>' PHP_EOL$key$val);
    }
    array_walk($yourArray'arrayToDivs'); 
    Now, if you want to capture the result, this won't work. Sometime between php 4.2 and 5.0, they deprecated the call-time pass-by-reference, so you would need to globalize (or choose a similar approach):
    PHP Code:
    function arrayToDivs($val$key$var)
    {
        
    $GLOBALS[$var] .= sprintf('<div name="%s">%s</div>' PHP_EOL$key$val);
    }

    array_walk($yourArray'arrayToDivs''s');
    print 
    $s
    The only reason why I chose the $GLOBALS approach is because I hate globalizing period. You could global $s and drop the third argument, or even global $$var and keep the third param. I'd keep the third param myself, since at least this way you can control the variable name at runtime, so thats good.

    Edit:
    BTW, I should mention that this would still work (its also enabled by default):
    PHP Code:
    function arrayToDivs($val$key$var)
    {
        
    $var .= sprintf('<div name="%s">%s</div>' PHP_EOL$key$val);
    }

    $s '';
    array_walk($a'arrayToDivs', &$s);
    var_dump($s); 
    But that functionality will disappear as of PHP 6.
    Last edited by Fou-Lu; 01-13-2010 at 03:57 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    hmm, ok well this is how the array is printed from the api im using

    PHP Code:
    print_r(Charactersheet::getSkillInTraining($charsheet)); 

    im not to familiar with this way of doing things, but it is pulling the data from the xml file it creates, maybe i should just edit the xml style?

    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    
    <eveapi version="2">
    
      <currentTime>2010-01-13 05:55:04</currentTime>
    
      <result>
    
        <currentTQTime offset="1">2010-01-13 05:55:05</currentTQTime>
    
        <trainingEndTime>2010-01-19 04:43:51</trainingEndTime>
    
        <trainingStartTime>2010-01-13 02:22:49</trainingStartTime>
    
        <trainingTypeID>3380</trainingTypeID>
    
        <trainingStartSP>45255</trainingStartSP>
    
        <trainingDestinationSP>256000</trainingDestinationSP>
    
        <trainingToLevel>5</trainingToLevel>
    
        <skillInTraining>1</skillInTraining>
    
      </result>
    
      <cachedUntil>2010-01-13 06:55:04</cachedUntil>
    
    </eveapi>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Thats an option as well.
    Otherwise, the Charactersheet::getSkillInTraining($charsheet) itself is what retrieves it as an array:
    PHP Code:
    $a Charactersheet::getSkillInTraining($charsheet);
    array_walk($a'arrayToDiv'); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    thank you for the help, i created a seperate php file with the generated array and used jquery to retrieve and and create the divs for each one. thanks!


  •  

    Posting Permissions

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