I have a page that displays the display of an item with a specific id and I want to display other items that are related based on title, subject, or author. I am having issues with displaying related items. Where I put my query together and execute it, the related results are just the same title of the item whose page I am already on (https://brawlins.com/soarOpen/itemRecord.php?id=65437). How do I displays related results that are not the same as the item whose page I currently am on?
<?php
include("config.php");
if(!isset($_GET["id"])) {
echo "No id passed into the page";
exit();
}
$id = $_GET['id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$item_query = $conn->prepare("SELECT * FROM oer_search WHERE id = :id");
$item_query->bindParam(":id", $id);
$item_query->execute();
while($row = $item_query->fetch(PDO::FETCH_ASSOC)){
$type = $row['type'];
$link = $row['link'];
$title = $row['title'];
$description = $row['description'];
$subject = $row['subject'];
$pub_date = $row["publication_date"];
$source = $row['source'];
$isbn = $row["isbn_number"];
$e_isbn = $row["e_isbn_number"];
$license = $row['license'];
$license_url = $row['license_url'];
$base_url = $row['base_url'];
$author = $row['author'];
$review = $row['review'];
$image = $row["image_url"];
$loc_collection = $row["loc_collection"];
$publisher = $row['publisher'];
$pub_url = $row['publisher_url'];
?>
<title><?php echo $title ?></title>
<div class="container content-container">
<div class="card card-content">
<div class="card-body">
<?php
echo "<strong><a class='itemRecordLink' rel='external' href='$link'><h3>$title</h3></a></strong><br/>";
}
$like_query = $conn->prepare("SELECT title FROM oer_search WHERE title LIKE :title OR subject=:subject OR author=:author LIMIT 4");
$titleLike = $searchTerm = "%". $title . "%";
$like_query->bindParam(":title", $titleLike);
$like_query->bindParam(":subject", $subject);
$like_query->bindParam(":author", $author);
$like_query->execute();
echo "<div class='row'>";
while($row = $like_query->fetch(PDO::FETCH_ASSOC)){
echo "<div class='col-md-3 text-center'>";
if($image != "") {
echo "<img src='$image' class='img-fluid img-thumbnail itemRecordImage' alt='cover image' /><br>";
}
else {
echo "<img src='images/cover-image.png' class='img-fluid itemRecordImage' alt='cover image' /><br>";
}
echo "<span>$title</span>";
echo "</div>";
}
echo "</div>";
?>
</div><!--end of card body-->
</div><!--end of card-->
</div><!--end of container-->
<?php include 'footer.php'?>
</body>
</html>
Related
I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />
I'm making a personal website and I just wanted it to be a bit easier for me to add/edit my posts without manually going into phpmyadmin.
When I go to the edit_post.php page and press update I get an "=" sign next to the pid eg.(foo.php?pid=3), and if i let it redirect to the blog page it doesn't update it.
Blog page
<?php
session_start();
include_once("../IncBlog/db.php");
?>
<?php include "../Includes/navHead.php"; ?>
<title>Adam Brickhill - Life Journal </title>
</head>
<body>
<div class="box">
<div class="header">
<nav class="nav"><p class="title"><a class="postLink" href="../IncBlog/post.php">- Lone Tree -</a></p></nav>
</div>
<!-- JOURNAL !-->
<?php
require_once("../nbbc/nbbc.php");
$bbcode = new BBCode;
$sql = "SELECT * FROM posts ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die(mysqli_error());
$posts = "";
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$id = $row['id'];
$title = $row['title'];
$img = $row['img'];
$content = $row['content'];
$date = $row['date'];
$admin = "<div><a href='../IncBlog/del_post.php?pid$id'>Delete</a> <div><a href='../IncBlog/edit_post.php?pid$id'>Edit</a>";
$output = $bbcode->Parse($content);
ob_start();
include('../IncBlog/blogSkel.php');
$posts .= ob_get_contents();
ob_end_clean();
}
echo $posts;
}
else {
echo "There are no posts to display";
}
?>
<div class="journal">
<div class="catagories">
</div>
<!-- Date !-->
<!-- Picture !-->
<!-- Description !-->
</div>
<?php include "../Includes/navFooter.php"; ?>
<!-- SCRIPTS !-->
<?php include "../Includes/navScriptImport.php"; ?>
</div>
</body>
</html>
Edit Page
<?php
session_start();
include_once("db.php");
if (!isset($_SESSION['username'])) {
header("Location: login.php");
return;
}
$pid = $_SERVER['REQUEST_URI'];
$pid = trim($pid, "/IncBlog/edit_post.php?pid");
$pid = strip_tags($pid);
$pid = stripslashes($pid);
$pid = mysqli_real_escape_string($db, $pid);
//echo "$pid";
if ($pid == "") {
header("Location: ../Nav/life.php");
}
if (isset($_POST['update'])) {
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$img = strip_tags($_POST['image']);
$title = mysqli_real_escape_string($db, $title);
$content = mysqli_real_escape_string($db, $content);
$img = mysqli_real_escape_string($db, $img);
$date = date("l jS \of F Y h:i:s A");
$sql = "UPDATE posts SET title='$title', content='$content', img='$img', date='$date' WHERE id=$pid";
if ($title == "" || $content == "") {
echo "The database is hungry you can't feed it nothing!";
return;
}
mysqli_query($db, $sql);
header("Location: ../Nav/life.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Blog - Post</title>
</head>
<body>
<?php
$sql_get = "SELECT * FROM posts WHERE id=$pid LIMIT 1";
$res = mysqli_query($db, $sql_get);
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$title = $row['title'];
$content = $row['content'];
$img = $row['image'];
echo "<form action='edit_post.php?pid=$pid' method='post' enctype='multipart/form-data'>";
echo " <input placeholder='Title' type='text' name='title' value='$title' autofocus size='48'><br /><br />";
echo " <input placeholder='Image' type='text' name='image' value='$img' autofocus size='48'><br /><br />";
echo " <textarea placeholder='Content' name='content' rows='40' cols='40'>$content</textarea><br />";
}
}
?>
<input type="submit" name="update" value="Update">
</form>
</body>
</html>
You have to set the columns and set the values for the columns.
UPDATE posts SET ( title, content, img, date ) VALUES ($title, $content, $img, $date) WHERE id = $pid;
$date = date("l jS \of F Y h:i:s A");
use instead of above line
$date = date('Y-m-d H:i:s', strtotime());
$sql = 'UPDATE posts SET title='".$title."', content='".$content."', img='".$img."', date='".$date."' WHERE id='".$pid."'";
Ok so I figured it out, very small programmers blindness but hey we all get it.
Line 66 echo "<form action='edit_post.php?pid$pid' method='post' enctype='multipart/form-data'>";
Where it says "?pid$pid" I just needed to remove the "=" that i had there.
I am having problem of getting the values from mysql / php and I would appreciate if someone could help me. My problem is that I have a table called albums and in that table I have 6 columns which are id, album_name, artist, company, genre, price. I have written this code in my index.php:
<?php
$query = mysqli_query ($dbconn, "SELECT * FROM albums ORDER BY id DESC LIMIT 4 OFFSET 8");
while($result = mysqli_fetch_assoc($query)){
$id = $result["id"];
$album_name = $result["album_name"];
$img = $result["image"];
$artist = $result["artist"];
$company = $result["company"];
$genre = $result["genre"];
$price = $result["price"];
echo "<div class='col-md-3 col-xs-6'>
<a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
<div class='inside_box'>
<div class='small_title'>
<h4>$album_name</h4>
</div>
<div class='photo_box'>
<img src=$img class='img_dim'>
</div>
<div class='info'>
<p>Artist: $artist</p>
<p>Company: $company</p>
<p>Genre: $genre</p>
<p>Price: $price</p>
</div>
</div>
<div class='buy_now'>
<p>Buy Now</p></a>
</div>
</div>
</div>";
}
?>
I have another page called album_page.php which i am sending from my index.php to album.php the $id in order to get the data from my database. In my album_page.php page i have this code:
<?php
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$id = $_GET["id"];
$album_name = $_GET["album_name"];
$artist = $_GET["artist"];
$company = $_GET["company"];
$genre = $_GET["genre"];
$price = $_GET["price"];
echo "<div class='col-md-4 right_box'>";
echo "<p>Album Name: $album_name</p>";
echo "<p>Artist: $artist</p>";
echo "<p>Company: $company</p>";
echo "<p>Genre: $genre</p>";
echo "<p>Price: $$price</p>";
echo "<div class='buy_now_box'>Buy Now</div>";
echo "</div>";
?>
What i would like to do is to send that $Id from my index.php and get all the data in my album_page.php using the $_GET. I would like to get all the data from my database with the $id = 5 for example. I have tried this using many paremeters for example:
<a href ='album_page.php?id=$id&image=$img&album_name=$album_name&artist=$artist&company=$company&genre=$genre&price=$price&buy_now=$buy'target='_blank' class='box_link_hover'><div class='box'>
and it is working fine but i would like to have the same result using only this:
<a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
Thanks.
OK i have found what the problem was. I had to write in my album_page.php this in order to grap the data:
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$result = mysqli_fetch_assoc($sql);
$id = $_GET["id"];
$album_name = $result["album_name"];
$artist = $result["artist"];
$company = $result["company"];
$genre = $result["genre"];
$price = $result["price"];
Maybe this works even better ;-)
$id = $_GET["id"]
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$result = mysqli_fetch_assoc($sql);
// ....
I'd like to put images next to each other when they are selected from a while loop in PHP.
So, currently it looks like this http://prntscr.com/7tb42j
And I'd like it to put the images next to each other.
My foreach loop looks like this:
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
You'll just need to add display:inline-block to each div that contains a picture.
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1' style="display:inline-block"><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
Make sure on "album-foto" you have the css set for (see below why I use album-foto not album-foto-1):
.album-foto {
display:inline; // or inline-block depending how you want to be displayed
}
Also if you're displaying multiple images you should use a class not an ID for the images as duplication of the same ID is not good:
foreach($stmt as $row)
{
$id = $row["id"]; // or whatever your ID field is
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div class='album-foto' id='album-foto-<? echo $id; ?>'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
I have a database with a few images already set, I would like to have the url display an ID from each query as the user hits next. The user should be able to share the URL and paste it into their browser, the url should pull that unique ID from the query. The issue i am having is every time i paste a url, I get a random image and not the image that is in the ID. I'm at a loss here and im not sure what to do :( here's the code I have so far.
<?php
if (isset($_GET['id'])) {
include("PHP/db.php");
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
if($_GET['next4']) {
echo 'HELLO THIS IS THE NEXT IF METHOD';
include("PHP/db.php");
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
Try this:
<?php
include("PHP/db.php");
$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];
if (!isset($_GET['id'])) {
$_GET['id'] = $rand_id;
}
if (isset($_GET['id'])) {
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
I changed:
-link to next is now id=rand
-changed your code to give me a "rand ID" and its already defined on the href of the page you load
It will go inside condition (next == true).
Make sure that your variables are initialized before use.