Show States if a record exists

15 posts by 2 authors in: Forums > CMS Builder
Last Post: February 16, 2015   (RSS)

By Mohaukla - February 2, 2015 - edited: February 2, 2015

I have a list of distributors populating from a list of states.

  list($statesRecords, $statesMetaData) = getRecords(array(
    'tableName'   => 'states',
    'loadUploads' => false,
    'allowSearch' => false,
  ));


-------------------------------------------------------------




<?php foreach ($statesRecords as $key=>$record): ?>
  <div class="panel-group secundary" id="accordion2">
    <div class="panel panel-default">
      <div class="panel-heading">
       <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse2<?php echo $key;?>"><?php echo htmlencode($record['title']) ?></a>
       </h4>
      </div>
      <div id="collapse2<?php echo $key;?>" class="accordion-body collapse">
          <? // load records from 'distributors'
          list($distributorsRecords, $distributorsMetaData) = getRecords(array(
            'tableName'   => 'distributors',
            'loadUploads' => false,
            'allowSearch' => false,
            'where' => "states = '". $record['num'] . "'",
             ));
           ?>
          <?php foreach ($distributorsRecords as $distributor): ?>
            <div class="panel-body">
              <strong><?php echo htmlencode($distributor['title']) ?></strong><br/>
              <p><?php echo htmlencode($distributor['address']) ?><br/>
                 <?php echo htmlencode($distributor['phone']) ?><br/>
                 Contact: <?php echo htmlencode($distributor['contact_name']) ?>
              </p>
                                         <hr>
             </div>
          <?php endforeach ?>
                                   
        </div>
    </div>
  </div>
<?php endforeach ?>

Can I write it somehow to list ONLY the states that actually have a distributor. Please Note: I built this as a list of states that are associated in a list of distributors. Maybe I need to move that get records to the outside of the loop?

Let me know what you think.

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By claire - February 3, 2015

Hi Michael

There's a few different ways to do this depending on how efficient you want to be, but here's the simplest way:

<?php foreach ($statesRecords as $key=>$record): ?>
          <? // load records from 'distributors'
          list($distributorsRecords, $distributorsMetaData) = getRecords(array(
            'tableName'   => 'distributors',
            'loadUploads' => false,
            'allowSearch' => false,
            'where' => "states = '". $record['num'] . "'",
             ));
           ?>
  <?php if(empty($distributorsRecords)) { continue; } ?>
  <div class="panel-group secundary" id="accordion2">
    <div class="panel panel-default">
      <div class="panel-heading">
       <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse2<?php echo $key;?>"><?php echo htmlencode($record['title']) ?></a>
       </h4>
      </div>
      <div id="collapse2<?php echo $key;?>" class="accordion-body collapse">
          <?php foreach ($distributorsRecords as $distributor): ?>
            <div class="panel-body">
              <strong><?php echo htmlencode($distributor['title']) ?></strong><br/>
              <p><?php echo htmlencode($distributor['address']) ?><br/>
                 <?php echo htmlencode($distributor['phone']) ?><br/>
                 Contact: <?php echo htmlencode($distributor['contact_name']) ?>
              </p>
                                         <hr>
             </div>
          <?php endforeach ?>
                                   
        </div>
    </div>
  </div>
<?php endforeach ?>

This checks the list of distributors and skips the state if no distributors are found.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

OK I think my 'where' statement must be wrong. If I comment the 'where' statement out, I get the list of ALL possible distributors in each state. When I set the line back then I get NO states showing at all. So it must no be sorting the records correctly.

Any ideas?

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By claire - February 4, 2015

Hi Michael

What's the 'states' field in the distributor table look like? Is it a list?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By claire - February 4, 2015

Okay, what kind of list is it? A dropdown? A multiselect? Checkboxes?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By claire - February 4, 2015

Okay, what kind of list is it? A dropdown? A multiselect? Checkboxes?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

In this case I used a pull down multiple choice list. But I usually use check boxes.  Are there separate ways to express both?

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Do you need anything else from me?

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By claire - February 6, 2015

Hi Michael, sorry for the delay on this.

If the states are stored as a multiple choice, then it shows up in the database a little differently and thus the where clause needs to be changed. So try this instead:

<?php foreach ($statesRecords as $key=>$record): ?>
          <? // load records from 'distributors'
          list($distributorsRecords, $distributorsMetaData) = getRecords(array(
            'tableName'   => 'distributors',
            'loadUploads' => false,
            'allowSearch' => false,
            'where' => "states LIKE '%\t". mysql_escape($record['num']) . "\t%'",
             ));
           ?>
  <?php if(empty($distributorsRecords)) { continue; } ?>
  <div class="panel-group secundary" id="accordion2">
    <div class="panel panel-default">
      <div class="panel-heading">
       <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse2<?php echo $key;?>"><?php echo htmlencode($record['title']) ?></a>
       </h4>
      </div>
      <div id="collapse2<?php echo $key;?>" class="accordion-body collapse">
          <?php foreach ($distributorsRecords as $distributor): ?>
            <div class="panel-body">
              <strong><?php echo htmlencode($distributor['title']) ?></strong><br/>
              <p><?php echo htmlencode($distributor['address']) ?><br/>
                 <?php echo htmlencode($distributor['phone']) ?><br/>
                 Contact: <?php echo htmlencode($distributor['contact_name']) ?>
              </p>
                                         <hr>
             </div>
          <?php endforeach ?>
                                   
        </div>
    </div>
  </div>
<?php endforeach ?>

This might need some tweaking to get it right though.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/