Pregunta

How could i use a php die function to only stop certain divs from loading? I understand it probably wouldnt be the exact same as die, but how could i stop only specific divs from loading? So i want the learn "col" and "col last" not to load, but for the div id bar to load. Also, how to i make it so the H2 tag in the if statement does not apply to the table in the else statement?

<html>
<body>



<div id="header">
<div id="site">
    <h1><a href="#">site</a></h1>
    </div>

   <div id="menuholder">
    <ul id="menu">
        <li><a href="index.html">Home</a></li>
        <li class="active"><a href="about.php">about application</a></li>
        <li><a href="#">register</a></li>
        <li><a href="#">contact</a></li>
    </ul>
    </div>


</div>

<div id="teaser">
    <div class="wrap">
        <div id="image"></div>
        <div class="box">

<?php
session_start ();
if (isset($_SESSION['username']))
echo "<h2>Welcome, ".$_SESSION['username']."!</h2>";
else
die("You must be logged in to view this page. 
Register to gain access to learn more about the application.    
<form action='login.php' method='POST'>
<table>
<tr><td>Username: </td><td><input type='text' name='username'/></td><td>Password: </td> <td>              <input type='password' name='password'/></td> <td><input type='submit'  name='submit' value='Login'/></td></tr>
</table>
</form>
");

?>


            <p>Information about the application would go here</p>

        </div>

    </div>
</div>

<div id="bar">
    <div class="wrap">

    </div>
</div>



<div class="wrap">
    <div class="col">
        <h3>Learn <span class="red">More</span></h3>
        <p>More info </p>
    </div>
    <div class="col">
        <h3>User <span class="red">Statistics</span></h3>
        <p>More info</p>
    </div>
    <div class="col last">
        <h3>About <span class="red">Phapsy</span></h3>
        <p>More info</p>

    </div>
</div>

<div id="footer">
    <p class="right">Contact: </p>
</div>

¿Fue útil?

Solución

You can't. die() indicates to PHP to stop processing any further.

This, however, would work:

<?php
session_start ();
if (isset($_SESSION['username'])) {
    echo "<h2>Welcome, ".$_SESSION['username']."!</h2>";
    $auth = true;
} else {
    $auth = false; // Show the following HTML
?>
You must be logged in to view this page. 
Register to gain access to learn more about the application.    
<form action='login.php' method='POST'>
<table>
<tr><td>Username: </td><td><input type='text' name='username'/></td><td>Password: </td> <td>              <input type='password' name='password'/></td> <td><input type='submit'  name='submit' value='Login'/></td></tr>
</table>
</form>
<?php } // End ?>

And then elsewhere...

<?php if ( $auth == true ) { // Display this HTML only if authenticated ?>
    <div class="col last">
        <h3>About <span class="red">Phapsy</span></h3>
        <p>More info</p>

    </div>
<?php } // End ?>

Otros consejos

In the else part just display whatever text you want to display using echo() and exit. You don't need to die().

Use an if block:

<?php if($stuff): ?>
    <div></div>
<?php endif; ?>

"die" is a full stop; there are ways to intercept it, but not convenient ways. In a slightly different situation, "return" might do what you need (since it exits only the current function or file).

"So i want the learn "col" and "col last" not to load". Highlight those lines with your mouse. Hit the delete key :)

If you don't want them to display, use an if else statement. It will look at the if part first, if it's true, it's executes the code, if it's false, it will move to the else statement and try that.

Please don't die! Don't abandon hope -- there's always an alternative.

Try outputting your markup from within conditional statements.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top