Page 1 of 1

php essentials on lynda.com

Posted: Thu Aug 12, 2010 10:19 am
by iamalex
I'm working on the course php essential on lynda.com and on the last item in the crud section and having problems with new_page, page_edit, getting parse error and won't go to edit page.
Should be able to select sub menu ie. 'History' and then select "edit_page" link and go to that page which pulls up a page_form called in page_edit but I'm messed up.

Can someone please take a look and help. So far everything else works fine, just this last section.

Thanks,

Alex

Code: Select all

content.php
<?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']; ?><br />
                <br /><a href="edit_page.php?page=<?php echo $edit_page['id']; ?>">Edit page</a><br />
            </div>
        <?php } else { // nothing selected       ?>
            <h2>Select a subject or page to edit</h2>
        <?php } ?>
        </td>
    </tr>
  
</table>

<?php require("includes/footer.php"); ?>
   
 new_page.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
    // make sure the subject id sent is an integer
    if (intval($_GET['subj']) == 0) {
       redirect_to("content.php"); 
    }
    
    include_once("includes/form_functions.php");
    
    // START FORM PROCESSING
    // only execute the form processing if the form has been submitted
    if (isset($_POST['submit'])) {
        // initialize an array to hold our errors
         $errors = array();
    
    //perform validations on the form data
    $required_fields = array('menu_name', 'position', 'visible', 'content');
    $errors = array_merge($errors, check_required_fields($required_fields));
    
    $fields_with_lengths = array('menu_name' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));
    
    // clean up the form data before putting it in the database
        $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']);
        
    // Database submission only proceeds if there were NO errors.
    if (empty($errors)) {
        $query = "UPDATE pages SET
                menu_name = '{$menu_name}',
                position = {$position},
                visible = {$visible},
                content = '{$content}'
            WHERE id = {$id}";
        $result = mysql_query($query);
        // test to see if the update occurred
            if (mysql_affected_rows()  == 1) {
                // Success
                $message = "The page was successfully updated.";
            } else {
                $message = "The page could not be updated.";
                $message .= "<br />" . mysql_error();
            }
        } else {
            if (count($errors) == 1) {
                $message = "There was 1 error in the form.";
            } else {
            $message = "There were " . count($errors) . " errors in the form.";
            
        }
    }
    // END FORM Processing
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
    <tr>
        
        
        
        
        
    <td id="navigation">
<?php echo navigation($sel_subject, $sel_page, $public = false); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
  </td>
    <td id="page">
        <h2>Adding New Page</h2>
        <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
        <?php if (!empty($errors)) { display_errors($errors); } ?>

        <form action="new_page.php?subj=<?php echo $sel_subject['id']; ?>" method="post">
            <?php $new_page = true; ?>
            <?php include "page_form.php" ?>
            <input type="submit" name="submit" value="Create Page" />
            </form>
           <br />
           <a href="edit_subject.php?subj=<?php echo $sel_subject['id']; ?>">Cancel</a><br />
              </td>
 
        </tr>
        </table>
   <?php include("includes/footer.php"); ?>
   
   
edit_page.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
    // make sure the subject id sent is an integer
    if (intval($_GET['page']) == 0) {
       redirect_to('content.php'); 
    }
    
    include_once("includes/form_functions.php");
    
    // START FORM PROCESSING
    // only execute the form processing if the form has been submitted
    if (isset($_POST['submit'])) {
        // initialize an array to hold our errors
         $errors = array();
  
    //perform validations on the form data
    $required_fields = array('menu_name', 'position', 'visible', 'content');
    $errors = array_merge($errors, check_required_fields($required_fields));
    
    $fields_with_lengths = array('menu_name' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));
    
    // clean up the form data before putting it in the database
        $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']);
        
    // Database submission only proceeds if there were NO errors.
    if (empty($errors)) {
        $query = "UPDATE pages SET
                menu_name = '{$menu_name}',
                position = {$position},
                visible = {$visible},
                content = '{$content}'
            WHERE id = {$id}";
        $result = mysql_query($query);
        // test to see if the update occurred
            if (mysql_affected_rows()  == 1) {
                // Success
                $message = "The page was successfully updated.";
            } else {
                $message = "The page could not be updated.";
                $message .= "<br />" . mysql_error();
            }
        } else {
            if (count($errors) == 1) {
                $message = "There was 1 error in the form.";
            } else {
            $message = "There were " . count($errors) . " errors in the form.";
        }
    }
    // END FORM Processing
    }
?>
<?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;&nbsp;
            <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>
   <?php include("includes/footer.php"); ?>
   
 page_form.php
<?php // this page is included by new_page.php and edit_page.php   ?>
<?php if (!isset($new_page)) {$new_page = false;} ?>

<p>Page name: <input type="text" name="menu_name" value="<?php echo $sel_page['menu_name']; ?>" id="menu_name" /></p>

<p>Position: <select name="position">
<?php
    if (!$new_page) {
        $page_set = get_pages_for_subject($sel_page['subject_id']);
        $page_count = mysql_num_rows($page_set);
    } else {
        $page_set = get_pages_for_subject($sel_subject['id']);
        $page_count = mysql_num_rows($page_set) + 1;  
    }
    for ($count=1; $count <= $page_count; $count++) {
        echo "<option value=\"{$count}\"";
        if ($sel_page['position'] == $count) { echo " selected"; }
        echo ">{$count}</option>";
    }
?>
</select></p>
<p>Visible:
    <input type="radio" name="visible" value="0"<?php
    if ($sel_page['visible'] == 0) { echo " checked"; }
    ?> /> No
    &nbsp;
    <input type="radio" name="visible" value="1"<?php
    if ($sel_page['visible'] == 1) { echo " checked"; }
    ?> /> Yes
</p>
<p>Content:<br />
    <textarea name="content" rows="20" cols="80"><?php echo $sel_page['content']; ?></textarea>
</p>

Re: php essentials on lynda.com

Posted: Thu Aug 12, 2010 11:33 am
by Jsphlane20
Questions...

Is that the exact error message you are receiving ?

Do you have access to the exercise files on 'lynda.com' ? You could compare your code if you are at a lost. It could be potentially difficult for someone to troubleshoot the error without having all the necessary include files. The error can be from the page itself or from other includes.

I will compare your code later today once I have access to the lynda files. Hopefully I would be able to provide assistance.

Re: php essentials on lynda.com

Posted: Thu Aug 12, 2010 12:11 pm
by iamalex
No, I don't have access to files, just video and this section he goes a little fast for me.

my includes seem to be exactly as his. do you need me to post?

Thanks

Re: php essentials on lynda.com

Posted: Thu Aug 12, 2010 1:18 pm
by Jsphlane20
iamalex wrote:No, I don't have access to files, just video and this section he goes a little fast for me.

my includes seem to be exactly as his. do you need me to post?

Thanks

I have access to the same content. When I am at home I will take a look at your code and compare it to the lynda files.

BTW what is the error message that you are receiving ?

Re: php essentials on lynda.com

Posted: Thu Aug 12, 2010 6:09 pm
by Jsphlane20

Code: Select all

<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>
For the php page 'content.php' it seems that you have some items out of place. More specifically take a look at the above code and compare it against your code. This is just the beginning. You may also want to watch the videos once again to pick up on any items missed.

Re: php essentials on lynda.com

Posted: Thu Aug 12, 2010 6:31 pm
by iamalex
This was the error. I moved that line but still something wrong as I open to new subject instead of edit page. Company too cheap to buy with code and some of pages don't show.

If you can help anymore, appreciate.

Getting error on this one"Parse error: parse error, expecting `','' or `';'' in G:\wamp\www\widget_corp\edit_subject.php on line 123"

but have same as what is shown in portion of script showing...


Thanks,

Alex