while($query->fetch()) both working and not working - php

I'm working on a site which displays a blog and a photo gallery, which both get all their data from a MySQL-database using the same code (see below).
<?php
include_once("sql/db_connect.php");
$query1 = $db->prepare("SELECT post_id, title, body, img, timeposted FROM blogposts ORDER BY post_id DESC");
$query2 = $db->prepare("SELECT img_id, imgsrc FROM gallery ORDER BY img_id DESC");
$query1->execute();
$query2->execute();
$query1->bind_result($post_id, $title, $body, $img, $timeposted);
$query2->bind_result($img_id, $imgsrc);
?>
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<header>...</header>
<main>
<div class="container">
<div>
<!-- Choose between tabs -->
</div>
<div class="wrapper1">
<?php
while($query1->fetch()):
?>
<div class="w3-col s12 m6">
<div class="card w3-depth-1">
<div class="cardheader">
<?php echo $timeposted ?>
<span></span>
</div>
<div class="cardmain">
<div class="cardtext">
<h1><?php echo $title ?></h1>
<p><?php echo $body ?></p>
</div>
<?php
if($img !== "NOPE") {
echo "<img src='uploads/" . $img . "' class='blogimage' />";
}
?>
</div>
</div>
</div>
<?php endwhile?>
</div>
<div class="wrapper2">
<?php
while($query2->fetch()):
?>
<div class="w3-col s12 m6">
<?php echo "<img src='uploads/" . $imgsrc . "' class='galleryphoto' />"; ?>
</div>
<?php endwhile?>
</div>
</div>
</main>
<footer>...</footer>
</body>
The div "wrapper1" is used for the blog, "wrapper2" for the gallery and at the top of the page there is a button which lets you switch between views (using jQuery). So on default, wrapper1 is displayed, but on clicking the button it will switch to wrapper2.
Now, my problem: in wrapper1, all blogposts are displayed, but in wrapper2, none of the pictures stored in the database are displayed at all.
And, before you ask, yes, the table is actually called "gallery" and the values are actually called "img_id" and "imgsrc". I guess I've checked that about ten times already.
When I placed "echo 'Hello World';" before the second while loop, the page said Hello World, but when I placed it in the while loop, the page still didn't display anything; that said, it shouldn't be the jQuery, I guess.
I hope that anybody can point out what I'm doing wrong. Thanks in advance! If there's any additional data / code you need to have, I'd be happy to provide that.

Related

<pre> tag messing with UI

I returning my data to build a list group. All is fine but when the data contains <pre> tag it messes my display.
My list group as follows.
if (isset($BS_array)){
//Create a list group to show results
echo '<div class="list-group">';
foreach($BS_array as $result){
?>
<!-- Build list group -->
<div class="list-group-item list-group-item-action" aria-current="true">
<div class="d-flex w-100 justify-content-between">
<!-- Incident number goes here -->
<h5 style="cursor: pointer;" id="<?php echo $result['number']; ?>" onclick="getINCDetails(this.id)" class="mb-1"><?php echo $result['number']; ?></h5>
</div>
<!-- Description goes here -->
<p class="mb-1"><?php echo $result['description']; ?></p>
<small>
<?php
//List BS in INC card
echo $result['state'];
?>
</small>
</div>
<?php
}
echo '</div>';
}
?>
The way when all goes ok should be like this.
But at times, $result['description'], contains the word <pre>, which messes things up.
It will look like this.
How do I fix/circumvent this problem?
If you dont require html tags to actually work on the description just use htmlentities():
<?php echo htmlentities($result['description']); ?>
That will actually show instead of the browser considering it html tag.

MYSQL Product listing grid

So i am trying to pull product data from a database and have it listed out in a nice grid. I have got everything to work and its layout is very nice apart from one issue, the image URL isnt pulling properly:
<?php
while ($row = mysql_fetch_array($query))
$rows[] = $row;
foreach ($rows as $row){
$etitle = $row['title'];
$eprice = $row['price'];
$eurl = $row['prod_url'];
$eimage = $row['image'];
echo ('<div class="col-sm-6 col-md-4">
<div class="card">
<div class="card-image" style="background-image: url("' . $eimage . '");">
<div class="card-image-rating">');
echo ('£' . $eprice);
echo ('
</div><!-- /.card-image-rating -->
</div><!-- /.card-image -->
<div class="card-content">
<h2><a href="' . $eurl . '">');
echo $etitle;
echo ('</a></h2>
</div><!-- /.card-content -->
<div class="card-actions">
<i class="md-icon">favorite</i>
Show More
</div><!-- /.card-actions -->
</div><!-- /.card -->
</div><!-- /.col-* -->');
} ?>
In the source code and on page everything look good apart from their being no image. The column in the database has a URL of the image location in it but in the source code it appears to be removing the / from the path.
Any help would be amazing.
Thanks,
Based on your script, I noticed something goes wrong
<div class="card-image" style="background-image: url("' . $eimage .'");">
you use double quotation marks in style section
your html will look like this
<div class="card-image" style="background-image: url(" ");">
which is shouldn't be like that.
you have to use Apostrophe inside style, since u use quotation mark for style
your html should like this
<div class="card-image" style="background-image: url(' ');">
here, an example for you
https://jsfiddle.net/have_full/kass6ukr/

How to retrieve image from database and display image on the web page

I have inserted image into database and store name in the table.
my image is saved in a folder named 'Uploads'.
Now need to retrieve image from the databse and display it. when I try to display It only shows the image name which is taken from my table.but it does not show the image.
retrieving code is given below
$sql="SELECT * FROM candi_profile WHERE can_email='{$_SESSION['usr_email']}'";
$result=mysqli_query($con,$sql);
if(!$result) die(mysqli_error($con));
<div class="container">
<!-- Page Header -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Employer Dashboard
</h1>
</div>
</div>
<!-- /.row -->
<!-- Projects Row -->
<div class="row">
<div class="col-md-4">
<?php
while($rows=mysqli_fetch_array($result)){
$c_id = $rows['can_id'];
var_dump($c_id);
?>
<p class="lead"><?php echo $rows['can_name'] ?></p>
<div class="profile-sidebar">
<!-- SIDEBAR USERPIC -->
<div class="profile-userpic">
<p class="lead">
<?php echo $rows['pic_name'] ?></p>
</div>
<!-- END SIDEBAR USERPIC -->
<!-- SIDEBAR USER TITLE -->
<div class="profile-usertitle">
<div class="profile-usertitle-name">
Marcus Doe
</div>
<div class="profile-usertitle-job">
<?php echo $rows['can_city'] ?>
<i class="glyphicon glyphicon-map-marker">
</i>
</div>
<div class="profile-usertitle-job">
<i class="glyphicon glyphicon-envelope"></i>
<?php echo $rows['can_email'] ?>
</div>
<div class="profile-usertitle-job">
<?php echo $rows['can_country'] ?>
</div>
</div>
<!-- END SIDEBAR USER TITLE -->
<!-- SIDEBAR BUTTONS -->
<div class="profile-userbuttons">
<hr>
</div>
<!-- END SIDEBAR BUTTONS -->
<!-- SIDEBAR MENU -->
<?php
}
?>
</div>
you can use this code to retrieve image from database
<?php
include 'connection.php'
?>
<?php
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
?>
<table border="1" cellpadding="5" cellspacing="5">
<tr> <th>Image</th></tr>
<?php
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
?>
<tr>
<td><img src="uploads/<?php echo $row['pic_name'];?>" alt=" " height="75" width="75"></td>
</tr>
<?php
}
}
?>
</table>
I assume that the content of $rows['pic_name'] is string only as said on your question.
Put an image attribute and call the path of the image with the corresponding filename save on the database.
<img src = "<path>/<?php echo $rows['pic_name'] ?>" />
NOTE:
Make sure the image is existing on your desire path.
Use image tag to display the image and give it path to the image folder
<img src="your path/<?php echo $rows['pic_name'] ?>" />
friend instead of making images folder you should make a new image column(i.e "imageColumn ") type as blob then
You need to create another php script to return the image data, e.g. getImage.php.
home.php(or display image page) code
<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>
Then getImage.php is
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT imageColumn FROM Tablename WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['imageColumn '];
?>
First fetch image from database using query
The imagejpeg() function is an inbuilt function in PHP which is used to display image to browser or file.
Get data using function ob_get_contents();
Display image in page with height and width
$id = $_GET['id'];
$sql = "select image from table where id='".$id."'";
$res = mysqli_query($sql);
$row = mysqli_fetch_assoc($res);
$image = $row['image'];
ob_start();
imagejpeg($image, null, 50);
$data = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,'".base64_encode($data)."' style='border:1px
black; border-radius:10px; width:100px; height:125px;'>";

php pass id into modal

I'm stuck again! So hopefully you awesome people can help me...
So far what I have is a search function that queries the database, and returns results, which works fine, and then the user can click one of the results, which takes them through to a new detailed page of what they clicked, for example a brand. Then on the detailed page, it returns all of that brands details (using $_GET['id']) and a new set of data, from a relational table, with a list of all the offers corresponding to that brand, which also works. But then I get stuck, I want the user to be able to click on each offer, and have the details of that offer load into a modal... I've tried using the $_GET['id'] for the offers id, but obviously, with it being on the same page (i presume), it passes the id of the brand / parent instead of the offer.
This is the code that calls back all the offers relating to the brand
$offers_sql = 'SELECT * FROM codes WHERE client_id LIKE :client_id AND CURDATE() between start AND expiry';
$offersq = $db->prepare($offers_sql);
$offers_params = array(':client_id' => $id);
$offersq->execute($offers_params);
$results = $offersq->fetchAll(PDO::FETCH_ASSOC);
if(count($results) < 1) {
$output = "<h4>no current offers available</h4>";
}
else {
foreach($results as $r) {
$title = $r['title'];
$code = $r['textcode'];
$offer_id = $r['id'];
$expiry = $r['expiry'];
$date = new DateTime($expiry);
$output .= '<h4>' . $title . '</h4><hr>';
}
}
} catch (PDOException $e) {
echo "failed to load offers";
}
?>
and this is the code i was using for the modal
require('inc/connect/config.php');
include('inc/result.php');
$page_title = "Title - " . $name . " Offers";
include('inc/style.php');
include('inc/header.php');
include('inc/offers.php');
?>
<!-- Intro Section -->
<section class="intro-section">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h3 class="navbar-fixed-top">Latest Deals</h3>
<h1><?php echo $name; ?></h1>
</div>
</div>
</div>
</section>
<section class="offer-section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-6 col-lg-offset-3">
<?php echo $output; ?>
</div>
</div>
</div>
</section>
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
</div>
</div>
<?php include('inc/footer.php'); ?>
The code that returns the details of the brand is on a separate page, but i can include that if necessary.
What am i doing wrong? Please go easy on me, I am relatively new to php.
Thanks in advance
Kaylee
Make Changes, Where Output is declared in else condition. (Changes Done)
$output='<h4><a class="OfferIdClass" data-OfferID="<?echo $offer_id;?>" href="#offer" data-modal="offer" data-toggle="modal">'.$title.'</a></h4>';
// Add This to footer.php (Changes Done)
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
</div>
</div>
//Add this below in that page, where you are clicking Modal.
//Changes Done
<script>
$('.OfferIdClass').click(function(){
var OfferID=$(this).attr('data-OfferID');
$.ajax({url:"Open-Offer.php?OfferID="+OfferID,cache:false,success:function(result){
$(".md-content").html(result); //Here, Changes Done
}});
});
</script>
//Create one Open-Offer.php page. (Changes Done)
Open-Offer.php
<?
extract($_GET);
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
?>

"if message/text exists in database", then show the message that the user created from latest to oldest

I'm working on this message system. The user has a form presented to him, where the user will type notes, in this case called a "message". When the user visits his profile page, he is shown the message that he had typed in the form earlier and pressed saved.
Screenshot:
http://i.stack.imgur.com/VEsDg.png
My database has a table called "messages" where I have columns pid, uid, message, time, and picture where the uid is the uid in my "users" table.
So the pid is a auto-incrementing and the uid is the user id of the user who posted the message.
(The user posts a URL to his picture in the form which is later saved to the picture column in the database.
My PHP code where I get data from the database:
<?php
$uid = $this->session->userdata('uid');
$this->db->limit(10); //Use this to limit the entires while you show the entires on the front page.
$query = $this->db->query("SELECT pid, message, time, picture FROM messages WHERE uid = '$uid';");
foreach ($query->result_array() as $row)
{
$message = $row['message'];
$time = $row['time'];
$picture = $row['picture'];
}
?>
My html code in my profile_body.php page in my views folder where I have the above's code included.
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $picture ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $message;?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
How would I get only 10 results for the messages that the users typed in the database and display it? I have tried to paste the same html code again but it displays the same message and picture.
Thanks.
Here is some improvements to do :
$uid = $this->session->userdata('uid');
$result = $this->db->select('pid, message, time, picture')
->where(array('uid' => $uid))
->limit(10)
->get('messages')
->result_array();
$this->load->view('profile_body', $result);
In your view profile_body.php file :
<?php foreach($result as $row): ?>
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $row['picture'] ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $row['message'];?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
<?php endforeach; ?>

Categories