How to echo out 'include_once' inside html [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Let's say I have a file which goes like this:
<?php
if(isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
echo "
<section>
<div class='container'>
<div class='row'>
"include_once 'php/includes/overall/widget.php'"
<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";
}
}
?>
And in this file I want to echo out an include_once command but I don't know the proper way to do this while echoing some html text.. So I get this error message as well:
Parse error: syntax error, unexpected 'include_once' (T_INCLUDE_ONCE), expecting ',' or ';' on line 19
And the line 19 is this:
"include_once 'php/includes/overall/widget.php'"
So how to correct this issue?

When you want to get into PHP context, you have to put <?php ?> tags in your HTML. Here you are using echo to output HTML from PHP, and requiring to do PHP again. If you want the HTML to switch to PHP to execute the include_once, you have to put the tags again. In your code, the include_once is viewed as HTML, which is not correct, hence the error.
But it gets a bit crazy with php that echos HTML, that requires PHP. So why not simply exit the PHP context to output your html, and get back in for the include_once?
Like so:
<?php
if(isset($_GET['cat_id']))
{
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro))
{
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
?>
<section>
<div class='container'>
<div class='row'>
<?php include_once('php/includes/overall/widget.php'); ?>
<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
<?php
}
}
?>

Try the following...
Because you started the echo with " you can't have " inside it, you can only use " again when the echo is ending or when you're connecting it to another string.
As you can see I connected it by using periods like so:
...ss='row'>" . include_once ('php/includes/overall/widget.php'); ."<div clas...
<?php
if(isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
echo "
<section>
<div class='container'>
<div class='row'>".
include_once ('php/includes/overall/widget.php');
."<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";
}
}

echo "
<section>
<div class='container'>
<div class='row'>";
include_once 'php/includes/overall/widget.php';
echo " <div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";

First off, since you want to echo a multiline string, I'd use Heredoc.
You could use fread(). Here's an example of what that would look like.
$str = fread(fopen('php/includes/overall/widget.php', 'a+'), filesize('php/includes/overall/widget.php'));
echo "
ALL YOUR HTML
{$str}
SOME MORE HTML";

Related

How do I filter with dropdown list? PHP and SQL

We are trying to filter based on the tags or the dropdown menu, we are trying to create a blog website that has tags and those tags can be used to filter the posted content on the homepage Tags dropdown menu:. Here should be the content in the page: Here should be the content in the page:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$query = "select * from posts order by date limit 8";
$result = mysqli_query($con,$query,$tagHomemade);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>`
</div>
Set tags in the url like this:
<li>Homemade</button></li>
Then you can get it by $_GET['tag'] in your code. Then put it on the SQL query.
Good to know: It's strongly recommended to use prepared statements and don't use $_GET['tag'] directly in your SQL code. It prevents SQL injection and cares about special characters as well.
Update:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$where = !empty($_GET['tag']) ? "where tag = '" . $_GET['tag'] . "'" : "";
$query = "select * from posts $where order by date limit 8";
$result = mysqli_query($con,$query);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>
</div>

MySQLi query in variable/function (that can be later used to echo content)

So I'm stuck with a problem. I'm creating some kind of "templated" page (no, I can't use Twig or anything like that) in PHP, that will later be incremented with actual content.
My main problem here is to echo links in navbar. I'm using MaterializeCSS, so in order to make the navbar responsive is to write the links twice inside different ULs. I can easily echo it one time, like this:
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$navstructure =
<<<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
$sitename
<ul class="right hide-on-med-and-down">
HTML;
echo $navstructure;
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
}
echo "
</ul>
</div>
</div>
</nav>
</header>";
?>
This works with no errors. The problem is, I need that (according to MaterializeCSS specifications/documentation):
<nav>
<div class="nav-wrapper">
Logo
<i class="material-icons">menu</i>
<ul class="right hide-on-med-and-down">
// links
</ul>
<ul class="side-nav" id="mobile-demo">
// links again
</ul>
</div>
</nav>
So what I wanted to do was simple: Make a navbar.php and have the structure on a HEREDOC variable, and in the variable $navbarlinks I would print all the links I got from the first code I showed you. The problem for me is that putting the links in a variable instead of showing all the results will only print one, and I couldn't find a way to change this.
What should I do to have a $navbarlinks or navbarlinks() that prints all the mySQL results and work everywhere?
You want to just put it in an array for later use. You should also try to separate your PHP from your HTML, this might be a good start but still needs things like HTML escaping. Note in particular the use of alternative syntax and short echo tags.
<?php
$links = [];
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result) {
while($row = $result->fetch_assoc()) {
$links[] = array_map('htmlspecialchars', $row);
}
}
// PHP is finished now, here's the HTML
?>
<?php if (count($links)):?>
<header>
<nav class="<?=$primarycolor?>">
<div class="container">
<div class="nav-wrapper">
<?=$sitename?>
<ul class="right hide-on-med-and-down">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
<ul class="side-nav" id="mobile-demo">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
</div>
</div>
</nav>
</header>
<?php endif?>
no longer practicing php but as far as i can remember html/css elements don't have to be pass to a variable.. you can just end php before the css elements and open php again from your loop
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
<?php=$sitename?>
<ul class="right hide-on-med-and-down">
<?php
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
?>
</ul>
</div>
</div>
</nav>
</header>";
<?php
}
?>
<!-- end -->

Nidified mysqli_fetch_assoc while

I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?
you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>
Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>
seems that use mysqli_data_seek($result, 0); do the trick the right way.

Limiting number of items to display per page

I have files like (for an instance ) being shown as shown in
http://lawcommission.gov.np/site/NepalLawCommissionFinal/NepalLawCommissionFinal/document.php?cat=112&sub_cat=10008
The document.php has following html to display the files:
<section class="three-fourth">
<div class="sort-by">
<h3>Sort by</h3>
<ul class="sort">
<li>Date ascendingdescending</li>
<li>Name ascendingdescending</li>
<li>Category ascendingdescending</li>
</ul>
<ul class="view-type">
<li class="grid-view">grid view</li>
<li class="list-view">list view</li>
</ul>
</div>
<div class="deals clearfix">
<!--deal-->
<?php
$id = $_GET['cat'];
$base_obj = new Base();
$lst = $base_obj->list_doc($id);
$lst->execute();
foreach ($lst as $rec)
{
$sub_head = $rec['Head_Id'];
$get_sub = $base_obj->list_by_id('Head_Id', $sub_head, 'tbl_heading');
$get_sub->execute();
foreach ($get_sub as $h)
{
$heading = $h['Head_Name'];
}
?>
<article class="one-fourth">
<figure><span style="float:left"><img src="images/uploads/london1.jpg" alt="" style="width:100px" /></span><span style="float:left"></span></figure>
<div class="details">
<h1><?=$rec['Doc_Name'];?></h1>
<span class="address"><?=$heading;?></span>
<span class="price">Added On <em><?=substr($rec['Uploaded_Date_Time'],0,10);?></em> </span>
Download
</div>
</article>
<!--//deal-->
<?php
} ?>
<!--bottom navigation-->
<div class="bottom-nav">
<!--back up button-->
Back up
<!--//back up button-->
<!--pager-->
<div class="pager">
<span>First page</span>
<span><</span>
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>></span>
<span>Last page</span>
</div>
<!--//pager-->
</div>
<!--//bottom navigation-->
</div>
</section>
<!--//three-fourth content-->
</div>
<!--//main content-->
</div>
<div class="wrap clearfix">
<!--info boxes--><!--//info boxes-->
</div>
Can you guys please suggest a way to show only limited no. of pdf files in one page?
here is how you need to change your function:
public function list_doc($cat, $page=0) {
$itemsPerPage = 20;
$cat = intval($cat, 10);
$start = intval($page, 10) * $itemsPerPage;
if ($start < 0) $start = 0;
$qry = $this->dbobj->db1->prepare("SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_documents, tbl_cat_head_doc
WHERE tbl_cat_head_doc.Cat_Id = " . $cat ." AND
tbl_cat_head_doc.Doc_Id = tbl_documents.Doc_Id AND
tbl_documents.Status = 'a'
limit " . $start . "," . $itemsPerPage);
return $qry;
}
and in main page call function like this:
$lst = $base_obj->list_doc($id, $_GET['page']);
to prevent next question: read about SQL_CALC_FOUND_ROWS and FOUND_ROWS()

Simplified php code for displaying results

Sorry if this have been asked before but I couldn't find what I wanted and I am not strong in PHP.
Right now I have this code, which is supposed to return result for different levels:
<div class="swiper-slide">
<img src="img/B1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='B1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div>
<div class="swiper-slide">
<img src="img/L1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='L1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div> and so on...
Right now I can only duplicate it in order to fulfil the displaying of result for each individual levels. If let's say the building have 10 levels, is there a way to simplified the coding?
Hope you guys understand. Thanks in advance! =)
Try this:
<?php
$levelArray=array('L1','B1','L2','B2');
foreach ($levelArray as $i=>$level) {
$data='';
$img = "img/".$levelArray[$i];
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$levelArray[$i]'");
while($row = mysqli_fetch_array($result)){
$data .= '<h1>'.$row['categories'].'</h1>
<ul class="shop_listing clearfix">
<li class="float_left">'.$row['name'].'</li>
<li class="float_right">'.$row['unit_number'].'</li>
</ul>';
}
echo '<div class="swiper-slide">
<img src="'.$img.'" alt="" />
<div class="content_container">'.$data.'</div>
</div>'
}
?>

Categories