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 } ?>
Related
I'm trying to echo certain elements from my news table throughout an article. This involves fields such as the author, headline, content and date. At the moment I can only echo the row and any attempt to echo elements in the page itself is either met with small errors or it outputs nothing. I've had a look around and only found people having problems with simply printing out the row database which I can already do.
Basically when I have Author in my article I want to echo author from my database and it will show next to Author. I'm not sure how possible it is as I am yet to find anything on this or I'm overlooking it.
Here is my current PHP file and I've left in the initial part with the article author, date and headline.
<?php
/* Database connection settings */
$host = 'xxxxx';
$user = 'xxxxx';
$pass = 'xxxxx';
$db = 'xxxxx';
$dbconnect=mysqli_connect($host,$user,$pass,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query = mysqli_query($dbconnect, "SELECT * FROM news WHERE news_Id = 1")
or die (mysqli_errr($dbconnect));
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>{$row['headline']}</td>
<td>{$row['content']}</td>
<td>{$row['author']}</td>
<td>{$row['date']}</td>
</tr>\n";
}
?>
<div class="container-fluid bg">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="container">
<div class="row mb-2">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="news-title">
<h2><?php echo $row['headline']; ?></h2>
</div>
<div class="news-cats">
<ul class="list-unstyled list-inline mb-1">
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Author:</small>
</li>
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Posted:</small>
</li>
</ul>
</div>
<hr>
Since your query only returns one row, a while loop is inappropriate for fetching the data, as at the end of the loop, $row will be left as a false value, thus making any attempt to access e.g. $row['headline'] fail. Change your while loop
while ($row = mysqli_fetch_array($query)) {
to a simple assignment:
$row = mysqli_fetch_array($query) or die(mysqli_error($dbconnect));
Note you have a typo earlier in your code,
or die (mysqli_errr($dbconnect));
should be
or die (mysqli_error($dbconnect));
You are just printing out the result, but don't have a label. You can add one like this:
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>Headline: {$row['headline']}</td>
<td>Content: {$row['content']}</td>
<td>Author: {$row['author']}</td>
<td>Date: {$row['date']}</td>
</tr>\n";
}
Here is my code for a gallery on the website that I am making
<h3>Gallery</h3>
<?php
include"includes/connection.php";
$sql = "select * from kategorije";
$res = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($res))
{
?>
<h4 class="text-center"><?php echo ucwords($row['naziv']); ?></h4>
<?php
$id = $row['sifra'];
$sql1 = "select * from rad where kategorija = $id";
$res1 = mysqli_query($con, $sql1);
$n = mysqli_num_rows($res1);
if($n>0)
{
while($row1 = mysqli_fetch_assoc($res1))
{
$pid = $row1['sifra'];
?>
<div class="col-md-2 col_1">
<img src="uploads/<?php echo $row1['datoteka']; ?>" class="img-responsive" alt=""/>
</div>
<?php
}
}else{
?>
<div class="col-md-2 col_1">
<h6>No Images</h6>
</div>
<?php
}
?>
<div class="clearfix"></div>
<?php
}
?>
I need to make that all the photos in gallery have same height and width when they are uploaded, but I don't know how. I tried to insert this code, but it just doesn't work:
echo"<img src='$dir_path$files[$i]'style='width:150px;height:200px;'>
Also I tried in CSS but it won't work either.
Since you just want to display them in another size, you can achieve this by the way you tried, but with the correct syntax. I hope its this line of code where you want the resized image:
<img src="uploads/<?php echo `$row1['datoteka']; ?>" class="img-responsive" height="200" width="150" alt=""/>`
Let me know it it worked for you. It this wasen't the line you want to do it, keep in mind you simply have to follow the syntax:
<img src="http://url.com/pic.png" width="150" height="200">
Thats all. :)
I have problem that I store in DB column (video_id) and the value of it like that store (1,3,4,7) the number mean the id of video in the Video table which store the video title and text and all information about the video but when I wrote code to select all video the result give me one video
<div class="panel-body">
<?
$qu="SELECT video_id FROM `training_questions` WHERE id=$questions_id AND category_id=$training_id";
$query_c= mysqli_query($con, $qu);
while ($row7 = mysqli_fetch_assoc($query_c)) {
$vedoes=$row['video_id'];
$pieces = explode(",", $vedoes);
}
for($i=0;$i<=count($pieces);$i++){
$query = "SELECT * FROM `video` WHERE id IN $pieces[$i] ";
$query_che= mysqli_query($con, $query);
while ($row2 = mysqli_fetch_assoc($query_che)) { ?>
<div class="col-lg-3 col-sm-6">
<div class="thumbnail">
<div class="video-container">
<div class="thumb"> <img src="../upload/<?= $row2['img'] ?>" class="img-responsive img-rounded media-preview" alt=""> <span class="zoom-image"><i class="icon-play3"></i></span> </div>
</div>
<div class="caption">
<h6 class="no-margin">
<a href ="video_details.php?id=<?= $row2['id'] ?>" class="text-default"><?= $row2['title'] ?>
</a>
</h6>
</div>
</div>
</div>
<?
}
} ?>
</div>
How I can fix it?
Remove the
$pieces = explode(",", $vedoes);
its useless
make your for loop condition look like this
for($i=0;$i<=count($vedoes);$i++)
Hope it Helps
I've been coding PHP for 2 weeks (it's not pretty) and I have not been able to find the answer to my question. I want an admin type user to be able to fill a form and post it to a page where base level users can view the content. I've gotten all of this to work like a charm, but my dilemma is to allow the admin user to include an image as well. Maybe I just don't know what to search for.
Here is the php code and the form for the admin user page:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
include_once("connection.php");
if (isset($_SESSION['adminid'])) {
$adminid = $_SESSION['adminid'];
$adminemail = $_SESSION['adminemail'];
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
} else {
header('Location: index.php');
die();
}
$sql = "SELECT adminid, adminemail, adminpassword, adminname FROM admin WHERE adminemail = '$adminemail' LIMIT 1";
$query = mysqli_query($dbcon, $sql);
if ($query) {
$row = mysqli_fetch_row($query);
$adminname = $row[3];
}
?>
and here is the code for the base level user page: (i commented out the image block where I want the admin's image to be shown.
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<!-- <div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div> -->
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>  propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>
All of this works perfectly, I just can't figure out how to have an image display in the same way as the title, deadline, and content. Youtube wont help either, too much outdated php + I haven't been coding long enough to really work things out on my own.
You can save all user images under a folder (let's call /images/user) and record the file name into database.
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$logoname = basename($_FILES["fileToUpload"]["logoname"]; // <-- Make sure your form is ready to submit an file
// Update below as per your need.
$target = 'images/users/' . $logoname;
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
You can then display the image your page
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
$logoname = 'images/user/' . $row['logoname'];
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="<?php echo $logoname; ?>">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div>
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>  propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>
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.