简体   繁体   中英

PHP code inside of an echo statement

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM