How to display recent posts from database into php homepage? - php

I want to display post details ( title, description, username ) on my home page within a div tag in a proper format like all websites have. The problem I'm facing is that all the data from database are getting displayed as a plain text, one below the other. I am new to php, so please guide me to achieve the result.
Here is my code.
I want to display in this tag:
<div id='display'>
<h3 class='name'></h3>
<h1 class='title'></h1>
<p class='desc'></p>
<p class='cat'></p>
<p class='sub_cat'></p>
</div>
And my php code is:
<?php
$row="";
$link = mysql_connect("localhost","username","password");
mysql_select_db("database");
$query = "SELECT * from posts ORDER by post_id DESC limit 0,5";
$result = mysql_query($query);
$result = mysql_query($query) or die("Query to get blah failed with error:".mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<div id='display'>";
echo "<h3 class='name'>".$row['username']."</h3>";
echo "<h1 class='title' >".$row['post_title']."</h1>";
echo "<p class='cat'>".$row['cat']."</p>";
echo "<p class='sub_cat'>".$row['sub_cat']."</p>";
echo "<p class='desc'>".$row['post_desc']."</p>";
echo "</div>";
}
mysql_close($link);
?>

I think what you are missing is some CSS. In HTML, DIV-Containers are displayed as block elements. So without any configuration, they are displayed one below the other. To get them next to each other, just add some CSS to your page:
<div id='display'>
<h3 class='name'></h3>
<h1 class='title' ></h1>
<p class='desc'></p>
<p class='cat'></p>
<p class='sub_cat'></p>
</div>
<div class="clear"></div>
<style type="text/css">
.name, .title, .desc, .cat, .sub_cat {
float:left;
}
.clear {
clear:both;
}
</style>

Related

Reload DIV with new images

I have an employee database which includes images as well as their work location (specialty). I have created a page where I fill out a form and upload the image to a directory and the path to the database. I then load the main page where I pull in all the images from the database (into the "photos" DIV. Everything works fine.
What I would like to do is reload the images in the DIV based on a MySQL query from a button. For example, instead of showing all employees, I only want to see those who have a specific job function i.e. Management. I currently have this accomplished by redirecting to a new page, where I run a specific query and that works fine as well. However, I'd like to learn how this is done without creating a new page for each query. I've spent many days looking at AJAX and PHP tutorials, which I how I was able to accomplish what I have, but I can't find a method to do what I want. This is the relevant part of my code:
Main.php
<div class="container-fluid">
<div class="row">
<div class ="col-lg-12" style="width: 18%; height: 100%; border:3px solid red;">
MANAGEMENT
HIGH
</div>
<?php
include ('db_connect.php');
//include_once ('functions.php');
$result = $db->query("SELECT * from monctonfir order by initials ASC");
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
?>
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card text-center" style="width: 5rem;">
<a href = "#">
<img class="img card-img-top" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
</div>
Can someone point me in the right direction?
Thanks!
You don't need jQuery for what you are doing. You can use query/GET parameters to build your sql so you don't have to create a different page. Like:
<div class="container-fluid">
<div class="row">
<div class ="col-lg-12" style="width: 18%; height: 100%; border:3px solid red;">
MANAGEMENT
HIGH
ALL
</div>
<?php
include ('db_connect.php');
//include_once ('functions.php');
$sql = "SELECT * from monctonfir WHERE 1 ";
if(isset($_GET['job'])) $sql .= " AND job = '".$_GET['job']."' ";
$sql .= " order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
?>
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card text-center" style="width: 5rem;">
<a href = "#">
<img class="img card-img-top" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
</div>
The simplest way is to use the load function of Jquery
Jquery
For example:
$( "#divID" ).load( "loadEmploye.php", { parameters1: 25, parameters2:3 });

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;'>";

Can i get only the img from a database field where different html tags are used?

I have this php array. Where in my database i have a field called body, and in that field there is some html code. Like this:
<h1>title</h1><img src="http://farm5.staticflickr.com/4075/4788694752_d03557765b_z.jpg" alt=""/>
Here is my php code:
<?php
$q = "SELECT * FROM journals ORDER BY timestamp DESC";
$r = mysqli_query($dbc, $q);
while($journal_list = mysqli_fetch_assoc($r)) { ?>
<div class="col-md-4">
<a class="list-group-item" href="journal.php?id=<?php echo $journal_list['id']; ? >">
<h4 class="list-group-item-heading"><?php echo $journal_list['body']; ยด?></h4>
</a>
</div>
<?php } ?>
In the h4 im calling the body field in the database. But i only want the img in that field??
Try phpquery:
$src = phpQuery::newDocumentHTML($journal_list['body'])->find('img')->attr('src');
This is a little hack that you can use
$img = explode('img',$journal['body']);
$final_img = '<img '.$img[1];
Now echo image as
<h4 class="list-group-item-heading"><?php echo $final_img; ?></h4>

Div tag doesn't close in php

Easy one! I'm trying to code a cheap forum. Coming from a C background, I started to noticed something strange about PHP. While having a function return a string (HTML) inside of a DIV into place, the browser would not print the </DIV> - even when it's echo'ed by itself.
Does PHP decide when it wants to echo certain DOM elements or have limitations on HTML output?
echo "Start<div id='Forum'>";
echo "Forum";
GetFullList();
echo "</div>";
Where, GetFullList() consists of:
function GetFullList(){
$sql="SELECT * FROM `Forum` WHERE `IsReply` =0";
$result=mysql_query($sql);
if (!$result){
echo mysql_error();
}
if($result){
while($ForumEntry = mysql_fetch_assoc($result)){
$IsReply = $ForumEntry["IsReply"];
$ParentPost = $ForumEntry["ParentTopic"];
$f_User = $ForumEntry["User"];
$f_Replies = $ForumEntry["Replies"];
$f_Views = $ForumEntry["Views"];
$f_Time = $ForumEntry["Time"];
$f_Post = $ForumEntry["Post"];
$f_Topic = $ForumEntry["Topic"];
$f_Index = $ForumEntry["Index"];
echo DisplayPost($f_User, $f_Replies, $f_Views, $f_Time, $f_Post, $f_Topic, $f_Index);
GetChildPostsOf($ParentPost);
}
}
}
And DisplayPost() is built as such:
function DisplayPost($f_User, $f_Replies, $f_Views, $f_Time, $f_Post, $f_Topic, $f_Index){
$PostBlock = "<div id='Grp_Cell' style='width:930;background-color:#999999;text-align:left;'><div id='Grp_Cell' style='float:left;'><div id='Tbl_Cel'>User: ".$f_User."</div><div id='Tbl_Cel'>Replies: ". $f_Replies."</div><div id='Tbl_Cel'>Views: ".$f_Views."</div><div id='Tbl_Cel'style='background-color:777777;height:112;'>Post started on ".$f_Time.". </div></div><div id='Grp_Cell' style='float:right;width:600;'><div id='Tbl_Cel'>Subject: ".$f_Topic."</div><div id='Tbl_Cel' style='background-color:777777;height:150;'>". $f_Post."</div><a onClick='Reply(".$f_Index.");Filter();'><div id='Tbl_Cel' style='background-color:#888888; height:50; width:50; float:right; padding:2;border-color:black; border:2;'><br>Reply</div></a></div>";
return $PostBlock;
}
(Displays a div scaffolding for DB results: the post.)
When I try to echo "< /div>" after GetFullList(), the result is not printed in HTML, leaving the rest of the page to be encompassed under the malformed div.
There are 10 opening divs and 9 closing divs in $PostBlock. A closing </div> should be added where necessary. An easy way to see what the output looks like is to break it into lines like this:
$PostBlock = "
<div id='Grp_Cell' style='width:930;background-color:#999999;text-align:left;'>
<div id='Grp_Cell' style='float:left;'>
<div id='Tbl_Cel'>User: ".$f_User."</div>
<div id='Tbl_Cel'>Replies: ". $f_Replies."</div>
<div id='Tbl_Cel'>Views: ".$f_Views."</div>
<div id='Tbl_Cel'style='background-color:777777;height:112;'>Post started on ".$f_Time.". </div>
</div>
<div id='Grp_Cell' style='float:right;width:600;'>
<div id='Tbl_Cel'>Subject: ".$f_Topic."</div>
<div id='Tbl_Cel' style='background-color:777777;height:150;'>". $f_Post."</div>
<a onClick='Reply(".$f_Index.");Filter();'><div id='Tbl_Cel' style='background-color:#888888; height:50; width:50; float:right; padding:2;border-color:black; border:2;'><br>Reply</div></a>
</div> ";

posts overlapping

Now i dont know if this is simple or hard. If its just css or php code i need
But basically i have posting system and users can comment on posts. In the comments page it shows orginal post and one users have left (the comments)
I had one in there and this was fine but i added another and it looked like this...
[1]: http://i.stack.imgur.com/2fIXd.jpg
As you can see its completly different! Heres my code for it...
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
echo "<a href='Untitled9.php'>Go Back...</a>";
?>
<br/><br/>
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '".
mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted"; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo " ".$r['message'].""; ?></div>
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down']."";>
</div>
<br/>
<hr width="725px">
<?php
echo "<h3>Replies...</h3>"; ?>
<div class="message"><?php
$sql = mysql_query("SELECT * FROM replies WHERE thread = '".
mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted"; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2">
<?php echo " ".$r['message']."" ; } ?> </div>
</div>
<hr width="725px">
<form action="newreply.php" method="POST">
Your Name: <input type="text" name="author">
<input type="hidden" value="<?php echo $_GET['id']; ?>" name="thread"><br>
Message:<br><textarea cols="60" rows="5" name="message"></textarea><br>
<input type="submit" value="Post Reply">
</form>
The code looks really messy on here. I tried editing but couldnt get much better.
So bascially what i want to know is how do i prevent this (the overlapping) from happening?
Edit * CSS
.message {
width: 500px;
color: black;
background: white;
padding:8px;
border:1px solid white;
margin:5px auto;
-moz-border-radius:8px;
}
.message2 {
background-color: grey;
}
It looks to me as though everything is posting inside the second php function but i have some code pretty much the same for just the individual post and this displays normally i.e. as many as i want. Im just wondering is there something i need to add/change
Wrong (Your code):
<?php echo " ".$r['message']."" ; } ?> </div>
</div>
Correct:
<?php echo " ".$r['message']."" ; ?> </div>
</div>
<?php } ?>
You were opening multiple DIVs in your while loop but only closing two.
Similarly to Cobra_Fast's reply, it seems that the positioning of your divs seemed to be causing the problem, and also the position of your while loop.
Try replacing the replies section with the following and let me know if it is any better.
<?php
echo "<h3>Replies...</h3>";
$sql = mysql_query("SELECT * FROM replies WHERE thread = '".mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
?>
<div class="message">
$posted = date("jS M Y h:i",$r['posted']);
echo $r['author']." ".$posted;
?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>
<div class="message2">
<?php
echo " ".$r['message'];
?>
</div>
<?php
}
?>

Categories