1

I have a page where the logged in user can see a list of different links, but only if the links match an entry in a MySQL database, something like this:

<li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='consulting' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "      
                            <a class='atags' href=\"javascript:showonlyone('consulting', this)\">Consulting</a>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>
                <li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='Projects' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "<li><a class='atags' href=\"javascript:showonlyone('projects', this)\">Projects</a></li>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>

ETC...

However, I just realized that if the $username in my session_start(); has 1 in the moderator field I want to show something different. How can I do this? Theoretically what I am looking for is something like this:

if ($username && $moderator == 1) {
   echo "You can edit this.";
}
else
    echo "<li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='art' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "      
                            <a class='atags' href=\"javascript:showonlyone('art', this)\">Art</a>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>
                <li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='computer' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "<li><a class='atags' href=\"javascript:showonlyone('computer', this)\">Computer</a></li>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>
                <li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='critical reading' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "<li><a class='atags' href=\"javascript:showonlyone('criticalreading', this)\">Critical Reading</a></li>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>
                <li>
                <?php
                        require ("scripts/connect.php");
                        $query = mysql_query("SELECT * FROM files WHERE subject='french' AND active='1'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows > 0) 
                            echo "<li><a class='atags' href=\"javascript:showonlyone('french', this)\">French</a></li>";
                        if ($numrows == 0)
                            echo "";
                ?>
                </li>";

Is this possible? Thanks.

2
  • why do you have so many 'require' statements? You only need them once for 'scripts/connect.php' Commented Jul 6, 2012 at 5:01
  • Fixed. I was just copying a pasting Commented Jul 6, 2012 at 5:02

2 Answers 2

3

You can freely switch back and forth between PHP and HTML code. Sometimes it helps to use the alternate colon-and-end syntax for control-flow statements rather than braces.

<?php if ($username && $moderator == 1): ?>
    You can edit this.
<?php else: ?>
    <li>
        ...
        <?php
            ...
            if ($numrows > 0):
        ?>
        <a class='atags' href="javascript:showonlyone('art', this)">Art</a>
        <?php
            endif;
        ?>
<?php endif ?>

This is a lot easier than using echo because you don't have to escape quotes and HTML reads more naturally.

Sign up to request clarification or add additional context in comments.

1 Comment

Would up vote if I had enough rep. Perfect! EXACTLY what I was looking for! THANKS! Sorry, I have to wait 2 minutes to accept...
1

Rather than including a php tag within the echo statement, handle the database work before the actual print (set up all the variables you are about to output).

This way, you can include any information you need by following this pattern:

// database stuff here

echo '...'

if ($moderator == 1) echo '...'

echo '...'

An even better option is to handle all of this within the MVC pattern. You may find more information here: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Comments

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.