• 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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor eskapism

    (@eskapism)

    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>
    Thread Starter Filespit

    (@filespit)

    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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘If several instances have same values, list only once’ is closed to new replies.