0

I'm working on my own simple Content Management System to implement on my projects for clients who want to do their own maintenance.

I'm going to contain only page titles and their content in a database table. All I want is to make a list which takes all the page titles and puts them into a <ul>. I believe I should use foreach, but I'm not too good at PHP, so I'd appreciate some help.

So how would I go about this?

Purag
  • 16,941
  • 4
  • 54
  • 75
  • It's a pretty broad question. Do you have the database set up? Do you know how to retrieve the DB-table's content? – Flambino Jul 31 '11 at 00:32
  • @Flambino: I don't currently have a database set up, but I know how to do that. I also know how to retrieve database content using SELECT. – Purag Jul 31 '11 at 02:08
  • In that case, something like Quasdunk's answer below should work fine. I've posted an alternative that assumes you've already retrieved the rows and uses `foreach` instead, but Quasdunk's solution does the same thing, just more compact – Flambino Jul 31 '11 at 02:29

2 Answers2

1
<?php
  $qry = mysql_query("SELECT * FROM links");
?>
<ul>
<?php while($row = mysql_fetch_array($qry)) { ?>
  <li><a href="<?php echo $row['link_url']; ?>"><?php echo $row['link_title']; ?></a></li>
<?php } ?>
</ul>

...in case your table with links has the colums 'link_url' and 'link_title'. But you get the idea, I guess.

Quasdunk
  • 14,944
  • 3
  • 36
  • 45
1

If you have retrieved an array with the table's rows, you can probably do something like this:

The PHP-in-HTML version:

<ul>
    <?php foreach( $pages as $page ): ?>
        <li><a href="<?=$page['url']?>"><?=$page['title']?></a></li>
    <?php endforeach ?>
</ul>

The HTML-in-PHP version:

echo "<ul>";
foreach( $pages as $page ) {
    echo "<li><a href=\"$page['url']\">$page['title']</a></li>";
}
echo "</ul>";

In both versions $pages is an array containing all the rows from the table.

Maybe you can adapt that to your needs?

Flambino
  • 18,507
  • 2
  • 39
  • 58