tags:

views:

571

answers:

3

I am building a menu that I want to display in a certain way. However, it is to be populated from a database so it cannot be hard-coded.

Essentially I want the 'li' tags to appear in set places when they are created and was hoping that I could set their class using php count() and already having the css set up for them.

Basically, the menu will have no more than 12 'li' tags. I want to be able to assign them a css id as they are generated. They cannot have a generic class as they have to be absolutely positioned on the page, therefore I was hoping to use count() to return the number of 'li' tags and and then somehow assign them a unique css id

    <ul id="work_div"> 

    <li id="one"><a href="detail/one">Project 1</a></li>
    <li id="two"><a href="detail/two">Project 2</a></li>
    <li id="three"><a href="detail/three">Project 3</a></li>
    <li id="four"><a href="detail/four">Project 4</a></li>

    </ul>

basically, as the menu is populated I would like to assign the specific id to the li by using count() to work out which one goes where.

I'm thinking that I need to place the database results in to a variable and assign values from there????

Make any sense??

Hopefully!

+2  A: 

Ok, first of all have a look at this. count() is not a function what you are looking for. It's a good idea to store the id label into database and gain them in them script in order to print later.

Basically code will looks like:
EDIT(After reading the comments), elements array can be extended to keep any data type you want, for example another array:

$elements = array ( "one" => array ( "Project 1", "detail/one"), "two" => array( "Project 2", "detail/two"));
echo "<ul id='work_div'>";
foreach( $elements as $id => $key)
{
   list( $name, $href) = $key;
   echo "<li id=$id><a href='$href'>$name</a></li>";
}
echo "</ul>";

Instead of $element you will use the row from database.

Artem Barger
would this work if 'project 1' was just a number?
DanC
Sure, no problem.
Artem Barger
+2  A: 

it is not very clear what do you need count() for. first of all you have to get associated array of items from database in desired order (sort by id, title, etc) you can get something like this:

//result from database
$array = array(array('page_id'=>'1','sef_title'=>'one','title'=>'Project 1'),array('page_id'=>'2','sef_title'=>'two','title'=>'Project 2'),array('page_id'=>'3','sef_title'=>'three','title'=>'Project 3'));

//create counter starting from 1
$i = 1;
$arrayLinks = array();
foreach($array as $link){
$arrayLinks[] = "<li id=\"link{$i}\"><a href=\"detail/{$link['sef_title']}\">{$link['title']}</a></li>";
$i++;
}
//print result in ul
echo '<ul id="work_div">' . implode("", $arrayLinks) . '</ul>';

As result you will receive list with ids 'link1','link2' etc

Nazariy
+1  A: 

Below is one way you can do it, where the project_id is a number the number in your id's.

echo '<ul id="work_div">'
$sql = 'SELECT project_id, project_name, project_link FROM projects';
$dh = mysql_query( $sql );

while ( $result = mysql_fetch_assoc( $dh ) ) {
 echo '<li id="'.$result['project_id'].'"><a href="'.$result['project_link'].'">'.$result['project_id'].'</a></li>';
}
echo '</ul>'

If you do not want to use a project ID, here is another way to do it:

$numbers = array("zero", "one", "two", "three", "four", "five", "six", "seven");
echo '<ul id="work_div">'
$sql = 'SELECT project_name, project_link FROM projects';
$dh = mysql_query( $sql );
$counter = 1;
while ( $result = mysql_fetch_assoc( $dh ) ) {
 echo '<li id="'.$numbers[$counter].'"><a href="'.$result['project_link'].'">'.$result['project_id'].'</a></li>';
 $counter++;
}
echo '</ul>'
Shadi Almosri