-2

I got this navigation bar and i dont know how to convert it from HTML to PHP. Can somone help me?

The HTML is

<nav>
<div id="menubar">
    <ul id="nav">
        <li  class="current"><a href="index.php?pg=1">Home</a></li>
        <li><a href="rpg.php?pg=2">RPG</a></li>
        <li><a href="sports.php?pg=3">Sports</a></li>
        <li><a href="strategy.php?pg=4">Strategy</a></li>
        <li><a href="android_ios.php?pg=5">Android/IOS</a></li>
        <li><a href="nice_to_try.php?pg=6">Nice to try</a></li>
    </ul>
</div>
</nav>

I'm just beginner in PHP and im trying to learn the code, i searched it up on google but i couldnt figure out how to apply it to my code. Thank you in advance!

  • I think you don't quite understand what each of them is used for. What exactly are you trying to do? Are you trying to produce the html code server-side? There isn't such a thing as "migrating HTML to PHP". They are two very different things. HTML is a markup language (used to present the content to the user), PHP is a programming language – Mihai Bujanca Apr 16 '16 at 13:55
  • frankly speakin, if you do not wish to manage it from an admin panel, you should leave it as it is. HTML is the fastest to render ;) – Gogol Apr 16 '16 at 16:34

3 Answers3

2

Just use echo to print html tags:

<?php
echo '
<nav>
<div id="menubar">
    <ul id="nav">
        <li  class="current"><a href="index.php?pg=1">Home</a></li>
        <li><a href="rpg.php?pg=2">RPG</a></li>
        <li><a href="sports.php?pg=3">Sports</a></li>
        <li><a href="strategy.php?pg=4">Strategy</a></li>
        <li><a href="android_ios.php?pg=5">Android/IOS</a></li>
        <li><a href="nice_to_try.php?pg=6">Nice to try</a></li>
    </ul>
</div>
</nav>
';
?>

For make a dynamic ul li see this links:

https://stackoverflow.com/a/5581200/4540183

https://stackoverflow.com/a/17638336/4540183

Community
  • 1
  • 1
Pedram
  • 15,766
  • 10
  • 44
  • 73
  • That's too easy, Maybe a little more challeging. I know there are ways to convert it to full PHP and that's the answer i'm looking for. I can't figure out how to do it with my code. Apprieciate your response though, but not the answer i'm looking for. –  Apr 16 '16 at 12:37
  • PHP is only going to print the html as in this answer. Thats what php does, it allows you to template according to preprocessing instructions. – DevDonkey Apr 16 '16 at 15:54
  • You want to `print` a `html` code or you want make `ul li` dynamically with `PHP` ? @RainierLaan – Pedram Apr 16 '16 at 16:06
  • I want to make ul li dynamically with php, but i dont know how thats why i'm here. –  Apr 16 '16 at 16:27
  • Ok, you must to clearify what you want in question, anyway. it's too easy just look at these links that added to answer. @RainierLaan – Pedram Apr 16 '16 at 16:31
1

After pedram's straight-forward answer, now let's get introduced to looping...

<?php 

/**
 * Hold Menu links and labels inside $menuLinks array
 **/
$menuLinks = [
'index.php?pg=' => 'Home',
'rpg.php?pg=' => 'RPG',
'sports.php?pg=' => 'Sports',
'strategy.php?pg=' => 'Strategy',
'android_ios.php?pg=' => 'Android/IOS',
'nice_to_try.php?pg=' => 'Nice to try'
];
/**
 * Introduce $menuLiList for storing <li>...</li> html.
 **/
$menuLiList = null;
/**
 * Introduce $i counter
 **/
$i = 1;
/**
 * inside foreach loop, take each link and label and build html
 **/
foreach($menuLinks as $link => $label){
    /**
     * if $i is 1, add current class to the li, i.e. add current class to the first element.
     **/
    $current = $i == 1 ? 'class="current"' : '';
    /**
     * append html
     **/
    $menuLiList .= '<li  '.$current.'><a href="'.$link.$i.'">'.$label.'</a></li>';
    /**
     * increase counter.
     **/
    $i++;
}
/**
 * Finally, echo what you have in list, plus the outer wrapper.
 **/
echo '<nav>
<div id="menubar">
    <ul id="nav">
        '.$menuLiList.'
    </ul>
</div>
</nav>';

Is it needed? Not really in your case, but this should be a good example how programming languages ease our daily-life problems. Logic may vary greatly depending on you you need. Update your question for a better answer.

Gogol
  • 3,033
  • 4
  • 28
  • 57
0

You don't really convert it... At the end of the day, PHP outputs a HTML file in browser. You can have mixed HTML, Javascript, PHP in your code. The browser can understand all of them. Just use correct tags.

Just use echo. Echo prints out your HTML code.

Will be like

<?php
echo '
<nav>
<div id="menubar">
    <ul id="nav">
        <li  class="current"><a href="index.php?pg=1">Home</a></li>
        <li><a href="rpg.php?pg=2">RPG</a></li>
        <li><a href="sports.php?pg=3">Sports</a></li>
        <li><a href="strategy.php?pg=4">Strategy</a></li>
        <li><a href="android_ios.php?pg=5">Android/IOS</a></li>
        <li><a href="nice_to_try.php?pg=6">Nice to try</a></li>
    </ul>
</div>
</nav>
';
?>

See your code as a quotation.

RockSolid
  • 202
  • 1
  • 3
  • 20