thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Video 58 , Notice: Undefined variable: opened #193

Open rayka85 opened 7 years ago

rayka85 commented 7 years ago

Hi Thanks for your tutorial, i'm, having problem and i couldn't fix it, i get that error as i mentioned at title, i only know that opened is not defined! please help me if you know what is wrong.

Thanks

rayka85 commented 7 years ago

And i also I should add this that i have same problem at pages, it means data is not loading in that page too.

creptor commented 7 years ago

Please post the code for the page editor and where the php is executing the opened variable

rayka85 commented 7 years ago

Thanks Creptor for replay , Which page do you mean?

creptor commented 7 years ago

The query.php and the pages.php from the admin section.

rayka85 commented 7 years ago

sure

this is for queries.php ` <?php

switch ($page) {

    case 'dashboard':

        break;

    case 'pages':

            if( isset( $_POST['submitted']) == 1){

                $title = mysqli_real_escape_string($dbc, $_POST['title']);
                $label = mysqli_real_escape_string($dbc, $_POST['label']);
                $header = mysqli_real_escape_string($dbc, $_POST['header']);
                $body = mysqli_real_escape_string($dbc, $_POST['body']);

                if (isset($_POST['id']) && $_POST['id'] != '') {
                    $action = 'upated';
                $q = "UPDATE pages SET user = $_POST[user],slug = '$_POST[slug]' , title = '$title' , label = '$label' , header = '$header' , body = '$body' WHERE id = $_GET[id] ";
                }
                else {
                    $action = 'added';
                    $q= "INSERT INTO pages (user, slug, title, label, header, body) VALUES ('$_POST[user]', '$_POST[slug]', '$title', '$label', '$header', '$body')";
                }

                $r=mysqli_query($dbc, $q);

                if($r){
                    $message= '<p>Page was '.$action.'! </p>';
                }
                else
                    {
                        $message= '<p>page could not be '.$action.' because '.mysqli_error($dbc); 
                        $message .= '<p>'.$q.'</p>';
                    }
                 }
            if(isset($_GET['id'])){$opened = data_user($dbc,$_GET['id']);}

        break;

            //FOR USER CASE 
        case 'users':

                    if( isset( $_POST['submitted']) == 1){

                    $first = mysqli_real_escape_string($dbc, $_POST['first']);
                    $last = mysqli_real_escape_string($dbc, $_POST['last']);

                    if($_POST['password'] != ''){

                        $password ="pass = '$_POST[password]',";
                    }

                    if (isset($_POST['id']) && $_POST['id'] != '') {

                        $action = 'upated';

                    $q = "UPDATE users SET first = '$first', last = '$last' , $password  status = $_POST[status]  WHERE id = $_GET[id] ";
                    }
                    else {
                        $action = 'added';

                        $q= "INSERT INTO users (first, last, pass, status) VALUES ('$first', '$last', '$_POST[password]', $_POST[status])";
                    }

                    $r=mysqli_query($dbc, $q);

                    if($r){
                        $message= '<p>user was '.$action.'! </p>';
                    }
                    else
                        {
                            $message= '<p>user could not be '.$action.' because '.mysqli_error($dbc); 
                            $message .= '<p>'.$q.'</p>';
                        }
                     }

                        if(isset($_GET['id'])){
                                $opened = data_user($dbc,$_GET['id']);  
                    }

        break;

        case 'settings':

        break;

    default:

        break;
}

?> `

and this is for page.php

`

Page Manager

<div class="row">

    <div class="col-md-3">

                <div class="list-group">

                <a  class="list-group-item" href="?page=pages">
                <i class="fa fa-plus-circle" aria-hidden="true"></i>  Add New Page 

                </a> 

        <?php

        $q="SELECT * FROM pages ORDER BY title ASC";

        $r = mysqli_query($dbc, $q);

        while ($list = mysqli_fetch_assoc($r)) {

            $blur = substr(strip_tags($list['body']),0,140);

            ?>

            <a class="list-group-item <?php selected($list['id'],$opened['id'],'active'); ?>" href="index.php?page=pages&?id=<?php echo $list['id']; ?>">
                <h4 class="list-group-item-heading"><?php echo $list['title']; ?></h4> 
                <p class="list-group-item-text"><?php echo $blur; ?></p>
                </a> 

        <?php }
        ?>

        </div>
    </div>

    <div class="col-md-9">

        <?php  if(isset($message)){echo $message; }   ?>

        <!--form and text boxes -->

        <form action="index.php?page=pages&?id= <?php echo $opened['id']; ?>" role="form" method="post">

        <div class="form-group">
            <label for="title" >page title</label>
            <input class="form-control" type="text" name="title" id="title" value="<?php echo $opened['title']; ?>" placeholder="page title" />

        </div>

        <div class="form-group">
            <label for="user" >user</label>
             <select class="form-control"  name="user" id="user" >

                <option value="0">No user</option>
                <?php
                $q = "SELECT id FROM users ORDER BY first ASC";
                $r = mysqli_query($dbc,$q);

                while ( $user_list = mysqli_fetch_assoc($r)) { 
                    $user_data = data_user($dbc,$user_list['id']);

                    ?>
                    <option value="<?php echo $user_data['id'];  ?>" 
                        <?php
                        if(isset($_GET['id'])){
                            //if($user_data['id'] == $opened['user']){echo 'selected'; }

                            selected($user_data['id'], $opened['user'], 'selected');

                        }
                        else{
                            //if($user_data['id'] == $user['id']){echo 'selected'; }
                            selected($user_data['id'], $user['id'], 'selected');
                        }

                          ?> > <?php echo $user_data['fullname'];  ?> </option> 

                <?php } ?>
            </select> 

        </div>

            <div class="form-group">

            <label for="slug" >Slug:</label>

            <input class="form-control" type="text" name="slug" id="slug" value="<?php echo $opened['slug']; ?>" placeholder="page slug" />

            </div>

            <div class="form-group">
            <label for="label" >page label</label>
            <input class="form-control" type="text" name="label" id="label" value="<?php echo $opened['label']; ?>" placeholder="page label" />

            </div>

            <div class="form-group">
            <label for="header" >page header</label>
            <input class="form-control" type="text" name="header"  id="header" value="<?php echo $opened['header']; ?>" placeholder="page heade" />

        </div>

            <div class="form-group">
            <label for="body" >body</label>
            <textarea class="form-control editor" name="body"  id="body" placeholder="page body"  rows="8">

                <?php echo $opened['body']; ?>
                 </textarea>

        </div>  

            <button type="submit" class="btn btn-default"> Save</button>
            <input type="hidden" name="submitted" value="1" />
            <input type="hidden" name="id" value="<?php echo $opened['id'];  ?>">
        </form>

    </div>

</div>`
creptor commented 7 years ago

I found your mistake, the thing is that you're populating the variable $opened with the information received from the users function. You need to create a new one to be able to load page information.

The mistake is on the query.php on the pages option, and it's this line of code:

if(isset($_GET['id'])){$opened = data_user($dbc,$_GET['id']);}
rayka85 commented 7 years ago

i followed the videos step by step until here, as far as i know opened variable is not defined and not recognized by php that's why i see this error "Undefined variable: opened" where i should create another opened variable? out of } ? sorry by that, i really confused by this error, it's taking few days dealing with this error...

creptor commented 7 years ago

Alright, as soon as I can get into my computer I'll send you the code fixed

rayka85 commented 7 years ago

Awsome, Thanks

creptor commented 7 years ago

Chante this (from the pages code from query.php):

if(isset($_GET['id'])){$opened = data_user($dbc,$_GET['id']);}

To this:

if(isset($_GET['id'])){$opened = data_post($dbc,$_GET['id']);}else{$opened=null;}

If that doesn't work then use this full code..

if(isset($_GET['id'])){
    $q="SELECT * FROM pages WHERE id = $_GET['id']";
    $r = mysqli_query($dbc, $q);
    $opened = mysqli_fetch_assoc($r);
}else{
    $opened=null;
}
rayka85 commented 7 years ago

Problem for first code that you gave it to me is that we haven't created data_post function yet(Video 58) and for second one also it doesn't work, it's very weird...

creptor commented 7 years ago

Please post the structure from the table for the pages information (MySQL table format), a picture should be enough.

rayka85 commented 7 years ago

2016-08-06

creptor commented 7 years ago

I have found your mistake....

For this code to work:

if(isset($_GET['id'])){
    $q="SELECT * FROM pages WHERE id = $_GET['id']";
    $r = mysqli_query($dbc, $q);
    $opened = mysqli_fetch_assoc($r);
}else{
    $opened=null;
}

You need a id value right? but in your code every url is like this: http://website/admin?page=pages&?id=1 When it should be this: http://website/admin?page=pages&id=1

So there's an extra ?...

To fix this, you need to change these lines of code:

<a class="list-group-item <?php selected($list['id'],$opened['id'],'active'); ?>" href="index.php?page=pages&?id=<?php echo $list['id']; ?>">
<form action="index.php?page=pages&?id= <?php echo $opened['id']; ?>" role="form" method="post">

and replace them with these:

<a class="list-group-item <?php selected($list['id'],$opened['id'],'active'); ?>" href="index.php?page=pages&id=<?php echo $list['id']; ?>">
<form action="index.php?page=pages&id= <?php echo $opened['id']; ?>" role="form" method="post">

Then the url will be fixed 😋.

The way url are rendered is -> http://domain/page?GET[values], anything extra will be omitted, and if you have multiple GET variables, then they should be joint using only a &

GET variables in the url are called like variable=value (or just variable)

rayka85 commented 7 years ago

creptor it's so funny, i tried those codes but still is same! i dont know i can skip this video tutorial or not? is this relevant to next videos?

creptor commented 7 years ago

Are you sure you removed the ?

creptor commented 7 years ago

The thing is that if you don't correctly send the GET information it wont work

creptor commented 7 years ago

NOTE: you have to click the page in order to see its content. (I don't know what's appening)

rayka85 commented 7 years ago

Yes Creptor i followed your steps and i get this php error!

( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\series\atomcms\admin\config\queries.php on line 41 Call Stack

Time Memory Function Location

1 0.0006 133032 {main}( ) ..\index.php:0 2 0.0010 136456 include( 'C:\wamp\www\series\atomcms\admin\template\header.php' ) ..\index.php:3 3 0.0020 140472 include( 'C:\wamp\www\series\atomcms\admin\config\setup.php' ) ..\header.php:9

creptor commented 7 years ago

Please post your hole query.php and pages.php code in http://pastebin.com to help me inspect it more easily. I'll post the rewritten code here for you :yum:

rayka85 commented 7 years ago

sure

this link is for query: http://pastebin.com/caJzRQXZ and this link is for pages: http://pastebin.com/bTpMG9xm

creptor commented 7 years ago

Wow I know what's the mistake. It seems I didn't put a space between the SELECT and the * on line 41. Please put the space there, and it should work fine :yum:

rayka85 commented 7 years ago

fantastic! Thanks creptor for help , i really appreciate that...