WordPress.org

Ready to get started?Download WordPress

Forums

Participants Database
[resolved] Need Help With Custom List Output - Almost There, But I'm Stuck (7 posts)

  1. harleyquinn
    Member
    Posted 1 year ago #

    I created a custom template to output the list so that it looks something like this:

    Business
    Address
    City, State Zip
    Phone

    I don't know if the php coding I used is the best or most efficient solution, but it does what I want except I can't figure out how to join the City, State and Zip fields to all be on the same line. Right now, all I can get it to do is spit out each field on it's own separate line.

    Here is the code I have so far, which doesn't include the state or zip fields:

    <?php if($this->field->name === 'business') : ?>
    <p class="bus">
    <?php $this->field->print_value() ?>
    </p>
    
    <?php elseif($this->field->name === 'address') : ?>
    <p class="add">
    <?php $this->field->print_value() ?>
    </p>
    
    <?php elseif ($this->field->name === 'city') : ?>
    <p class="city">
    <?php $this->field->print_value('city') ?>
    </p>				
    
    <?php elseif ($this->field->name === 'phone') : ?>
    <p class="pho">
    <?php $this->field->print_value() ?>
    </p>

    I have been trying to figure this out on my own now for hours, but my php knowledge is very basic and I am not having any luck. How do I join the City, State and Zip fields so that they are all on one line?

    http://wordpress.org/extend/plugins/participants-database/

  2. xnau
    Member
    Plugin Author

    Posted 1 year ago #

    Easiest way to do this is to build up the value then display it once all the pieces are in...

    <?php elseif ($this->field->name === 'city') :
    $location = $this->field->value;
    elseif ($this->field->name === 'state') :
    $location .= ', ' . $this->field->value;
    elseif ($this->field->name === 'zip') :
    $location .= ' ' . $this->field->value;
    ?>
    <p class="location">
    <?php echo $location ?>
    </p>

    See, the $location variable collects all the values then shows them once the last value is added. Of course, you have to make sure those values are listed in that order. There are more robust ways to do this, but I did it this way to kept things compatible with your code.

  3. harleyquinn
    Member
    Posted 1 year ago #

    That worked! Thanks.

    You mentioned that "there are more robust ways to do this," can you point me in the direction of what that would be? I know the solution I came up with is not the most elegant, but if there is a better way to get the output I need, I'm definitely open to any suggestions.

  4. xnau
    Member
    Plugin Author

    Posted 1 year ago #

    Well, the lovely thing about coding is there's usually many ways to get something done, so what's important is that it works and you understand it. The code is working, so the only reason to change it is to learn better coding habits...robustness means that the code is hard to break. The code I gave you can be broken, for instance, if the order of the fields gets changed. So that leads to a process of finding another way to get the job done that doesn't have that vulnerability.

    For me that would be mandatory because I'm writing code that lots of people will use...it would be less critical if you knew no one was going to mess with things.

  5. harleyquinn
    Member
    Posted 1 year ago #

    That make total sense and I can see where in your case as a plugin developer that writing code that won't break is a must. Thanks again for your help.

  6. roodavis
    Member
    Posted 4 months ago #

    I would like to use a custom list as described here. Is this the way that I should do it? Or has anything changed in the past eight months that would be easier? Built into the plugin template maybe?

  7. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Yes... I suggest you take a look at this article: Using the PDb_Template class in your custom templates

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.