thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

PDO - Adding body formatted to existing function #179

Closed dbashby closed 8 years ago

dbashby commented 8 years ago

Creptor kindly gave me a solution when trying to convert all MySQLi to PDO the other day for the first function in the project #176 and it has helped me with trying to solve other issues however when Alan starts to add to that function with the body formatted etc it starts to fall apart. I cannot seem to add this extra functionality to the existing function without everything falling apart.

The function Creptor gave me

#Page function function data_page($pdo, $id){ if($id != null){ if(is_numeric($id)){ $conn=$pdo->prepare("SELECT * FROM posts WHERE id = ?"); $conn->bindParam(1,$id,PDO::PARAM_INT); }else{ $conn=$pdo->prepare("SELECT * FROM posts WHERE slug = ?"); $conn->bindParam(1,$id,PDO::PARAM_STR); } $conn->execute(); if($conn->rowCount()==1){ $data=$conn->fetchAll(PDO::FETCH_ASSOC); }else{ $data=null; } return $data; } }

The code Alan gives us that slots into the MySQLi function

`$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'];
        }`

Can someone tell me if this additional functionality should just slot into the main function or if it will need some re-writing?

Thanks

creptor commented 8 years ago

Just add it in, I made it that way so you don't need to change anyting in this part.

dbashby commented 8 years ago

Thanks for the response Creptor, I have tried just slotting the code into place but do not get any body being returned

I have tried

`#Page function function data_page($pdo, $id){ if($id != null){ if(is_numeric($id)){ $conn=$pdo->prepare("SELECT * FROM posts WHERE id = ?"); $conn->bindParam(1,$id,PDO::PARAM_INT); }else{ $conn=$pdo->prepare("SELECT * FROM posts WHERE slug = ?"); $conn->bindParam(1,$id,PDO::PARAM_STR); } $conn->execute(); if($conn->rowCount()==1){

                $data=$conn->fetchAll(PDO::FETCH_ASSOC);
                $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'];
                }

            }else{
                $data=null;
            }
            return $data;
        }
    }`

Thinking this was the logical place for that piece of code but nothing returned as otherwise I would need to paste it in twice, once for each query. I also tried it with the return although it would not make sense for it to go there.

creptor commented 8 years ago

ok, so I need a photo of what your posts database looks like... please, then I'll fix the code for you.

dbashby commented 8 years ago

Thanks. Ok, I assume you want the structure of the table.

postsdb

creptor commented 8 years ago

I have found your problem, it should be the fetchAll, because it returns values as arrays... so you should use fetch. I'm sorry I said it was the same.

Code:

function data_page($pdo,$id){
    if(is_numeric($id)){
        $conn=$pdo->prepare('SELECT * FROM posts WHERE id = ?');
        $conn->bindParam(1,$id,PDO::PARAM_INT);
    }else{
        $conn=$pdo->prepare('SELECT * FROM posts WHERE slug = ?');
        $conn->bindParam(1,$id,PDO::PARAM_STR);
    }
    $conn->execute();
    if($conn->rowCount()==1){
        $data=$conn->fetch(PDO::FETCH_ASSOC);
        $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;
    }else{
        return null;
    }
}
dbashby commented 8 years ago

No Problem, once again a big thanks for your help!

Update:

Unfortunately the body formatted does not work from the above. I get the following with the body_formatted code.

bodyformatted

I also get the same results if I just echo out the body.

On a side note it is also echoing out the less than symbol < as the page name now as well.

If I strip the segment of code for the formatting and just take the new page function I only get the first letter of the page in the browser tab and the first letter of the title along with the same letter 'H' being repeated underneath where the body should be. It is picking up the first letter of home as that was the page I was on.

format

If I go back to the original function you wrote I get oldfunc

But cannot slot the body_formatted code in place.

When I go into the debug panel and view page array there is nothing in there at all with the new function.

dbashby commented 8 years ago

I still have not managed to get this code to function. I am still getting the results as shown above. The first piece of code Creptor gave me functioned and gave me the page info but as soon as the text formatting code was added I got the above.

` //Creptors Code from previous post

function data_page($pdo,$id){ if(is_numeric($id)){ $conn=$pdo->prepare('SELECT * FROM posts WHERE id = ?'); $conn->bindParam(1,$id,PDO::PARAM_INT); }else{ $conn=$pdo->prepare('SELECT * FROM posts WHERE slug = ?'); $conn->bindParam(1,$id,PDO::PARAM_STR); } $conn->execute(); if($conn->rowCount()==1){ $data=$conn->fetch(PDO::FETCH_ASSOC); $data['body_nohtml'] = strip_tags($data['body']); if($data['body'] == $data['body_nohtml']){ $data['body_formatted'] = '

' . $data['body'] . '

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

Any suggestions as to finding out why this is not working? Thanks

dbashby commented 8 years ago

I have tested using var_dump($data); and commented where it works and nothing returned

function data_page($pdo, $id){ if($id != null){ if(is_numeric($id)){ $conn=$pdo->prepare("SELECT * FROM posts WHERE id = ?"); $conn->bindParam(1,$id,PDO::PARAM_INT); }else{ $conn=$pdo->prepare("SELECT * FROM posts WHERE slug = ?"); $conn->bindParam(1,$id,PDO::PARAM_STR); } $conn->execute(); if($conn->rowCount()==1){ $data=$conn->fetchAll(PDO::FETCH_ASSOC); var_dump($data); // returning data to this point $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']; } //var_dump($data); // Not returning data }else{ $data=null; } return $data; } }

creptor commented 8 years ago

can you please post the entire page, functions, and the connection. Please use http://pastebin.com/

Sorry for the delay, windows was upgrading :3

dbashby commented 8 years ago

I got delayed as well, first time I have had chance to come back to let you know the issues.

I have pasted functions, the connection and index. I am unsure if you will need other pages but let me know. Thanks again. http://pastebin.com/Ly33QaLu

creptor commented 8 years ago

I'm sorry, I missed the config.php, I need it to check that all variables are correctly placed. please add it to the pastebin

dbashby commented 8 years ago

There is very little in the config

I have added it to pastebin but this is all there is

`<?php error_reporting(0); // Site config require_once 'connection/connect.php'; require 'setup.php';

?>`

creptor commented 8 years ago

.-. then I'll need setup.php.

dbashby commented 8 years ago

http://pastebin.com/6jZFaArU

dbashby commented 8 years ago

After refreshing the site on localhost after using the error_reporting(-1) I have

Undefined variable: data in C:\xampp\htdocs\1mysite\php_inc\setup.php on line 70

Array to string conversion in C:\xampp\htdocs\1mysite\php_inc\setup.php on line 70

Notice: Undefined variable: nav in C:\xampp\htdocs\1mysite\template\navigation.php on line 7

The undefined variables are one thing but the array to string is a little offputting.

Line 70 is

$data.=$row;

from

$page = data_page($pdo, $pageid); foreach($page as $row){ $data.=$row; } return $data;

UPDATE: When I do a var_dump on $data I get Null Do the same with $row and I get the array returned $page also returns an array

dbashby commented 8 years ago

Just tried something with the function above, went from

$page = data_page($pdo, $pageid); foreach($page as $row){ $data.=$row; var_dump($data); } return $data;

To

$page = data_page($pdo, $pageid); foreach($page as $row){ $data=$row; var_dump($data); } return $data;

The array error went away but I am not convinced it is fixed but when I do a var_dump($data); I get an array returned.

dbashby commented 8 years ago

For some reason I also had $nav in the function

`function nav_main($pdo, $pageid, $nav){ $stmt = $pdo->query("SELECT * FROM posts"); $stmt->bindParam(1,$pageid,PDO::PARAM_INT); $stmt->execute(); foreach($stmt as $nav){ ?>

        <li<?php if($pageid == $nav['slug']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['slug']; ?>"><?php echo $nav['label']; ?></a></li>
 <?php }
    }`

Removed it as not needed, also removed from navigation.php and those three errors have gone.

That error_reporting(-1); picked up another error as well, I had not included my connection in setup.php which flagged a function issue as page was not being included along with undeclared $pdo which is the connection as per setup.php issue.

creptor commented 8 years ago

see, error_reporting is very useful (that way no one gets an error). Try to fix them.

For the $data problem you must specify it first, let me explain how can we do this.... first you need to know if any other variable uses $data, as is a common variable to use (we don't want more difficulty fixing the issue). Then, you should really find if that code is useful, because as I see it, is breaking the hole thing.... and it seems that it doesn't belong there.

creptor commented 8 years ago

Another thing.... the $nav error is because you didn't fully understood the foreach loop.

Let me explain..

A foreach loop, gets an array, and divides it into 'standard variables' that don't need to be previously called or created (if they are already created, it will ignore it, working just like a function)

So, how it works? simple, here is a demostration...

//the test array
$array=array('awesome'=>'stuff' , 'declared'=>1 , '3'=>null);

foreach($array as $data){
    echo $data;
    echo '<br/>';
    var_dump($data);
}
/*
Returns:
    error - var $data is not a string<br/>error- var.....
    -
    {array(1){'awesome'=>'stuff'}}{array(1){'declared'=>1}}{array(1){'3'=>NULL}}
*/

//example 2
foreach($array as $data => $value){
    echo $data.'=>'.$value;
    echo '<br/>';
    var_dump($data);
    var_dump($value);
}
/*
Returns:
     awesome=>stuff<br/>declared=>1<br/>3=><br/>
     -
     {string(1){'awesome'}}{string(1){'stuff'}}{string(1){'declared'}}{int(1){'1'}}{int(1){'3'}}{string(1){''}}
*/

If I have an error I'm sorry, but I believe it's correct.

So, in that code you must remove the $nav placed in the request variables inside of the function, to leave it like this: function nav_main($pdo, $pageid){

dbashby commented 8 years ago

As you see I did try to resolve the issues. I am getting there but very very slowly

dbashby commented 8 years ago

because as I see it, is breaking the hole thing.... and it seems that it doesn't belong there.

Although I have not totally followed Alans tutorial as far as the functions from what I remember $data is what he used in his code, hence me using it as I wanted the basic functionality.

I will go through all your suggestions, I have just tested the code from one of my scripts in the link you sent me and it says syntax ok, no errors, just a few pages to go to identify the issue.

Thanks again for all the help Creptor

creptor commented 8 years ago

Nice, if you find out what was that code for, I can rewrite it, and fix it. So try to figure it out, else I'll have no clue on how to handle the information. :+1:

dbashby commented 8 years ago

I remember now why I did use $siteTitle instead of $site-title, from research I was doing it seems a lot of people are saying not to use hyphens in variables and when I enter $site-title say in the tags I get half highlighted as a variable in blue and the rest is in black as it it is only seeing $site

In the end it came down to $siteTitle or $site_title

creptor commented 8 years ago

yea, that's why you should change the id of the table to mach your wanted variable (to write less code) so, for example, the new id for the site-title row, should be siteTitle, that way my code works.

dbashby commented 8 years ago

Well I have a new error that I am just looking into

Using fetch

fetch

FetchAll

fetchall

But again if I strip the formatting part of the function out using fetch I get the page as it should be, obviously without the formatting

The error is pointing at

<?php echo $row["body"]; ?>

on the index.php

creptor commented 8 years ago

alright, I'll need a picture of the posts database (structure), and the page view file, to check for errors :3

dbashby commented 8 years ago

posts1

posts2

creptor commented 8 years ago

what I mean with the view page, was the php file similar to this one: https://github.com/thedigicraft/Atom.CMS/blob/master/views/page.php

dbashby commented 8 years ago

I didn't break the page down in this due to the issues I was having so all the code relating to the front/users side is what you have seen. I was going to break it down once I had everything I have working as that would be the main bit sorted

creptor commented 8 years ago

The error is caused because in the page you're calling for:

<h1><?php echo $row["header"]; ?></h1>
<?php echo $row["body"]; ?>

And in the script you defined the page data as $page = data_page($pdo, $pageid);.... It should be:

<h1><?php echo $page["header"]; ?></h1>
<?php echo $page["body"]; ?>

PD: use fetch, else it won't work. No need for fetchAll. fetchAll does a different kind of approach and it's slower for use if not needed

creptor commented 8 years ago

Also you should really try to make your colors blend for the page, it helps the users and makes them comfortable with it.... for help with that you should visit https://design.google.com/ and https://www.google.com/design/spec/resources/color-palettes.html . (just talking about that search bar :3 )

Of course this is just for reference, and software it's needed, but I have tried this and it looks great. Just a recommendation by the way.

Good work.

dbashby commented 8 years ago

The bannerbox will be white at the end, I am only using the one shade of green but all other areas will be white. The searchbox is just in place until I get everything else done but I will take a look as it may give me ideas further down the road.

Right, I have the page function working now, the p tags are put in automatically.

A BIG THANKS for all of that with this and the other issues.