Page Goes Blank With Function On Php - php

I have the following function.php:
function getPublishedPosts() {
// use global $conn object in function
global $conn;
$sql = "SELECT * FROM posts WHERE published=true";
$result = mysqli_query($conn, $sql);
// fetch all posts as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
$final_posts = array();
foreach ($posts as $post) {
$post['topic'] = getPostTopic($post['id']);
array_push($final_posts, $post);
}
return $final_posts;
}
and I am calling it to the other page with this:
<!-- THIS LOOP -->
<?php $posts = **getPublishedPosts();** ?>
<div class="content">
<h2 class="content-title">Recent Articles</h2>
<hr>
<!-- more content still to come here ... -->
<!-- Add this ... -->
<?php $posts = getPublishedPosts(); ?>
<?php foreach ($posts as $post): ?>
<div class="post" style="margin-left: 0px;">
<img src="<?php echo BASE_URL . '/static/images/' . $post['image']; ?>" class="post_image" alt="">
<!-- Added this if statement... -->
<?php if (isset($post['topic']['name'])): ?>
<a
href="<?php echo BASE_URL . 'filtered_posts.php?topic=' . $post['topic']['id'] ?>"
class="btn category">
<?php echo $post['topic']['name'] ?>
</a>
<?php endif ?>
<a href="single_post.php?post-slug=<?php echo $post['slug']; ?>">
<div class="post_info">
<h3><?php echo $post['title'] ?></h3>
<div class="info">
<span><?php echo date("F j, Y ", strtotime($post["created_at"])); ?></span>
<span class="read_more">Read more...</span>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</div>
However, when it is on the server, the page goes blank.

I finally solved it, it was just a server extension and version problem regarding the incompatibility of my function.

Related

Div-container around product-cards in php

I'm really new to php and now I got stucked on what I thought was something really easy. Byt I can't see wheres my problem is.
I'm trying to create a webshop page that displays all my products.
To the problem!
Here is my code so far. It displays all products as expected but it closes the main and product-container before all product-cards except from the first one. How to wrap all product-cards in the same div?
$pdo = connect();
$limit = 20;
$offset = 0;
$stmt = get_all_products($pdo, $limit, $offset);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<main>
<section class="product-container">
<?php
foreach($rows as $row) {
?>
<div class="product-card">
<img class="product-image" src="<?php echo $row['Img'];?>" >
<h2 class="title"><?php echo $row['ProductName']; ?></h2>
<span class="price"><?php echo $row['Price'];?></span><span>:-</span>
</div>
</section>
</main>
<?php
}?>
You should move the closing main and section tag after the foreach loop is closed.
<main>
<section class="product-container">
<?php
foreach($rows as $row) {
?>
<div class="product-card">
<img class="product-image" src="<?php echo $row['Img'];?>" >
<h2 class="title"><?php echo $row['ProductName']; ?></h2>
<span class="price"><?php echo $row['Price'];?></span><span>:-</span>
</div>
<?php }?> <!-- Close the foreach loop here -->
</section>
</main>

Trouble closing if statement and while loop in PHP

I'm trying to display some html inside a php "if statement" and during a "While" Loop.
I keep getting unexpected end of file error and I know there are two braces missing but I can't see where to close the while loop properly.
I'm still new to coding so please be gentle... :-)
tried php syntax checker and other similar posts here
<?php include 'includes/header.php';?>
<!-- Navigation -->
<?php include 'includes/navbar.php';?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
}
else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn)) ;
if (mysqli_num_rows($run_query) > 0 ) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p><h2><?php echo $post_title; ?></h2></p>
<p><h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php';?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Try below:
<?php include 'includes/header.php'; ?>
<!-- Navigation -->
<?php include 'includes/navbar.php'; ?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
} else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn));
if (mysqli_num_rows($run_query) > 0) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p>
<h2><?php echo $post_title; ?></h2></p>
<p>
<h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext == "mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>"
alt="900 * 300">
<?php endif; ?>
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php'; ?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php
} // End while
} // End if
Note: your code was missing <?php endif; ?> and two braces at the end.
PHP is syntatically more similar to C than to Python or VisualBasic. Indentation means nothing here. Code blocks need to be explicitly open and closed.
There are 2 types to quote the if-else blocks but you did neither of those.
You can either:
use brackets if { ... } else { ... }
Example:
<?php if ($img_ext=="mp4") { ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php } else { ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php } ?>
use colon and keyworkds if, endif
Example,
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else: ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php endif ?>
Just chose which one to go for.
Similarly, you have to close your while-loop with either bracket } or endwhile, depends of your syntax of choice.
you opened the if statement and forgot to close it also while is not closed closed it after the container gets closed

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

Getting images from DB in MYSQL, PDO and putting into slider foreach

This is my code, actually, it actually retrieves data, but, its looking bad.
I use Jquery, (nivoSlider), and Bootstrap loaded in that order.
I have 2 results on DB, it successfully fetched title, description and other information, but it looks bad formatted, or I cannot make it to display the information correctly.
QUESTION: How can I achieve From this-> http://prntscr.com/5imr6e to this-> http://prntscr.com/5imy35 - With its proper thumbnails ?
Any help will be greatly appreciated. Thank you
$sql = "SELECT * FROM homeslider ORDER by id DESC LIMIT 6";
$query = $handler->prepare($sql);
$query->execute();
$row = $query->fetchAll();
return $row;
}
?>
<?php
$data = getContent();
foreach ($data as $row) {
echo '<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
';
$id = $row['id'];
$titulo = $row['titulo'];
$descripcion = $row['descripcion'];
$link = $row['link'];
$imgurl = $row['imgurl'];
$ultimo_update = $row['ultimo_update'];
$captions = '';
}
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = '<div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->
<?php echo $captions; ?>
</div> <!-- Close slider -->
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
Well the first thing to do is switch this to use a more readable output syntax - breaking in and out of php instead of echoing all the html as complete strings. Additionally i think you want all the images to be slides but im pretty sure you are actually creating a slider for each image...
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php foreach ($data as $row): ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php endforeach; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
We could further optimize this by building a big string of the captions in the loop so that we do not need loop over the array twice:
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php
// initialize captions as an empty string
$captions = '';
?>
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php ob_start(); // start a buffer ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php
// get the buffer contents, append them to $captions
// and then clear the buffer and stop buffering
$captions .= ob_get_clean();
?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php echo $captions; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
It seems that you are not closing the nivoslider div:
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = 'CLOSE HERE: </div><div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->

Pagination in PHP blog

I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.

Categories