Reputation: 100
I can't find what am I doing wrong with my current code. I am trying to add info in database via control panel and this info should be displayed in my website. I have created the adminpanel and all conections, but right now when I add something with the admin panel it's beeing added in the database but if I want to display that data in the website I have to switch ID's by changing my code in the index.php by writing. Can someone recommend me some solution where the data will be also displayed in the website.
This is how I am getting the data from the DB:
<div class="post">
<div class="post-image hover01">
<figure><img src="images/posts/latestnews4.jpg" alt="post image" /></figure>
</div>
<div class="post-detail">
<?php
if($_SESSION["ses"]=="")
{
$cont="select * from content where id='1'";
//echo $cont;
//exit;
$cont_row=mysql_query($cont);
$cont_res=mysql_fetch_assoc($cont_row);
?>
<h3 class="post-title"><?php echo $cont_res["page_title"]?></h3>
<span class="dates">21 Oct, 2016 / New York</span>
<p class="post-description">
<?php
echo $cont_res["details"];
?>
</p>
<?php } ?>
<div class="read-more">
<a href="detail.html">Вижте повече...</a>
</div>
<div class="sign-up"></div>
</div>
</div>
So I have 5 containers like this one and in each of them I am adding new info the the data base table named: "contents" and in each of them I have to write the id by my self. What is the solution so that I don't have to write it by my self and it will be filled automaticly?
Upvotes: 0
Views: 44
Reputation: 266
Try fetching all the data from contents
with select * from contents
to one variable, like $posts
, then try something like this:
<?php foreach ($posts as $post): ?>
<div class="post">
<div class="post-image hover01">
<figure><img src="images/posts/latestnews4.jpg" alt="post image"/></figure>
</div>
<div class="post-detail">
<h3 class="post-title"><?php echo $post["page_title"]; ?></h3>
<span class="dates">21 Oct, 2016 / New York</span>
<p class="post-description"><?php echo $post["details"]; ?></p>
<div class="read-more">
<a href="detail.html">Вижте повече...</a>
</div>
<div class="sign-up"></div>
</div>
</div>
<?php endforeach; ?>
I'm not sure if that's what you intended to do, if not, correct me.
By the way, mysql_fetch_assoc()
and mysql_query()
are deprecated in PHP 5.5 and removed in PHP 7.
Upvotes: 1