WordPress.org

Forums

Simple Fields
If several instances have same values, list only once (3 posts)

  1. Filespit
    Member
    Posted 2 years ago #

    Hello there,

    I'm having some difficulties with a PHP script using the Simple Fields plugin. I'm still a rookie at PHP so help would be much appreciated.

    On my website I'm listing retailers using a set of six fields, where I add the retailer's name, street adress, zip code, e-mail, city and country. Several retailers are located in the same country, and some in the same city.

    Since some retailers are located in the same country, I only want to list each country once, and the same goes for cities. How do I solve this?

    My code;

    <div id="buy-list">
    	<ul>
    		<?php $store = simple_fields_get_post_group_values($post->ID, "Store", true, 2); ?>
    		<?php foreach ($store as $store_value) : ?>
    		<li class="buy-country">
    			<span class="buy-country-span">
    				<?php echo $store_value["Country"] ?>
    			</span>
    			<ul>
    				<li class="buy-city">
    					<span>
    						<?php echo $store_value["City"] ?>
    					</span>
    					<ul>
    						<li class='buy-store'>
    							<span>
    								<?php echo $store_value["Retailer"] ?>
    							</span>
    							<ul>
    								<li class='buy-info'>
    									<span>
    										<?php echo $store_value["Retailer address"] ?><br />
    										<?php echo $store_value["Retailer phone"] ?><br />
    										<a href="mailto:<?php echo $store_value["Retailer email"] ?>"><?php echo $store_value["Retailer email"] ?></a>
    									</span>
    								</li>
    							</ul>
    						</li>
    					</ul>
    				</li>
    			</ul>
    		</li>
    		<?php endforeach; ?>
    	</ul>
    </div>

    http://wordpress.org/extend/plugins/simple-fields/

  2. Pär Thernström
    Member
    Plugin Author

    Posted 2 years ago #

    perhaps something like this:

    <div id="buy-list">
    	<ul>
    		<?php $store = simple_fields_get_post_group_values($post->ID, "Store", true, 2); ?>
    		<?php
    		$prev_country = "";
    		foreach ($store as $store_value) : ?>
    		<li class="buy-country">
    			<?php if ($prev_country !== $store_value["Country"] ) { ?>
    				<span class="buy-country-span">
    					<?php echo $store_value["Country"] ?>
    				</span>
    				<?php
    				$prev_country = $store_value["Country"];
    			} ?>
    			<ul>
    				<li class="buy-city">
    					<span>
    						<?php echo $store_value["City"] ?>
    					</span>
    					<ul>
    						<li class='buy-store'>
    							<span>
    								<?php echo $store_value["Retailer"] ?>
    							</span>
    							<ul>
    								<li class='buy-info'>
    									<span>
    										<?php echo $store_value["Retailer address"] ?><br />
    										<?php echo $store_value["Retailer phone"] ?><br />
    										<a href="mailto:<?php echo $store_value["Retailer email"] ?>"><?php echo $store_value["Retailer email"] ?></a>
    									</span>
    								</li>
    							</ul>
    						</li>
    					</ul>
    				</li>
    			</ul>
    		</li>
    		<?php endforeach; ?>
    	</ul>
    </div>
  3. Filespit
    Member
    Posted 2 years ago #

    Thank you Pär, that helped a bit but it's still not right.

    This way each country only displays one city in separate <div class="buy-country"> tags, and makes the html code look like this.

    <div id="buy-list">
      <ul>
        <li class="buy-country">
          <span>Sweden</span>
          <ul>
            <li class="buy-city">
              <span>Gothenburg</span>
            </li>
          </ul>
        </li>
        <li class="buy-country">
          <span>Sweden</span>
          <ul>
            <li class="buy-city">
              <span>Stockholm</span>
            </li>
          </ul>
        </li>
        <li class="buy-country">
          <span>Denmark</span>
          <ul>
            ...
          </ul>
        </li>
      </ul>
    </div>

    The code only displays the first "buy-country" instance, the rest that have the same country are hidden. I want the html code to look like this;

    <div id="buy-list">
      <ul>
        <li class="buy-country">
          <span>Sweden</span>
          <ul>
            <li class="buy-city">
              <span>Gothenburg</span>
            </li>
            <li class="buy-city">
              <span>Stockholm</span>
            </li>
          </ul>
        </li>
        <li class="buy-country">
          <span>Denmark</span>
          <ul>
            ...
          </ul>
        </li>
      </ul>
    </div>

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Simple Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic