Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2004
    Location
    UK
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Exiting a function, not the subsequent HTML

    Hi,

    I have an HTML file with a section of PHP code in it. The PHP function contains an if / else statement. If the first 'if' criteria is met then i want the php code to end, but load the rest of the HTML. The problem I am having at the moment is when using the exit() command, the remaining HTML does not load.

    Is there any way around this? Any help would be greatly appreciated.

    Thanks,
    Paul

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just leave the if brackets empty:
    PHP Code:
    if ($true) {
         
    // do nothing, HTML loads
    } else {
        
    // do your php stuff

    you could do this instead:
    if (!$true) {
    // do your php stuff
    }
    "There is more than one way to do it."

  • #3
    New Coder
    Join Date
    Mar 2004
    Location
    UK
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, thanks for replying so quickly.

    The problem i am facing is that in the second 'if' section (1) the code checks to see whether a persons birthdate matches the date stored in the database (the first if statement checks to see whether the header contains the word continue). If this criteria is matched then this section echo's a form to the user to check the time of birth.

    If the time of birth is before a set time then the user is sent to a different php page because a different calculation must be done.

    If it is after the set time then the word 'continue' is written into the header and the 'if' section (1) is bypassed and the else statement continues to gather the information that is required from the database.

    The error does not occur if the persons birthdate is not the same as one in the database because it will bypass the first if statement.

    The problem occurs when it does the first check to see first of all if the word continue is present and then if the date matches. If the date is the same it displays the new form and runs the exit() command, but this stops the rest of the html from being displayed. If i take out this exit command it continues on and displays both the form and the 'taster'.


    I have included the code below to clarify the explanation.

    PHP Code:
    <?php
                            error_reporting
    (E_ALL);
                            
    $db = @mysql_connect("localhost","test","test");
    if (!
    $db)
    {
    echo (
    "<P>unable to connect to Database</P>");
    exit();
    }

    //select what database to use

    if (! mysql_select_db("test"))
    {
    echo (
    "<P> Unable to locate  data base</P>");
    exit();
    }

    //end db connection


    // get the day, month and year from the header
    if (isset($_GET['day']))
    {
    $day $_GET['day'];
    $month $_GET['month'];
    $year $_GET['year'];
    $fulldate = (''.$day.'-'.$month.'-'.$year.'');
    $testdate = (''.$year.'-'.$month.'-'.$day.'');
    echo (
    '<strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'.$fulldate.'</font>');

    $result mysql_query("SELECT * 
    FROM `signs` 
    WHERE `Date` <= '$year-$month-$day'
    ORDER BY `Date` DESC 
    LIMIT 0 , 1 
    "
    );

    if (!
    $result)
    {
    echo (
    "error with the query");
    }
    else
    {

    // get the sign, date and time from the database

        
    $data mysql_fetch_array($result);
        
        
    $sign $data['Sign'];
        
    $date $data['Date'];
        
    $time $data['Time'];
            
    // if statement to check if birthdate is same as change date
    // random number code between 0 and 3 to select different 'snippets' each time

    srand(time());
    $random = (rand()%3);
    echo 
    $random;
    // random number code end
        
        // query to select the taster from the database.            
    $tasterresult mysql_query("SELECT * 

    FROM `taster` 

    WHERE `taster_typ` LIKE '%$sign%'

    AND `taster_num` LIKE '$random'

    "
    );


    if (!isset(
    $_GET['continue']))
        {
            if (
    $testdate == $date)
            {

     echo (
    '<br><br><strong><font size="3" face="Verdana, Arial, Helvetica, sans-serif">Were you born before or after '.$time.' on '.$day.'-'.$month.'-'.$year.'?<br></font>
            <form name="timecheck" method="GET" action="'
    .$_SERVER['PHP_SELF'].'">
                       <input type="hidden" name="day" value="'
    .$day.'">
                    <input type="hidden" name="month" value="'
    .$month.'">
                    <input type="hidden" name="year" value="'
    .$year.'">
                    <input type="hidden" name="continue" value="continue">                
                    <input type="submit" name="Submit" value="After this time">
                    </form>
                <form name="timecheck2" method="GET" action="tasterextra.php">
                       <input type="hidden" name="day" value="'
    .$day.'">
                    <input type="hidden" name="month" value="'
    .$month.'">
                    <input type="hidden" name="year" value="'
    .$year.'">
                    <input type="hidden" name="cdate" value="'
    .$date.'">            
                    <input type="submit" name="Submit" value="Before this time">
                    </form>'
    );
                    
                    exit();
            }
        
        }
        

    //if (!$tasterresult)
    //{
    //echo ("error with the query");
    //}
    else
    {
        
    $tasterdata mysql_fetch_array($tasterresult);
        
        
    $taster $tasterdata['taster_text'];
        
    echo (
    '<br><br><strong><font size="3" face="Verdana, Arial, Helvetica, sans-serif"><em>
            '
    .$taster.'</em></font>');

    }


    }
    }
    else
    {
    header ('Location:taster.php');
    }
    ?>
    Thanks once again for any help.

    Paul

  • #4
    New Coder
    Join Date
    May 2003
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    exit() is an alias of die().

    When in a function and you want to stop the function from executing anymore PHP code, use return 0;

    Even if your function isn't supposed to return anything, use return 0; It will exit the function and continue with whatever code is outside of the function, and then the HTML code.
    cpCommerce - Finally a template based open-source e-commerce solution

  • #5
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Could you not use
    PHP Code:
    break; 

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,071
    Thanks
    11
    Thanked 96 Times in 94 Posts
    instead of
    echo $HTML;
    try
    $str = $HTML; (and use $str .= $moreHTML; if required further on)

    remove the exit call;
    then assuming your code gets past the location header ..

    echo $str;
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    New Coder
    Join Date
    Mar 2004
    Location
    UK
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for all the help, i used the $str = html method and the problem is now sorted.

    Paul


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •