0

I've got a list of "sites" in an SQL database that I'd like to display on a web page. I need to create a div ID for every site. For the time being I'm doing this (fetch the table into a $_SESSION array, get and display the name of the sites in a loop and do an echo):

 <div id='leftnavigation1' class="leftnavigation">
       <ul>
           <?php
                $maxKeys = max(array_keys($_SESSION['usersmeter'])); //check how many sites are in the usersmeter table
                for ($i = 0; $i <= $maxKeys; $i++) { //loop every key in the array
                            $siteName = $_SESSION['usersmeter'][$i]['siteName'];    
                echo "<li id='siteID$i'><a href='#'><span>" . $siteName . "</span></a></li>"; //display the name of the sites
                }
            ?>                        
       </ul>
 </div>

It works well but I think it is a bad practice to include php code into the View.

How else can I do? An AJAX in Javascript ?

Thanks,

3
  • I would suggest doing a foreach loop and if you really want to keep your HTML clean, then use a template engine like Twig or Smarty or any other. Commented Jul 15, 2014 at 11:12
  • @Janisimo Thanks I'll have a look at these templates, but not sure I'll be able to install it as I'm using a mutualized hosting. Commented Jul 15, 2014 at 11:55
  • there shouldn't be any problems installing it because it is just a PHP library that you include in your project and use it :) I prefer TWIG and use it for all my projects. It's really sexy and it makes HTML sexy too :) Commented Jul 16, 2014 at 8:00

1 Answer 1

0
   Like that you will br try:

 <?php    

    $my_array = array(
         'GSU4300' => 'Lili Markina',
         'GSU4301' => 'John Kokina',
         'GSU4304' => 'Bill Clinong',
         'GSU4305' => 'Obamark Chiko'
    );

    echo '<ul id="somethig">';
    foreach ($my_array as $k => $v) {
        echo '<li id="' . $k . '">' . $v . '</li>';
    }
    echo '</ul>';
    ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll use a foreach instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.