user2212564
user2212564

Reputation: 261

Foreach results in numbered format

I am using codeingiter and have soms results that i am showing from a database. The results are shown using PHP as follows:

 <?php
        if(isset($foo)){
            foreach($foo as $bar){ ?>
            <ol><li>
                <div class="results">  
                    <p class="name"><?php echo $bar['name']; ?></p>
                    <p class="address"><?php echo $bar['address']; ?></p>
                    <p class="address"><?php echo $bar['postcode']; ?></p> 
                    <span><?php number_format($bar['miles'],3) ?></span> miles               
                </div>
            </li></ol>  
            <?php
          }
        }
        ?>

I've also tried adding the class 'results' to the ol li.results and no luck. Have added a border and the border wraps around each result so i know my tags are in the right place. CSS:

ol li {
    border:1px solid red;
    list-style-type: decimal;
}

In the reset.css i had this:

loads...of...elements including ul, ol and li {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

I removed the ol, li from this and i got the numbers working but all have 1. for each result rather than 1, 2 3

These are shown as a list of addresses and i would like to have them numbered so the first one would have 1.RESULT 1, 2.RESULT 2 and so on... I've tried to use the ordered list tag and nothing seems to show up regarding numbers. i have checked in firebug and the tag is firing out for each one. Is this a css issue or the way PHP is showing the results?

Any help would be much appreciated.

Upvotes: 1

Views: 1537

Answers (4)

Alan Piralla
Alan Piralla

Reputation: 1196

If I got it right, your <OL> and </OL> tags should stay outside the for cicle, while you should wrap your output between <LI> and </LI> inside the cicle. Also, it's not a good practice to put DIVs inside LI elements, avoid it if you can by changing your styles.

Upvotes: 1

Jonathan de M.
Jonathan de M.

Reputation: 9828

Try the following

<?php if(isset($foo)): ?>
    <ol>
      <?php foreach($foo as $bar): ?>
      <li class='results'>
        <p class="name"><?php echo $bar['name']; ?></p>
        <p class="address"><?php echo $bar['address']; ?></p>
        <p class="address"><?php echo $bar['postcode']; ?></p> 
        <span><?php number_format($bar['miles'],3) ?></span> miles               
      <li>
      <?php endforeach; ?>
    </ol> 
<?php endif; ?>

Upvotes: 4

dKen
dKen

Reputation: 3127

It's a CSS issue if the numbers are not showing. Common issue with reset stylesheets. Try adding:

list-style-type: decimal;

to your list style.

Upvotes: 1

Karl Adler
Karl Adler

Reputation: 16836

You forgot the <li> tags inside the <ol> tag

<?php
        if(isset($foo)){
            echo '<ol>';
            foreach($foo as $bar){ ?>
            <li>
                <div class="results">  
                    <p class="name"><?php echo $bar['name']; ?></p>
                    <p class="address"><?php echo $bar['address']; ?></p>
                    <p class="address"><?php echo $bar['postcode']; ?></p> 
                    <span><?php number_format($bar['miles'],3) ?></span> miles               
                </div>
            </li>  
            <?php
          }
          echo '</ol>';
        }
?>

Upvotes: 6

Related Questions