PHP - Query and loop to show different data - php

I'm struggling to show different news in my page. I want to show each of the news in 1 div and the limit is 4 news. Example, i have 4 different news and i want of every each of it to display separately in divs. Im new to html and php. Can someone give me ideas what the best loop and queries for this?
to easily understand what i want to do here is the picture.
here is what i im doing. i think im lost.
here is my php code for the news..
<div class="content">
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-9">
<div class="fix leftbar floatleft">
<div class="fix left_sidebar">
<div class="news">
<h2><i class="fa fa-newspaper-o"></i> Latest News</h2>
<hr class="carved">
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
View More News
</div>
</div>
</div>
</div>

Simply like your first loop... with better format
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<div class="fix single_news">
<?php echo $title; ?>
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<hr class="carved"/>
<?php
}
?>
Your loop is taking all article, so you need only one loop like this.
Inside you just need to format only one article, all others will take the same format.

You are looping the output over and over again, you only need the single loop to get the data from the database.
Note this is untested:
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
echo '<div class="fix single_news">';
echo '<div class="single_image">';
echo '<img src="img/coveredcourt.jpg" alt="court">';
echo '</div>';
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<hr>
<?php
}
?>
</div>
View More News

Related

Generate HTML code with PHP automatically, after data pull in MySql

I have written a really simple php page that populate a database.
I now want to fetch this data in another php page and that is ok.
What I would like to achive is:
when I add another row into the database, I would like the html to create a new card, and not add the information in the same card.
I am not sure I understand how this can be achived.
Do I have to use php templates like smarty or anybody can point me how could I proceed?
This is how it look when I add second raw:
While what i want to achive should look like
Here is the HTML code I use with the PHP code:
<section class="tm-section-3 tm-section-mb" id="tm-section-3">
<div class="row">
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h2>
<?php if (mysqli_num_rows($result) > 0) {
?>
<table>
<?php
include_once '../scripts/connection.php';
$result = mysqli_query($link,"SELECT
domain,subdomain,topic,topictitle,topictext FROM newpage");
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["domain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["subdomain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["topic"]; ?></td>
</tr>
<tr>
<td><h4><?php echo $row["topictitle"]; ?></h4></td>
</tr>
<tr>
<td><h5><?php echo $row["topictext"]; ?></h5></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</h2>
<p>
</p>
<div class="text-center">
Details
</div>
</div>
</div>
</div>
</section>
This is how i send the code to the db:
<?php
include("connection.php");
$domain = mysqli_real_escape_string($link, $_POST['domain']);
$subdomain = mysqli_real_escape_string($link, $_POST['subdomain']);
$topic = mysqli_real_escape_string($link, $_POST['topic']);
$topictitle = mysqli_real_escape_string($link, $_POST['topictitle']);
$topictext = mysqli_real_escape_string($link, $_POST['topictext']);
$sql = "INSERT INTO newpage (domain,subdomain,topic,topictitle,topictext) VALUES ('$domain','$subdomain','$topic','$topictitle','$topictext')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
$sql = "INSERT INTO menu (item) VALUES ('$domain')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:../scripts/add-new-page-script-end.php");
exit;
echo "You'll never see this";
?>
Here the code that works even the style is bad. But logically is correct:
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<?php
include_once '../scripts/connection.php'; $result = mysqli_query($link,"SELECT domain,subdomain,topic,topictitle,topictext FROM newpage"); foreach($result as $row){
?>
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h1><?php echo $row['domain']; ?></h1>
<h2><?php echo $row['subdomain']; ?></h2>
<h3><span><?php echo $row['topic']; ?></span></h3>
<h4> <span><?php echo $row['topictitle']; ?></span></h4>
<p><?php echo $row['topictext']; ?></p>
<div class="text-center">
Details
</div>
</div>
<?php
}
?>
</div>
It currently looks like you have something like this:
<div class="card">
<img src="..." />
<?php
foreach($result as $row){
?>
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<?php
}
?>
<button>Details</button>
</div>
If you instead put the foreach loop outside of the card div then it will make a new card for each result, something like this:
<?php
foreach($result as $row){
?>
<div class="card">
<img src="..." />
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<button>Details</button>
</div>
<?php
}
?>
Assuming that your are not using any framework.
In raw php context you could do something like this:
<div>
<?php foreach($arr as $item): ?>
<div>
<img src="...">
<h1><?php echo $item->domain_name; ?></h1>
<h2><?php echo $item->subdomain_name; ?></h2>
<h3><?php echo $item->topic; ?></h3>
<h4><?php echo $item->topic_text_title; ?></h4>
<h5><?php echo $item->text_pf_topic; ?></h5>
</div>
<?php endforeach; ?>
</div>

How to fetch comments from mysql database without destroying my html tags

I want to give a little style for my comment section, here is the code without any css, but i want to give it some style
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "<br>";
echo $row['time'];
echo "</p>";
}
} else {
echo "there are no comments!";
}
?>
</div>
<button>More comments</button>
and down here is my html section of which i want to appear while handling my php comments where USER, COMMENT and TIME are are stored in my database, here is the html, how can i echo the above variables into the below html tags ?
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5>USER</h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li>Reply</li>
</ul>
</div>
</div>
You can do like this:
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
</div>
hope it will help you.

Mysqli query are not fetching database values

I am trying with php 7.0 as everyone recommend me here to start with it as 5.6 has been depreveted . But after writing its code i am unable to fetch the values from database. what wrong i am doing here. thank you
//This is my aboutus page//
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query('select * from about ');
while($row = mysqli_fetch_array($query)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query('select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
//This is my config.php file//
<?php
$connection = mysqli_connect('localhost','root','','dandelion') or die(mysqli_error($connection));
?>
Looking for good suggestion as a learner. thank you
Your About us page should be like this:
<body>
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query($connection, 'select * from about ');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query($connection, 'select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</body>
if your database connection is sucessfully work and problem with fetching data than use number.it may be useful to fetch the data.
<?php echo $row[o]; ?>
<?php echo $row[1]; ?>
instead of
<?php echo $row['about_desc1']; ?>

Trouble with html link tag and image

Got some trouble when i tried to use an url to image.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$query = mysql_query($sql);
if ($query > 0){
?>
<div class="container">
<div class="description-plate">
<?php
while
($row = mysql_fetch_array($query)){
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<!--div class="caption-btm">
<p style="margin-left:6px; margin-top:175px;">Start Airing From:</p>
<h5 style="margin-left:10px;"><?php echo $start; ?></h5>
<p style="margin-left:6px;">Airing Schedule:</p>
<h5 style="margin-left:10px;"><?php echo $schedule; ?></h5>
</div-->
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
So when i tried to call the image using php, the tag only appear on the last image. What i'm trying to do is having the tag on every images. Would appreciate any help, thanks :)
Right now you are going through the loop (not sure why you are using while) and each time creating
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
What you want to do is build up an html string on each pass appending the next image tag, something more like
...
$myimages = '';
while // skipped details
$myimages .= ' <div class="thumbnail-fluid">
<a href=". $row['image'] . '>
<div id="og-plate">
<div><img src="admin/' . $row['image'] . '></div>'
. '</div>
</a>
</div>';
}
Its appear last image because ORDER BY id and the condition status = 'On Going' can return one image. Your html structure should be like this.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$result = mysql_query($sql);
$n = mysql_num_rows($result);
if ($n > 0) {
?>
<div class="container">
<div class="description-plate">
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
</div>
</a>
</div>
<?php } ?>
</div>
</div>
<?php } ?>

Using htmlspecialchars() in php/html the correct and professional way

I would like to know the correct way to use htmlspecialchars()
I have been reading about it and looking at what examples I can find but
I guess its just not registering because I have not been able to apply it my self in my own working example.
Could someone show me how to implement htmlspecialchars() and any other appropriate configuration to make this statement secure and what would be considered professional.
<h3>Recent Post</h3>
<?php
$stmt = $con->query('SELECT * FROM blogData ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time#2x.png" width="100" height="100" alt="Wuno Inc.">
<h6><?php echo $category; ?> - <?php echo $title; ?></h6>
<p><?php echo $content; ?></p>
</div>
</div>
<?php
}
?>
</div>
Is this how it should be done? Or what more could I do to this.
<h3>Recent Post</h3>
<?php
$stmt = $con->query('SELECT * FROM blogData ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time#2x.png" width="100" height="100" alt="Wuno Inc.">
<h6><?php echo htmlspecialchars($category); ?> - <?php echo htmlspecialchars($title); ?></h6>
<p><?php echo htmlspecialchars($content); ?></p>
</div>
</div>
<?php
}
?>
Yes, that is how it should be done. But you could also put it in the code above. Like
$title = htmlspecialchars($row['title']);
$content = htmlspecialchars($row['content']);
$category = htmlspecialchars($row['category']);
This way the variables used between your HTML stay short and readable.

Categories