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);
// ....
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 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>
I can't seem to return any values on my $_GET array.
It works fine when e.g.
$sql = "SELECT * FROM review WHERE brand='brandx'" but when I change it to brand='$id' in line 5, nothing gets passed.
The fetch array in my index.php works perfectly fine however when it gets href to brand.php (as shown below), I lose my marbles.
<?php
if(isset($_GET["id"])){
include "php_includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET["id"]);
$sql = "SELECT * FROM review WHERE brand='$id'";
$query = mysqli_query($db_conx, $sql);
$productList = "";
// Now make sure that brand exists in the table
$productCount = mysqli_num_rows($query);// count the output amount
if($productCount > 0){
//get the products off the selected brand
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row['username'];
$productname = $row['productname'];
$comment = $row['comment'];
$rating = $row['rating'];
$date = $row['date'];
$productList .=
'
<div class="wrapper">
<div class="brand-and-name">
<div class="brand">
<a href="brand.php?id='.$id.'">
<span>'.$id.'</span>
</a>
</div>
<div class="prod-name">
'.$productname.'
</div>
</div>
<div class="prod-container" id="pd1">
<div class="prod-img"><img src="https://giovanniphotography.files.wordpress.com/2011/09/creativemevid19.jpg" /></div>
<div class="comment">
<b>My Score: '.$rating.'/10</b>
<br /><br />
<p>'.$comment.'</p>
</div>
<div class="profile">
<div class="profile-thumb" id="pt1"></div>
<div class="name" id="nm1">
'.$username.'<br />'.$date.'
</div>
</div>
<div class="social-share-1">
<div class="like-btn"></div>
<div class="comment-btn"></div>
<div class="wishlist-btn">+ wishlist</div>
</div>
</div><!--end .prod-container#pd1-->
</div><!--wrPer-->
';
}
}else{
echo "Product doesnt exist";
exit ();
}
}else{
echo "You got to pick a brand man!";
exit ();
}
?>
Does this code work, after your preg_replace? If not you may not have magic quotes enabled in php.ini. I notice in the rest of your output you are concatenating strings and variables.
print "ID: $id";
use a prepared statement and dont hassle with sanitizing of user input:
if($stmt = $db_conx->prepare("SELECT username, productname, comment, rating, date FROM review WHERE brand=?)")
{
$stmt->bind_value('s', $_GET["id"]);
$result = $stmt-execute();
$stmt->bind_result($username, $productname, $comment, $rating, $date ); //bind result to vars
//now you can loop through your result:
while($stmt->fetch()) {
//use $username, $productname, $comment, $rating, $date etc to work with your values
}
}
can you help me figure out the correct structure for this mysqli_fetch_array? I read up on http://www.w3schools.com/php/func_mysqli_fetch_array.asp but I still don't know what am I missing.
I am still new to php!
I am getting this error.
Notice: Undefined variable: video_name in D:\xampp\htdocs\video_upload\index.php on line 68
<div id="box">
<?php
$query = mysqli_query($db, "SELECT 'id', 'name', 'url' FROM videos");
while($run = mysqli_fetch_array($query)){
$video_id = $run['id'];
$video_name = $run['name'];
$video_url = $run['url'];
}
?>
<a href='vide.php?videos=<?php echo $video_url; ?>'>
<div id='url'>
<?php echo $video_name; ?>
</div>
You are using ' instead of `. Change that first. Then put the anchor tag inside the loop.
Edit like this,
<div id="box">
<?php
$query = mysqli_query($db, "SELECT `id`, `name`, `url` FROM videos");
while($run = mysqli_fetch_array($query)){
$video_id = $run['id'];
$video_name = $run['name'];
$video_url = $run['url'];
?>
<a href='vide.php?videos=<?php echo $video_url; ?>'>
<div id='url'>
<?php echo $video_name; ?>
</div>
</a>
<?php
}
?>
</div>
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.