Why infinate loop, why?

Codes here !

Moderators: macek, egami, gesf

User avatar
carelkat
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Sep 21, 2011 9:28 am
Location: Nylstroom, South Africa

Why infinate loop, why?

Postby carelkat » Tue Jul 24, 2012 11:58 am

Hi there, I hope everybody is well.

My name is Carel and I am from South Africa and I could not find a place to introduce myself so I thought I will do it with my first post. Been a member for few months now, but never found it necessary to post, as I got quite a few of my answers from reading other posts.


I am doing a PHP course from the internet and ran into a little problem. I get what I assume is an infinite loop if I hit my "Edit Page" link.. It just shows the heading and background and then keeps on waiting for the local host.

I include code for the "content.php", "edit_page.php" and my "form_funtions.php" pages. If you need more, please let me know.

Not included is the code for "connections.php" and "functions.php" as it gives no errors on the other pages.

What I can also mention is that if I test the page "edit_page.php" in my browser directly from my code editor it goes to "content.php", like it says on line 6.

Thank you so long in advance for you trouble looking at my code.

Carelkat

Here is the code for content.php

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
    <tr>
        <td id="navigation">
        <?php echo navigation($sel_subject, $sel_page); ?>
            <br />
            <a href="new_subject.php">+ Add a new subject</a>
        </td>
        <td id="page">
        <?php if (!is_null($sel_subject)) { //subject selected} ?>
            <h2><?php echo $sel_subject['menu_name']; ?></h2>
    <?php } elseif (!is_null($sel_page)) { //page selected} ?>
             <h2><?php echo $sel_page['menu_name']; ?></h2>
             <div class="page-content">
                <?php echo $sel_page['content']; ?>
                </div>
                <br />
            <a href="edit_page.php?page=<?php echo urlencode($sel_page['id']); ?>">Edit page</a>
    <?php } else { //nothing selected ?>
        <h2>Select a subject or a page to edit</h2>         
        <?php } ?>   
        </td>
    </tr>
</table>
  <?php
    require("includes/footer.php"); ?>

and for edit_page.php

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
      //Maak seker die subject id word gestuur as 'n interger
      if (intval($_GET['page']) == 0) {
          redirect_to('content.php');
      }
     
      include_once("includes/form_functions.php");
     
      //BEGIN FORM PROCESSING
      //begin slegs as die form gesubmit is.
      if (isset($_POST['submit'])){
          //begin 'n array om ons foute te hou.
          $errors = array();
      }   
          //begin validations op die form data
          $required_fields = array('menu_name', 'position', 'visible', 'content');
          $errors = array_merge((array)$errors, check_required_fields((array)$required_fields));
         
          $fields_with_lengths = array('menu_name' => 30);
          $errors = array_merge((array)$errors, check_max_field_lengths((array)$fields_with_lengths));
     
          //maak form data skoon voor dit in db ingesit word.
          $id = mysql_prep($_GET['page']);
          $menu_name = trim(mysql_prep($_POST['menu_name']));
          $position = mysql_prep($_POST['position']);
          $visible = mysql_prep($_POST['visible']);
          $content = mysql_prep($_POST['content']);
           
          //word slegs in die databasis ingesit as dit skoon is
          if (empty($errors)){
            $query = "UPDATE pages SET
                        menu_name = '{$menu_name}',
                        position = {$position},
                        visible = {$visible}
                        content = '{$content}'
                      WHERE id = {$id}";
            $result = mysql_query($query);
          //toets om te sien update suksesvol was.
          if (mysql_affected_rows() ==1) {
              //suksesvol 
                $message = "Die bladsy is suksesvol verander.";
          } else {
                $message = "Die bladsy is nie suksesvol verander nie.";
                $message .= "<br />" . mysql_error();  } 
          }    else {
              if (count($errors) == 1){
                  $message = "Daar was 1 fout in die form";
              } else {
                  $message = "Daar was" . count($errors) . "in die form.";
              }
          }
          //Einde van form proseseering.
         
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
        <tr>
            <td id="navigation">
                <?php echo navigation($sel_subject, $sel_page); ?>
                <br />
                <a href="new_subject.php">+ Add a new subject</a>
                </td>
            <td id="page">
                <h2>Edit page: <?php echo $sel_page['menu_name']; ?></h2>
                <?php if (!empty($message)) {echo "<p class=\"message\">" . $message .
"</p>";} ?>
                <?php if (!empty($errors)) {display_errors($errors);} ?>
                <form action="edit_page.php?page=<?php echo $sel_page['id']; ?>" method="post">
                    <?php include "page_form.php" ?>
                    <input type="submit" name="submit" value="Update Page" />&nbsp;&bsp;
                    <a href="delete_page.php?page=<?php echo $sel_page['id']; ?>"
                    onclick="return confirm('Are you sure you want to delete this page?');">
Delete page</a>
                </form>
                <br />
                <a href="content.php?page=<?php echo $sel_page['id']; ?>">Cancel</a> <br />
         </td>
      </tr>
</table>

and for form_functions.php

Code: Select all

<?php
function check_required_fields($required_array) {
        $field_errors = array();
        foreach($required_array as $fieldname) {
            if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) &&
    $_POST[$fieldname] = 0)) {
                $field_errors[] = $fieldname;
    }
        }
        return $field_errors;
    } 

function check_max_field_lengths($field_length_array) {
        $field_errors = array();
         foreach($field_length_array as $fieldname => $maxlength) {
             if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    $field_errors[] = $fieldname; }
         }
         return $field_errors;
    }

function display_errors($error_array) {
        echo "<p class=\"errors\">";
        echo "Kyk asseblief na die volgende velde: <br />";
        foreach($error_array as $error) {
            echo " - " . $error . "<br />";
                }
            echo "</p>";
        }
?>

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Why infinate loop, why?

Postby NigelRen » Thu Aug 02, 2012 11:41 pm

One thing - if you where in an infinite loop - you wouldn't get any of your page back. The server would still think it's building the page to be displayed.
So I think the page is coming back - but your probably getting an error on the page. Have a look in your error logs and see if you can see anything in there ( if using LAMP - the Apache error log should contain this information ).

User avatar
carelkat
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Sep 21, 2011 9:28 am
Location: Nylstroom, South Africa

Re: Why infinate loop, why?

Postby carelkat » Sun Oct 07, 2012 7:05 am

Can you believe it, after going back to the project every now and then I found it. It was a small spelling mistake in the word "count" on the form_page.php which I didn't even post.

I came across the fault by carefully reading my error message again, and going to that page and line and noticing I left out a "u" in "$count++".

So whats the lesson to all newbies like myself: READ YOUR ERROR MESSAGES!!!

Thank you to everybody who had a look.

Carel

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Why infinate loop, why?

Postby seandisanti » Thu Oct 11, 2012 9:13 am

NigelRen wrote:One thing - if you where in an infinite loop - you wouldn't get any of your page back. The server would still think it's building the page to be displayed.
So I think the page is coming back - but your probably getting an error on the page. Have a look in your error logs and see if you can see anything in there ( if using LAMP - the Apache error log should contain this information ).

That's not accurate, if you have buffered output on, and output statements to the screen in your infinite loop, statements will be written as the buffer threshold is met and the buffer dumped. This can be demonstrated with the following code. A couple of caveats, this code was someone else's from viewtopic.php?f=2&t=21059 . It is only an infinite loop because it ignores the exit condition, and it's a poor implementation of a while loop. Only reproducing it to show that you can get output from an infinite loop.

Code: Select all

<?php
$i=0;
$j=5;
while($j==5)
{echo $i;
echo ",";
$i=$i+.2;
if($i==2)
{break;}
?>


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests

cron