Issues making a template loops with variables - php

I'm trying to make a simple blog to my site and there is no tutorial of how to make one properly, so i'm trying to make one from scratch. My problem right now is to display a template of posts in a loop with variables. Here are the columns of my table:
Table blog
id
user_id
date
title
content
Code of variables:
<?php
// query //
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
$row = mysql_fetch_array($sql);
// variables //
$user_id = $row['user_id']; //only the id, not the name
$date = $row['date'];
$title = $row['title'];
$content = $row['content'];
// get user_id name //
$sql2 = mysql_query("SELECT name FROM users WHERE id = '$user_id'");
$row2 = mysql_fetch_array($sql2);
$author = $row2['name']; //last variable
?>
What i'm trying to do, is to display them all descendant from id value from the next template vertically:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
How could i loop the template with different variables values in a simple page?

try this:
<?php
// query //
$sql = mysql_query ( "
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC" );
while ( $row = mysql_fetch_array ( $sql ) ) {
// variables //
$user_id = $row ['user_id']; // only the id, not the name
$date = $row ['date'];
$title = $row ['title'];
$content = $row ['content'];
$author = $row ['name']; // last variable
?>
<div id="container">
<div align="center" style="background-color: gray">
<b><?php echo $title;?></b>
</div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
<?php
}
?>
first make one query for info if you have a lot of rows it will eat resources
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC
then you need to loop the query using:
while ( $row = mysql_fetch_array ( $sql ) ) {}

First you need to put your blog table in an array:
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
while($row = mysql_fetch_array($sql)) {
$blog[] = $row;
}
Then, in the template:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php
foreach($blog as $val) {
echo "<h1>".$val['title']."</h1>"M
echo $val['content'];
}
?></div>
</div>
<hr>

$records = mysql_fetch_array($sql);
$blogs_table = '<table> <tr> <th>User id</th> <th>Date</th> <th>Title</th> <th>Content</th></tr>';
foreach($records as $row) {
$blogs_table .= '<tr>';
$blogs_table .= '<td>' . $row ['user_id'] .'</td>';
$blogs_table .= '<td>' . $row ['date'] .'</td>';
$blogs_table .= '<td>' . $row ['title'] .'</td>';
$blogs_table .= '<td>' . $row ['content'] .'</td>';
$blogs_table .= '</tr>';
}
$blogs_table .= '</table>';
At last you can print your $blogs_table table to any where like,
<div align="right"><?php echo $blogs_table ?></div>

Related

link not reading underscore and text after it

I have made one site which get the name from the database and display values from the data base.
Some of my database values having underscore (_) like abc_xyz.dll
But on get method it return only abc not the whole value abc_xyz.dll (skips the underscore and values after it)
Here is two links one having vales without underscore which works http://windllfiles.com/dlldata/SDACQ32MP.dll
Here is another link which have underscore which is not working
http://windllfiles.com/dlldata/ETA_USB.dll
Here is the code
('include/config.php'); $character = '';
if(isset($_GET["character"])) {
$character = $_GET["character"];
if($character=='09'){
$query = "SELECT * FROM tbl_student WHERE student_name LIKE '0%'
or student_name LIKE '1%'
or student_name LIKE '2%'
or student_name LIKE '3%'
or student_name LIKE '4%'
or student_name LIKE '5%'
or student_name LIKE '6%'
or student_name LIKE '7%'
or student_name LIKE '8%'
or student_name LIKE '9%'
";
}else{
$character = preg_replace('#[^a-z]#i', '', $character);
$query = "SELECT * FROM tbl_student WHERE student_name LIKE '$character%'";
}
} else {
$query = "SELECT * FROM tbl_student ORDER BY RAND () LIMIT 30"; } $result = mysqli_query($connect, $query); ?> <?php
require 'include/header.php';
?>
Get Your DLL
file
<?php
$character = range('A', 'Z');
echo ' <ul class="pagination alphabet_pagination">';
echo '<li>0-9</li>';
foreach($character as $alphabet)
{
echo '<li>'.$alphabet.'</li>';
}
echo '</ul>';
?>
</div>
<table class="table table-bordered">
<tr>
<th width="auto">All DLL Files</th>
</tr>
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$name=$row["student_name"]
?>
<tr>
<td><?php echo ''.$name.'';?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3" align="center">Data not Found</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<p> <?php include ('include/ads.php'); ?></p> </div></div>
<div class="col-sm-3">
<?php include('include/right.php');?>
Here is the data file where I am using get method
'include/header.php'; $name = $_GET['name']; ?>
class="container-fluid"> Are you missing
File? <div
class="list-group-item list-group-item-action>
Download '.$name.'
File';?>
<?php
$query = "SELECT * FROM tbl_student WHERE student_name='$name'";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$id=$row["student_phone"]
?>
<li class='list-group-item'><p align="justify"><?php echo $id;?></p></li>
<?php
}
}
?>
<br/> <?php $filename = "file/".$name; $filesize = filesize($filename); $filesize =round($filesize/1024, 2);
echo "File Information:"; echo ""; echo "File
Size:  ".$filesize.''; $md5file =
md5_file($filename); echo "MD5 file
sum:  ".strtoupper($md5file).''; $sha1file =
sha1_file($filename); echo "SHA1 file
sum:  ".strtoupper($sha1file).''; ?> Download '.$name.'
File';?> Dll Missing Error Message:

crud delete not working anymore

i used the crud delete method and it works for me, but when i tried it with a different database and table it keeps saying "Invalid argument supplied for foreach()"
this is my "vacature-verwijderen.php" where you should be able to delete it
<div>
<table id="tabel">
<thead>
<tr>
<th>Functie</th>
<th>Omschrijving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM vacature ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['functie'] . '</td>';
echo '<td>'. $row['omschrijving'] . '</td>';
echo '<td>'. $row['salaris'] . '</td>';
echo '<td width=250>';
echo '<a class="button " href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="button" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
and then when you click delete you go to "delete.php" which looks like this
<?php
require 'database.php';
$id = 0;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( !empty($_POST)) {
$id = $_POST['id'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM vacature WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
Database::disconnect();
header("Location: vacature-verwijderen.php");
}
?>
i don't know what is wrong since it used to work, also my database table looks like this
foreach ($pdo->query($sql) as $row) {
That's what is wrong. $pdo->query($sql) already returns your $row.
Use a while instead so that it do the query and returns you the new row as long as there is data to fetch :
while ($row = $pdo->query($sql)) {
Also :
$sql = "DELETE FROM vacature WHERE id = ?";
The table you provided doesn't contain any field named id, although there is one named vacature_id. You might want to change that too :
$sql = "DELETE FROM vacature WHERE vacature_id = ?";
Don't hesitate to come back if it doesn't solve your issue.

Only the result result from php query displays in the html table, the rest display outside

I'm building a forum for learning purposes. I'm trying without success to retrieve forum categories from the database and displaying them in a table, but only the first category displays in the table, the rest display outside the table. I will post my code, and a screenshot of the image.
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>';
while($row = mysql_fetch_assoc($result)) {
echo '<tbody>';
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>'; //container
}
}
}
include 'footer.php';
?>
https://www.dropbox.com/s/c1dgijuafgv9jzu/Capture.PNG?dl=0
Quite simply you have the closing </table> tag inside the while loop so once the first row is output you close the table. Just move it outside the while loop like this, also the opening <tbody> need moving above the while loop as well
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>
<tbody>'; //<-- moved
while($row = mysql_fetch_assoc($result)) {
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody>'; //<-- moved
echo '</table>'; //<-- moved
echo '</div>'; //container //<-- moved
}
include 'footer.php';
?>
Becuse you're closing the <table> tag within the while-loop. Also, the <tbody> should be outside the loop as well, since there should only be one <tbody> in a table like this.

Can we use table to echo data called by two different queries?

I am selecting user_id from table follow and then calling data from other table members matching that user_id but when I echo out It shows only one member with least user_id. So I thought displaying members in table may help and now my code is like this:
$following =$db->prepare("SELECT user_id FROM follow WHERE uid_fk=:userid order by user_id");
$following->execute(array(':userid'=>$userid));
$row = $following->fetch(PDO::FETCH_ASSOC);
$user_id = $row['user_id'];
$check =$db->prepare("SELECT * FROM members WHERE userid=:user_id");
$check->execute(array(':user_id'=>$user_id));
$row = $check->fetch(PDO::FETCH_ASSOC);
<table>
<tr>
<?php do { ?>
<td>
<div><p><?php echo $row['id']; ?></p></div>
<div><p><?php echo $row['name']; ?></p></div>
<div><p><?php echo $row['about']; ?></p></div>
<div style="height:20px;"></div>
</td>
<?php
$row = $following->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%2==0) {
echo "</tr><tr>";
}
} while ($row);
?>
</table>
As you can see my table is only for $following and so now it displays all members $user_id in table but how to add second query to table code i.e, $check so that I can also echo out info about members in table. I don't want to store member info in follow table any other way to do that?
Actually I was omitting where attribute so this is how we could call data from two tables and show the result in table.
$sql = "SELECT * FROM members INNER JOIN follow ON follow.user_id=members.userid where user_id=$userid ORDER BY userid "; $query = $db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
<table>
<tr>
<?php do { ?>
<td>
<div><p><?php echo $row['id']; ?></p></div>
<div><p><?php echo $row['name']; ?></p></div>
<div><p><?php echo $row['about']; ?></p></div>
<div style="height:20px;"></div>
</td>
<?php
$row = $following->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%2==0) {
echo "</tr><tr>";
}
} while ($row);
?>
</table>

MySQL/PHP News System

I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.
The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.
**MySQL Information**
id INT AUTO_INCREMENT,
author VARCHAR(xxx),
title VARCHAR(xxx),
message TEXT,
date TEXT,
time TEXT,
PRIMARY KEY(id)
This is the insertion page (news_center.php)
<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
<tr>
<td>Title</td>
<td><input style="width:100%;" type='text' name='news_title' /></td>
</tr>
<tr>
<td height="57">Message</td>
<td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
This is news_center_ac.php
<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$author == $_SESSION['name'];
if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
{
$date = date( 'jS F Y' );
$time = date( 'H:i' );
$query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
$insert = mysql_query($query) or die(mysql_error());
echo '<p>Successful News Update “'.$_POST['news_title'].'”';
}
else
{
echo '<p>Please fill in all fields</p>';
}
}
?>
This is the Output on the news page (/news/index.php)
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>
*CONTENT*
<?php
foreach ($output as $value) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
I just want it to output each news article in turn i can sort out the formatting later once it works.
You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
EDIT Added htmlentities() calls to convert html special characters
while ($row = mysql_fetch_array($news)) {
echo "<p> “" . htmlentities($row['content']);
echo "”";
echo "Posted:" . htmlentities($row['date']);
echo " " . htmlentities($row['time']);
}
Rewrite it this way:
<?php
while($output = mysql_fetch_array($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
When you call output first, it is only returning one value, this will loop through all.
try /news/index.php
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
while($output = mysql_fetch_assoc($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
Shouldn't the last foreach loop use $value, ie
<?php
foreach ($output as $value) {
echo "<p> “" .$value['content'];
echo "”";
echo "Posted:" .$value['date'];
echo " " .$value['time'];
}
?>

Categories