PHP link not working - cannot find the mistake - php

I built a CMS system using CKEditor and KCFinder that store information od a databse via textarea/php. So far so good!
The issue comes to when I want to store and display images that link to themselves. The way I am storing images is exactly the same: There is a textarea where I insert an image via KCFinder/CKEditor. The image is uploaded to the server and the path stored at the database. Later I try to pick up that path from the database to display the image (that part also works) and because I want the image to link to itself, I try to use the same method to insert the url on the link. Problem? The link is missing.
Can anyone point me the error and suggest any solution? I would be so thankful!
Code:
<?php
$sql = "SELECT * FROM php_maskiner ORDER BY timestamp DESC";
$result = mysql_query($sql) or print ('<div class="alert alert-standard fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<strong>Can't read the database!</strong>
</div>' . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$images = html_entity_decode($row['images']);
$img_url = $row['images'];
$img_pack = '<div class="mask3 span3">
<a rel="prettyPhoto" href="' . $img_url . '">' . $images . '</a>
</div>';
?>
<article class="span12 post">
<?php echo $img_pack; ?>
<div class="inside">
<div class="span8 entry-content">
<div class="span12">
<h2><?php echo $title; ?></h2>
<p><?php echo $entry; ?></p>
</div>
</div>
</div>
</article>
<?php
}
?>
UPDATE:
I think that this might be a problem caused by CKEditor. In the database the image path is store as: . This is in my understanding what is being outputted. How do I do to output only "/nysida/admin/kcfinder/upload/images/1307594_10243178.jpg"?

Your first mistake is still using the mysql_ extensions to access your databases. You must use PDO (if available):
Example (for mysql):
try {
$DBH = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'user', 'password');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $DBH->prepare('SELECT * FROM php_maskiner ORDER BY timestamp DESC');
$STH->execute();
$STH->setFetchMode(PDO::FETCH_OBJ);
while($row = $STH->fetch()) {
$title = $row->title;
$entry = $row->entry;
$images = $row->images;
$img_url = $row->images;
$img_pack =
'<div class="mask3 span3">
<a rel="prettyPhoto" href="'.$img_url.'"><img src="'.$images.'"></a>
</div>';
}
$DBH = null;
} catch (PDOException $e) {
echo '<div class="alert alert-standard fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<strong>Can\'t read the database!</strong>
</div><br />'.$e;
}
And later in your code:
<?php echo
'<article class="span12 post">
'.$img_pack.'
<div class="inside">
<div class="span8 entry-content">
<div class="span12">
<h2>'.$title.'</h2>
<p>'.$entry.'</p>
</div>
</div>
</div>
</article>';
?>
And regarding the original question: make sure $img_url is not null.

Related

Displaying data from while loop into html code

I need tips or direction on how can I display data from mysql using echo. But I want to display it in html code. I want to display $row["title"] of first title in mysql instead title1 and $row["content"] of first content in mysql instead content1 and do that for all 3 divs. php code works fine I just can't figure out how to make that possible.
<div class="carousel-inner" style="background-size:cover;">
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title1</h2>
<p>content1</p>
</div>
</div>
<div class="item">
<img src="img/road2.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title2</h2>
<p>content2</p>
</div>
</div>
<div class="item">
<img src="img/road3.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title3</h2>
<p>content3</p>
</div>
</div>-->
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "0 results";
}
?>
You're almost there. Just move the html into the echo of the while loop.
echo '<div class="carousel-inner" style="background-size:cover;">';
$counter = 1;
while($row = mysqli_fetch_assoc($query)) {
echo '
<div class="item ' . ($counter == 1 ? 'active' : '') . '">
<img src="img/road{$counter}.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>' . $row["title"] . '</h2>
<p>' . $row["content"] . '</p>
</div>
</div>';
$counter++;
}
echo '</div>';
The only issue is the image, realistically you'd save the image in the database with the title and content then use the same method but for this simple case lets just use a counter
please note that I change your entire code a little bit to make the desired results...
<div class="carousel-inner" style="background-size:cover;">
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) { ?>
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2><?php echo $row["title"]; ?></h2>
<p><?php echo $row["content"]; ?></p>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
?>
Also note that I'm repeating just the first image... You need an extra on planning to determine how to handle images in code and then update this one.

NO error but not working while retrieving rows from mysql database through php

This is php code for retrieving query from mysql database. This has no error as far as I know. Unfortunately this is not working. I cannot find the mistake.
<?php
if(isset($_POST['submit'])) // submit is submit button name as POST method
{
$username = $_POST['in'];
$Timein= date("Y-m-d H:i:s",strtotime(str_replace('/','-',$username))); // converting into mysql datetime format
$username = $_POST['out'];
$Timeout = date("Y-m-d H:i:s",strtotime(str_replace('/','-',$username)));
$dbc = mysqli_connect('localhost','dev','dev#123','Database') or die('Error while connecting Database'.mysql_connect_error);
// connection to database
$q="select * from camera where NOT chkin > '$Timein' AND NOT chkout < '$Timeout'";
$r = mysqli_query($dbc,$q);
// query
while ($data = mysqli_fetch_assoc($r)){ ?>
// printing a bootstrap grid
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 col-xxs-12">
<div class="tm-tours-box-2">
<img src="img/index-0 <?php echo $data['id'];?>.jpg" alt="image" class="img-responsive">
<div class="tm-tours-box-2-info">
<h3 class="margin-bottom-15"><?php echo $data['name']; ?></h3>
<p>Rs <?php echo $data['price']; ?>/day</p>
</div>
<a class="tm-tours-box-2-link" onclick="POP()">Book Now</a>
</div>
</div>
<?php
}
}
?>

Div tag around php code

I'm trying to configure my database data that is being pulled with php to look like my homepage.
I can't figure out what I need to do.
This is for a project for school, and I have very limited knowledge on php.
Here is my php script.
<?php
include('mysql_connect.php');
$dbh = con();
foreach($dbh->query("SELECT * FROM `Product` WHERE `ProductType` = 'memory' ",PDO::FETCH_ASSOC) as $row){
$Pname = $row['ProductName'];
$description = $row['description'];
$price = $row['ProductPrice'];
$ID = $row['ProductID'];
echo <div class="row">;
echo <div class="col-sm-4 col-lg-4 col-md-4">;
echo <div class="thumbnail">;
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
echo "<div class="caption">";
echo "<h4 class="pull-right">$price</h4>";
echo "<h4>$Pname</h4>";
echo "<p>$description</p>";
echo "<p>$ID</p>";
}
?>
</div>
</div>
</div>
Here is my index that has my desired results.
<div class="row">
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
<div class="caption">
<h4 class="pull-right">$74.99</h4>
<h4>GIGABYTE GA-B75M
</h4>
<p>Micro ATX, LGA 1155, Intel B75 Express Chipset, 2200MHz DDR3 (O.C.), SATA III (6Gb/s), 7.1-CH Audio, Gigabit LAN, USB 3.0 - GA-B75M-D3H</p>
</div>
</div>
</div>
Picture of homepage: http://imgur.com/g72Wrxk
Any help/guidance is greatly appreciated, this is driving me crazy.
Use '.' for string concatinations.
For example:
echo '<h4 class="pull-right">' . $price . '</h4>';
You have to use single quote to echo content in your PHP script like this for instead :
echo '<div class="row">';
The single quotes are used to explain that it's a string to show. You have to do the same for your other echo part.
Or you can escape the HTML part from the PHP and replace by vars inside it like this :
<?php
include('mysql_connect.php');
$dbh = con();
echo '<div class="row">';
foreach($dbh->query("SELECT * FROM `Product` WHERE `ProductType` = 'memory' ",PDO::FETCH_ASSOC) as $row){
$Pname = $row['ProductName'];
$description = $row['description'];
$price = $row['ProductPrice'];
$ID = $row['ProductID'];
?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
<div class="caption">
<h4 class="pull-right"><?php echo $price; ?></h4>
<h4><?php echo $Pname; ?></h4>
<p>$description</p>
<p>$ID</p>
</div>
</div>
</div>
<?php
}
?>
</div>
Also you have to be careful about the row system of bootstrap to echo col-md-4 inside a row, you can add 3 items like this per row. So you have to close and reopen a row each time you fetch 3 elements to get a correct HTML structure.
You didn't supplied any information about your database, yet the first problem that's visible is your echo's
To output HTML through PHP you have to output pure strings.
An example for that would be ""
In PHP when you want to connect different kinds of inputs like variables and "" or '' etc you connect them with a dot.
For example:
$string = ' hello3';
echo "Hello 1 ".' hello2 '.$string;
The outcome will be:
Hello 1 hello2 hello3
I hope you got it the hang of it.

Some HTML tags disappear when i use PHP

I'm trying to generate dynamic contents fill from mysql db
here is my php code:
<?php
include 'header.php';
error_reporting(1);
$user = "root";
$pass = "";
$dbName = "haimi";
mysql_connect('localhost', $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
$sql = "SELECT * FROM Projects ";
$result = mysql_query($sql);
?>
<?php
while ($line = mysql_fetch_array($result)) {
?>
<li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php
}
?>
The li displays correctly, but the following does not:
<div class="row projects m0">
<?php
while ($line = mysql_fetch_array($result)) { ?>
<div class="project mix catHouses">
<div class="tint"></div>
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-
lightbox="project" data-title="Central Hospital (building)">
<img src="images/projects/".<?php echo
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"
class="projectImg"> </a>
<div class="projectDetails row m0">
<div class="fleft nameType">
<div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
<div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
</div>
<div class="fright projectIcons btn-group" role="group">
<a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital (building)" class="btn btn-default">
<i class="fa fa-link"></i></a>
<i class="fa fa- search"></i>
</div>
</div>
</div>
<?php
}
?>
</div>
It data in the divs doesn't appear.
You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:
//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );
The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.
Important Note
The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).
You have some HTML mistakes here:
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>
First, there is no need to use . operator (as you are in HTML, not PHP),
Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:
<a
href="images/projects/<?php echo $line['ProjectImage1']; ?>"
data-lightbox="project"
data-title="Central Hospital (building)"
>
<img
src="images/projects/<?php echo $line['ProjectImage1']; ?>"
alt="<?php echo $line['ProjectTitle']; ?>"
class="projectImg"
>
</a>
You will get older fast writing code like that ;)
How about this:
while ($line = mysql_fetch_array($result)) {
$category = $line['Category'];
echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}
Like what Mr #matthew said
I was making a single call, and I was trying to loop through it twice.
The problem solved with this code before the while loop:
$result = mysql_query($sql);

Using a link to connect two pages in database

I have a page with a search bar that gets images of people in alphabetical order from a database. I also have a page with Next and Previous buttons that allows the user to browse through the database of images using Next and Previous buttons. I'm trying figure out a way to make an image a link so that the user can search through images, click the image, and it takes them to the same image on the Next and Previous page.
This is my code that allows me to search and returns, lastname, firstname, and a picture:
<div class="clearfix"></div>
<?php
if (isset($_GET['LastName'])) {
$ln = $_GET['LastName'];
}
include 'connection.php';
$query = "SELECT * FROM residents WHERE LastName like '$ln%' ";
$result = mysql_query($query);
while($person = mysql_fetch_array($result)) { ?>
<div class="media col-sm-4">
<a class="pull-left" href="Browse.php">
<img class="media-object" src="upload/<?php echo $person['Picture'];?>" width="100"
height="100"/>
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $person['LastName'] . ", " .
$person['FirstName']; ?></h4>
</div>
The page I'm trying to connect to is "Browse.php" but as you browse through the images the URL changes by increasing..."Browse.php?page=1"..."Browse.php?page=2" and so on. Is there an easy way to connect an image with the corresponding Browse.php page? I've tried several things and any help would be much appreciated!
If you have id column in residents table. You can do something like this.
<div class="clearfix"></div>
<?php
if (isset($_GET['page'])) {
$page_id = $_GET['page'];
}
include 'connection.php';
$query = "SELECT * FROM residents WHERE ID = $page_id";
$result = mysql_query($query);
while($person = mysql_fetch_array($result)) { ?>
<div class="media col-sm-4">
<a class="pull-left" href="Browse.php?page=<?php echo $page_id; ?>">
<img class="media-object" src="upload/<?php echo $person['Picture'];?>" width="100" height="100"/>
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $person['LastName'] . ", " . $person['FirstName']; ?></h4>
</div>
</div>
<?php } ?>

Categories