thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Part 30-31: Username not displaying #73

Closed Chriswilldo closed 9 years ago

Chriswilldo commented 9 years ago

Hello, currently my issue is with the $data = mysqli_fetch_assoc($r); For some reason mysqli_fetch_assoc($r); always seems to mess up on every time as the same parameter 1 Boolean error that has happened to me in the past. Also too, my return $data will only work before $data['fullname'], otherwise it'll cause even more errors. Any suggestions?

data.php:

<?php 

function data_setting_value($dbc, $id) {

$q = "SELECT * FROM settings WHERE id = '$id'";
$r = mysqli_query($dbc, $q);

$data = mysqli_fetch_assoc($r);
return $data['value'];  

}

function data_user($dbc, $id) {

$q = "SELECT * FROM users WHERE email = '$id'";
$r = mysqli_query($dbc, $q);

$data['fullname'] = $data['first'].' '.$data['last']; return $data;
$data['fullname_reversed'] = $data['last'].', '.$data['first'];

}

function data_page($dbc, $id) {

$q = "SELECT * FROM pages WHERE id = $id";
$r = mysqli_query($dbc, $q); 
$data = mysqli_fetch_assoc($r); 

$data['body_nohtml'] = strip_tags($data['body']);

if($data['body'] == $data['body_nohtml']) 

{

$data['body_formatted'] = '<p>'.$data['body'].'</p>'; 

} else { $data['body_formatted'] = $data['body']; }

return $data;

}

?>

setup.php:

<?php 
// SETUP file:

#Database Connection
include('../config/connection.php');

#ConstantS:
DEFINE('D_TEMPLATE', 'template');

#functions:
include ('functions/data.php');
include ('functions/template.php');

#Site setup:
$debug = data_setting_value($dbc, 'debug-status');

$site_title = 'CMS #3';

if(isset($_GET['page'])) {
$pageid = $_GET['page']; } else { $pageid = 1; }

 #page setup:
$page = data_page($dbc, $pageid); 

# User setup:
$user = data_user($dbc, $_SESSION['username']); ?>

navigation.php:

<nav class="navbar navbar-default" role="navigation">

        <ul class="nav navbar-nav">

            <?php //nav_main($dbc, $pageid); ?> 

            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Pages</a></li>
            <li><a href="#">Users</a></li>
            <li><a href="#">Settings</a></li>

        </ul>

<div class="pull-right">

<ul class=" nav navbar-nav">

    <li>    

        <?php if($debug == 1) { ?>  
    <button type="button" id="btn-debug" class="btn btn-warning navbar-btn"><i class="fa fa-bug"; ><br> D-BUG</i></button>
        <?php } ?>
    </li>

<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo $user['fullname']; ?><b class="caret"></b></a> 
<ul class="dropdown-menu">

<li><a href="logout.php">Logout</a></li>    

</ul>

</li>           

</ul>   

</div>  
</nav>  

index.php:

<?php 

# Start session
session_start();

if(!isset($_SESSION['username'])) {
header('Location: login.php');  

}

?>

<?php include('config/setup.php'); ?>

<!DOCTYPE html>
<html>

<head>
<title> <?php echo $page['title'].' | '.$site_title; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<?php include('config/css.php'); ?>
<?php include('config/js.php'); ?>

</head>

    <body>

<div id="wrap">

<?php include(D_TEMPLATE.'/navigation.php'); ?>

<h1>Admin's Dashboard</h1>

</div><!-- end wrap -->

<?php include(D_TEMPLATE.'/footer.php'); ?>

<?php if($debug == 1) { include('widgets/debug.php'); } ?>

</body>

</html>
thedigicraft commented 9 years ago

Okay I am looking at this and there are a couple issues to look at...

Your Code:


function data_user($dbc, $id) {

$q = "SELECT * FROM users WHERE email = '$id'";
$r = mysqli_query($dbc, $q);

$data['fullname'] = $data['first'].' '.$data['last']; return $data;
$data['fullname_reversed'] = $data['last'].', '.$data['first'];

}

Should be:


function data_user($dbc, $id) {

$q = "SELECT * FROM users WHERE email = '$id'";
$r = mysqli_query($dbc, $q);

$data = mysqli_fetch_assoc($r); // put this back in...

$data['fullname'] = $data['first'].' '.$data['last'];
$data['fullname_reversed'] = $data['last'].', '.$data['first'];

return $data; // move this here...

}
Chriswilldo commented 9 years ago

Also, I tried $return $data and return $id a few extra times on data.php to try to fix it. It worked, except for the fact that I can't use <?php echo $user['fullname']; ?> in navigation.php or else it'll count it as an illegal string. Here's the edited data.php and navigation.php

data.php

<?php 

function data_setting_value($dbc, $id) {

$q = "SELECT * FROM settings WHERE id = '$id'";
$r = mysqli_query($dbc, $q);

$data = mysqli_fetch_assoc($r);
return $data['value'];  

}

function data_user($dbc, $id) {

return $id; 
$q = "SELECT * FROM users WHERE email = '$id'"; return $data;
$r = mysqli_query($dbc, $q); return $data;

$data['fullname'] = $data['first'].' '.$data['last']; return $data;
$data['fullname_reverse'] = $data['last'].', '.$data['first'];

}

function data_page($dbc, $id) {

$q = "SELECT * FROM pages WHERE id = $id";
$r = mysqli_query($dbc, $q); 
$data = mysqli_fetch_assoc($r); 

$data['body_nohtml'] = strip_tags($data['body']);

if($data['body'] == $data['body_nohtml']) 

{

$data['body_formatted'] = '<p>'.$data['body'].'</p>'; 

} else { $data['body_formatted'] = $data['body']; }

return $data;

}

?>

navigation.php

<nav class="navbar navbar-default" role="navigation">

        <ul class="nav navbar-nav">

            <?php //nav_main($dbc, $pageid); ?> 

            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Pages</a></li>
            <li><a href="#">Users</a></li>
            <li><a href="#">Settings</a></li>

        </ul>

<div class="pull-right">

<ul class=" nav navbar-nav">

    <li>    

        <?php if($debug == 1) { ?>  
    <button type="button" id="btn-debug" class="btn btn-warning navbar-btn"><i class="fa fa-bug"; ><br> D-BUG</i></button>
        <?php } ?>
    </li>

<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php  ?><b class="caret"></b></a> 
<ul class="dropdown-menu">

<li><a href="logout.php">Logout</a></li>    

</ul>

</li>           

</ul>   

</div>  
</nav>  
thedigicraft commented 9 years ago

the return must be the last thing you do in the function. You can only return one variable/array. So return $data; must be the only return used and you want it as the last statement in the function.

You need to take it off of the line that has:

$data['fullname'] = $data['first'].' '.$data['last']; return $data;

should be:

$data['fullname'] = $data['first'].' '.$data['last'];

otherwise it will return $data before $data['fullname_reverse'] has been created and you do not want that.

Chriswilldo commented 9 years ago

ok so when everything is in its right spot, I still get the same errors. Both the indefined index for $data ['fullname'] and the parameter 1 is supposed to be mysqli_result error for $data = mysqli_fetch_assoc($r);

Chriswilldo commented 9 years ago

Also, return $ id; will work after $r = mysqli_query($dbc, $q); even if return $data is put in last, but it'll say or make <?php echo $user['fullname'] ?> an illegal string offset

thedigicraft commented 9 years ago

you need to take the return $id out completely. i may be wrong... it may actually be returning the first return instead of the last. try taking that out.

Chriswilldo commented 9 years ago

When I do I get the same error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\website\dynamic......\admin\functions\data.php on line 18:

($data = mysqli_fetch_assoc($r); )

Notice: Undefined index: last in C:\xampp\htdocs\website\dynamic......\admin\functions\data.php on line 21

( $data['fullname_reverse'] = $data['last'].', '.$data['first']; )

Chriswilldo commented 9 years ago

Got it to work! I just had to start the admin folder all over!

thedigicraft commented 9 years ago

Glad that worked! On Oct 13, 2014 6:49 PM, "Chriswilldo" notifications@github.com wrote:

Got it to work! I just had to start the admin folder all over!

— Reply to this email directly or view it on GitHub https://github.com/thedigicraft/Atom.CMS/issues/73#issuecomment-58966342 .