Create a new HTML page and add content using PHP - php

I am trying to create new page (file) using PHP.
I have opened and created the page and I have add it content from MySQL but I couldn't get id variable from the previous page.
My admin panel is like this:
<?php
if(isset($_POST['sayfaekle'])) {
$baslik = $_POST['baslik'];
$text = $_POST['editor1'];
$sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";
$db->exec($sql);
$id=$db->lastInsertId();
echo "basarılı";
include "sayfagonder.php";
}
?>
In here, I get baslik and yazi and insert into database successfully. I also get id successfully.
In other page that creates a new PHP page codes are like this:
<?php
$_GET['id'] = $id;
ini_set('display_errors','0');
error_reporting(0);
$pagename = '../'.$baslik. '.php' ;
$myfile = fopen( $pagename , "w");
$text = ' <?php
include "config.php";
$id = $_GET["id"];
echo $id;
$sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
$sayfasor->execute(array(0));
$sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);
include "header.php";
?>
<div class="content">
<div class="row">
<div class="pageana">
<div class="col-sm-8">
<h1><?php echo $sayfacek["sayfa_baslik"]; ?></h1>
<?php echo $sayfacek["sayfa_text"]; ?>
</div>
</div>
</div>
</div>
</div>
<?php
include "footer.php";
?> ';
fwrite($myfile , $text);
fclose($myfile);
?>
And I get this error:
Notice: Undefined index: id in C:\xampp\htdocs\doktor\deneme56.php on line 5
I can't get the value of id..

You are fetching the value of $id but that value is not persisting. Set the value in a php $_SESSION variable then access it on new page. Something like this -
<?php
session_start();
if(isset($_POST['sayfaekle'])) {
$baslik = $_POST['baslik'];
$text = $_POST['editor1'];
$sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";
$db->exec($sql);
$id=$db->lastInsertId();
$_SESSION['id'] = $id;
echo "basarılı";
include "sayfagonder.php";
}
?>
Now you can access this value on new page like this -
<?php
session_start();
$_GET['id'] = $_SESSION['id'];
ini_set('display_errors','0');
error_reporting(0);
$pagename = '../'.$baslik. '.php' ;
$myfile = fopen( $pagename , "w");
$text = ' <?php
include "config.php";
$id = $_GET["id"];
echo $id;
$sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
$sayfasor->execute(array(0));
$sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);
include "header.php";
?>
<div class="content">
<div class="row">
<div class="pageana">
<div class="col-sm-8">
<h1><?php echo $sayfacek["sayfa_baslik"]; ?></h1>
<?php echo $sayfacek["sayfa_text"]; ?>
</div>
</div></div>
</div>
</div>
<?php
include "footer.php";
?> ';
fwrite($myfile , $text);
fclose($myfile);
?>
Now coming to your code. When you already have value in Session variable you don't need to assign in $_GET variable. Access it directly.

Related

If echo is empty then display nothing

I've got a var that is either empty or not and I want to display nothing when its empty. Code Snippet:
<?php
global $bp;
$user_id = $bp->displayed_user->id;
$user_info2 = get_userdata($user_id);
?>
<div class="meta">
<?php _e('Status','fisa'); ?>: <?php echo fidsa_fpsa_status($user_id); ?>
</div>
If The var is empty it still shows Status: is there someway to change this to not output Status: when the var is empty?
Assign fidsa_fpsa_status($user_id) to a variable, check if the variable is empty - if its not, print the content!
<?php
global $bp;
$user_id = $bp->displayed_user->id;
$user_info2 = get_userdata($user_id);
$fidsa_fpsa_status = fidsa_fpsa_status($user_id);
if (!empty($fidsa_fpsa_status)) {
?>
<div class="meta">
<?php _e('Status','fisa'); ?>: <?php echo $fidsa_fpsa_status; ?>
</div>
<?php
}

Using buttons to loop to the next element from the data fetched from database

I want to have a next button on my Productdetails.php page that would show the next image fetched from the database using while loop. Below is the productdetails.php page shows details of a particular product.
I am using simple php format, How can I go about it? I have come this far at this point.
<div class = "uniqueproduct">
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
$query1 = mysqli_query($conn, "select * from products where PRODUCT_ID = $id");
while ($row1 = mysqli_fetch_array($query1)) {
?>
<div class = "singleproduct">
<?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($row1['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/>'; ?>
</div>
<div class = "productName">
<?php echo $row1['PRODUCT_NAME']; ?>
</div>
<div class = "productDescription">
<?php echo $row1['PRODUCT_DESCRIPTION'];?>
</div>
<div class = "productPrice">
<?php echo $row1['PRODUCT_PRICE']; ?>
</div>
<?php
}
}
?>
<button class = "btnPicture"> ..............</button>
</div>

PHP How to add active class to dynamic menu?

how can i add active class to dynamic menu.I use while for my product list.But i cannot understand how can i add to active class.
<div class="col-md-4">
<h3>Products</h3>
<div class="list-group">
<?php
$db = new connection();
$productsor = mysqli_query($db,"select * from products");
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>
</div>
</div>
You need to check against a previously set _GET parameter and compare it in your while loop
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php $active = (isset($_GET['products_id']) && $productcek['products_id'] == $_GET['products_id']) ? 'active' : null; ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>

How to use require_once() multiple and connect to different database in the same page?

Im trying to connect to different database in the same page and select two times . but it seems not working and i get just one database and select working! this is my code
<?php
require_once ('../connect_upevent.php');
require_once ('../connect_events.php');
$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con,$select_upevent);
$select_events="SELECT img_name , title , LEFT(`content` , 540) as `content`,date, id FROM events";
$result_events=mysqli_query($con,$select_events);
mysqli_close($con);
?>
my while()
<?php while ($row_upevent = mysqli_fetch_array($result_upevent)) { ?>
<li>
<h4><?php echo $row_upevent['title'] ?></h4>
<div class="dt-sc-two-third column first event-index-float">
<img src="images/event/<?php echo $row_upevent['img_name'] ?>" alt="" title="">
<p><?php echo $row_upevent['content'] ?></p>
</div>
<div class="dt-sc-one-third column event-index-float">
<div class="venue">
<h5>location</h5>
<p><?php echo $row_upevent['place'] ?></p>
</div>
<div class="details">
<h5>time</h5>
<p><?php echo $row_upevent['date'] ?></p>
</div>
</div>
</li>
<?php } ?>
my while() #2
<?php while ($row_events = mysqli_fetch_array($result_events)) { ?>
<!--blog starts-->
<div class="dt-sc-one-half column first animate" data-animation="fadeInLeft" data-delay="300">
<article class="blog-entry">
<div class="entry-thumb">
<img src="images/event/<?php echo $row_events['img_name'] ?>" alt="" title="">
</div>
<div class="entry-details">
<div class="entry-title">
<h2><?php echo $row_events['title'] ?></h2>
</div>
<div class="entry-body">
<p><?php echo $row_events['content'].' [ ... ]' ?></p>
</div>
<div class="entry-footer">
read more<span class="fa fa-caret-left"></span>
</div>
</div>
</article>
</div>
<!--blog ends-->
<?php } ?>
so how can i get it work !?
Create two connection with different object
connect_upevent.php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection 1
$con1 = new mysqli($servername, $username, $password);
connect_events.php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection 2
$con2 = new mysqli($servername, $username, $password);
And use these connection like that
<?php
require_once ('../connect_upevent.php');
require_once ('../connect_events.php');
$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con1,$select_upevent);
$select_events="SELECT img_name , title , LEFT(`content` , 540) as `content`,date, id FROM events";
$result_events=mysqli_query($con2,$select_events);
mysqli_close($con1);
mysqli_close($con2);
?>
You are connecting successively and have same names for both database instances. You should rather make different connection objects. Just name them differently.
<?php
require_once ('../connect_upevent.php'); // $con_upevent
require_once ('../connect_events.php'); // $con_events
$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con_upevent, $select_upevent);
$select_events="SELECT img_name , title , LEFT(`content` , 540) as `content`,date, id FROM events";
$result_events=mysqli_query($con_events, $select_events);
mysqli_close($con_upevent);
mysqli_close($con_events);
?>
or connect to one, make query, connect to second and make query
<?php
require_once ('../connect_upevent.php');
require_once ('../connect_events.php');
$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con_upevent, $select_upevent);
$select_events="SELECT img_name , title , LEFT(`content` , 540) as `content`,date, id FROM events";
$result_events=mysqli_query($con_events, $select_events);
mysqli_close($con_upevent);
mysqli_close($con_events);
?>
<?php
// Connect to first DB
require_once ('../connect_upevent.php');
$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con,$select_upevent);
mysqli_close($con);
// Connect to second DB
require_once ('../connect_events.php');
$select_events="SELECT img_name , title , LEFT(`content` , 540) as `content`,date, id FROM events";
$result_events=mysqli_query($con,$select_events);
mysqli_close($con);
?>
I need to see the content of the included files:
connect_upevent.php
connect_events.php
But i guess, the problem is the following:
You work with the same instance $con.
So the first time you declare $con in the file "connect_upevent.php" and then it gets overwritten in the file "connect_events.php"
You should make two instances in the included files. Like:
$con_upevent and $con_events.
Otherwise, the $con variable is overwritten by the "connect_events.php" file

Unable to read GET variable when used in $page_content

I have a file called productFocus.php where I use the GET variable ID: $id = $_GET["id"];
However this page is used in the product.php file in the following manner:
product.php
<?php
$page_content = 'productFocus.php'; // $id = $_GET["id"];
include('master.php');
?>
productFocus.php
<?php
include "db/db.php";
$id = $_GET["id"];
$product = get_product_by_id($id);
?>
<div class="product-focus">
<h3><?php echo $product->name ?></h3>
<img src="/images/products/<?php echo $product->image ?>">
<div id="description">
<h4>Productinformatie</h4>
<p><?php echo $product->description ?></p>
<h4>Partners</h4>
<table>
<?php
foreach($product->partners_obj as $partner) {
?>
<tr>
<td>
<a href=<?php echo $partner->$product_url ?> target="_blank">
<img id="partner" src="/images/partners/<?php echo $partner->image ?>">
</a>
</td>
<td>
<?php $partner->$product_price ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
master.php
//HTML code
...
<?php include($page_content);?>
...
//HTML code
When I browse to productFocus.php?id=324324 I can read the GET variable, but when I browse to product.php?id=324324 I do not have access to the GET variable ID.
Is there an elegant way to solve this issue?
You need to check the GET variable before you call the product details in the prodcts.php file before regular page load.
<?php
if (isset($_GET['id'])) {
// GET PRODUCT DATA
}else{
// LOAD PRODUCTS PAGE
}
?>
As for getting the product details, I would suggest writing the separate call to the database, but if you just need to load page content, you are not calling it properly:
$id = $_GET['id'];
$fileurl = 'products.php?id='.$id;
$pagecontent = file_get_contents('$fileurl');

Categories