Alright, I can't bash my head at this anymore tonight. Thought I would pose the question. Maybe there's a simple answer I have not come across yet.
I have a number of related tables in PHPMyAdmin: pubtopics, pubtypes, and pubtitles.
Pubtitles is the main table (table 1), pubtopics is a simple list assigning a number to a topic (table 2), and pubtypes is another simple list assigning a number to a publication type (table 3).
I'm trying to create a page that lists every publication by type (list of all pubs in table 3), but also shows the multiple topics (table 2) that may be assigned to a unique pubtitle (table 1), and have each of those topics (table 2) link to publications listed by that topic.
The data for pubtopics looks something like this:
pubtopics
What I'm looking for is a line like this:
June 15, 2016 | Comparative Data Report | Education | Fiscal Affairs
Where the date is a link, the pub type is a link, and each of the concatenated issues is a unique link.
When I try to list all the publications for a specific publication type, all I can generate so far is either a list with multiple duplicate entries (because one may address several topics). I've tried group_concat and group by, but that doesn't allow me to have an individual link for each topic.
I've played around with explode and arrays, but it's a bit beyond my know-how. Any brilliant solutions?
Code looks like:
elseif (isset($_GET['type'])) {
$var3_getDisplay4 = $_GET['type'];
$query_getDisplay = sprintf("SELECT
DATE_FORMAT(publicationsnew.date, '%%Y') AS archive,
DATE_FORMAT(publicationsnew.date, '%%M %%e, %%Y') AS pubdate,
pub_type.number AS typelink,
pub_type.type AS pubtype,
GROUP_CONCAT(categories.category_name ORDER BY categories.category_name SEPARATOR ' | ') AS category,
categories.category_id AS catlink,
publicationsnew.title,
publicationsnew.author,
publicationsnew.imagelink,
publicationsnew.description
FROM
pub_cat
LEFT JOIN publicationsnew ON (pub_cat.pub_id = publicationsnew.pub_id)
LEFT JOIN categories ON (pub_cat.category_id = categories.category_id)
LEFT JOIN pub_type ON (publicationsnew.type = pub_type.number)
WHERE pub_type.number = %s
GROUP BY publicationsnew.pub_id
ORDER BY publicationsnew.date DESC",
GetSQLValueString($var3_getDisplay4, "text"));
and in the body:
<?php do { ?>
<h2><?php echo $row_getDisplay['pubdate']; ?> | <?php echo $row_getDisplay['pubtype']; ?> |
<a href="test.php?topic=<?php echo $row_getDisplay['catlink']; ?>">
<?php echo $row_getDisplay['category']; ?>
</a>
</h2>
<p class="pubtitle"><?php echo $row_getDisplay['title']; ?></p>
<?php echo ($row_getDisplay['author']); ?>
<?php echo ($row_getDisplay['imagelink']); ?>
<div class="pubdescription"><?php echo ($row_getDisplay['description']); ?></div>
<div class="clear"></div>
<hr>
<?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?>
Burned out for the day. To be continued...
Related
I am making an extremely basic posting system, and I cant seem to figure out how to get the most recent rows from a certain table. I have tried other solutions offered here, but my posts were randomly placed. How would I accomplish this? My code is below.
function load_posts(){
$posts_sql = "SELECT * FROM posts";
$posts_result = Phoenix\Database\Database::$database->query($posts_sql);
while($row = $posts_result->fetch_assoc()){
$posts_display = '
<div class = "card" style = "width:500px">
<div class = "card-body">
<div class = "card-title">'. $row['username'] .'</div>
<p>'. $row['content'] .'</p>
</div>
</div>
';
echo $posts_display;
}
}
Again, I want the posts to be displayed from most recent, to old.
You need to have information in each row that captures this information. The most common suspects are:
an auto-incrementing id
a creation date
Then you just ask the database to sort the results. For instance, if post_id is an auto-incremented id:
select p.*
from posts p
order by p.post_id;
SELECT * FROM TableName ORDER BY id DESC
// order by should be with primary key
I am building website like social network.
And i made 3 tables: users, posts, and friends.
How can I write code to display posts from me and from my friends:
Users: id, email, username, password, image
Posts: id, pid, pname, ppost, pimage
Friends: id, user1, user2, friends(yes or no)
So i need to check if user1 is $_SESSION[user] , than too check every row with friends as yes and than echo every post from user1(me) and other users who are friend of mine.
I made some code but it echo posts only from me and first user i marked as friend, i can't show posts from other users i marked as friend.
Users: id:1 email:email#email.com, username:test, password:xd , image:This is link
Users: id:2 , email: test#email.com, username:test2, password:xd, image:Link
Users: id:3 , email: test2#email.com, username:test3, password:xd, image:Link
Posts: id:1 , pid(this is publisher id):etc 1,2,3 , pname:Name of publisher , ppost:Text , pimage: image of publisher:
So just imagine i have 3 posts from 3 diferent users
Friends: id:(auto)1,2,3,4 , user1(user1 is session user) if i login to first user it will be 1, user2(id of other user like 2(test2)), friends(yes)
Friends: id:(auto)1,2,3,4 , user1(user1 is session user) if i login to first user it will be 1, user2(id of other user like 3(test3)), friends(yes)
So i need to echo posts from users(me) , and two others and every other user which have friends(yes) , and don't show posts from users which are marked no.
I made this so far: Querys:
`session_start();
$res=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['user']);
$row=mysql_fetch_array($res);
$res2=mysql_query("SELECT * FROM posts WHERE pid=".$_SESSION['user']);
$row2=mysql_fetch_array($res2);
$res3=mysql_query("SELECT * FROM obavijesti");
$row3=mysql_fetch_array($res3);
$res5=mysql_query("SELECT user1, user2 FROM osobe WHERE friends='yes' AND user1='$_SESSION[user]' order by id limit 5000");
$row5=mysql_fetch_array($res5);
$user2=$row5['user2'];
$user1=$_SESSION['user'];
`
PHP script:
<?php $result = mysql_query("SELECT * FROM posts WHERE pid='$user1' OR pid='$user2' order by id desc limit 5000") ?>
<div class="publishcontent">
<link rel="stylesheet" type="text/css" href="testt.css">
<div class="posts">
<article class="post">
<header class="post-header"><span class="post-user-name"><?php echo $row4['pname']; ?></span><span class="post-header-posted">Objavljeno: Danas</span></header>
<div class="post-flex-container">
<aside class="post-user">
<image src="<?php echo $row4['pimage']; ?>" class="post-user-image"></image>
<div class="post-user-status is-online">TEST</div>
<div class="post-user-posts">Broj objava: x</div>
<div class="post-user-joined"><?php if($row['spol'] == "zensko" ) {
echo "Pridružila se";
}
else {
echo "Pridružio se";
} ?> : <?php echo $row['date']; ?></div>
</aside>
<section class="post-content">
<p>
<?php echo htmlspecialchars($row4['ppost']); ?>
</p>
</section>
</div>
</article>
</div>
</div>
<?php endwhile ?>
And it only displays posts from me and first person i marked as friend, but don't show posts from third person
I would go back to your mysql and refactor your database schema. Your Users table will need to have a one to many relationship with user_id in your Friends table. And your Users table will also need to have a one to many relationship with friend_id in the Friends table. (this allows you to store the user_id of the friend and the user_id of the befriended.) The users table will also have a one to many relationship with the Posts table. Then Change Your mysql to use an inner join query. The inner join query will allow you to query all the results from each table with one query. Here is a link to learn more about that. http://www.w3schools.com/sql/sql_join_inner.asp
SELECT users.*, friends.*, posts.* FROM users INNER JOIN friends ON users.id = friends.user_id INNER JOIN posts ON users.id = posts.user_id
Also you can add a WHERE clause at the end of the query to get related posts and friends for the current logged in user
Afternoon All,
Looking for some direction with listing posts by category.
I have begun coding a CMS (as a PHP beginner) and up until now all was going well.
I have made a link to pre-set categories (Category 1, Category 2 etc etc)
I also have a categories table with ID and Category name.
A table for users posts with CatID and category name in it as well
I'm guessing I would need to join tables to be able to list any posts in specific category (Select Cat 1 to see all Cat 1 posts. Same for Cat 2, 3 etc etc)
When a user adds a post it fills the category name in users posts table but I get no CAT ID and nothing added into categories table so how do i call on this to display categorised posts?
I have a feeling I am probably thinking to much into things and over-complicating what should probably be simple to do.
Ihe code i have at the moment (see below) has no effect at all?
Please help point me in the right direction, I have tried everything.
Many thanks to all in advance
CODE:
$catSql ="SELECT ID, Category
FROM categories
LEFT JOIN users_posts
ON CatID, category, BlogID";
$catQry = mysqli_query($link, $catSql);
while ($row = mysqli_fetch_assoc($catQry)){
if($row['category_name'] != $lastCategory)
{
$lastCategory = $row['category'];
echo "<br /><strong>$lastCategory</strong>";
}
echo $row['category'] . ' <br />';
}
Your SQL is wrong I think this should work
$catSql = "SELECT *
FROM categories
LEFT JOIN users_posts
ON categories.ID = users_posts.CatID";
Your SQL for listing posts could check to see if a categoryID exists in the URL, and if it does, use it to filter the results.. Then, you would just need to create some links to the same page to add the required category id to the URL.
<a href='?cat=1'>Cat 1</a> | <a href='?cat=2'>Cat 2</a> | etc..
SQL
$sql = "SELECT * FROM POSTS";
if(isset($_GET['cat'])){
$catID = (int)$_GET['cat']; //or something similar
$sql .= " LEFT JOIN category USING categoryID WHERE categoryID = $catID";
}
I have 2 mysql tables named
blog
comment
What i am trying to do to retrieve following columns
user_name comment_desc from the comment table using blog_id.
Both the tables have blog_id in common.
here below are the screenshots of both sql tables
Comment Table:
Blog Table:
I tried the query this way.
SELECT blog.blog_id, comment.user_name, comment.comment_desc
FROM
blog (b)
INNER JOIN
comment (c)
ON b.blog_id = c.blog_id
i dont have access to upload the image thats why i uploaded to photobucket.
The PHP Code....
<?php
$comments_set = blog_comments();
var_dump($comments_set);
while($comments_all = mysql_fetch_assoc($comments_set)){
$name = $comments_all['user_name'];
$desc = $comments_all['comment_desc'];
echo
"<ol class=\"commentlist clearfix\">
<li class=\"comment even thread-even depth-1\" id=\"li-comment-1\">
<div id=\"comment-1\" class=\"comment-wrap clearfix\">
<div class=\"comment-meta\">
<div class=\"comment-author vcard\">
<span class=\"comment-avatar clearfix\">
<img alt='' src='http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=60' class='avatar avatar-60 photo avatar-default' height='60' width='60' /></span>
</div>
</div>
<div class=\"comment-content clearfix\">
<div class=\"comment-author\">$name<span>January 24, 2013 at 10:46 am · <a class='comment-reply-link' href=\"#\">Reply</a></span></div>
<p>$desc</p>
</div>
</div>
</li>
</ol>
";
}
?>
You need user_name and desc from comment table with blog_ig
Why you need to join these 2 tables then ? you could retrieve information from comment table by blog id
select * from comments where blog_id='blog_id'
You are using alias so at the time of SELECT statement, specify it through alias name. You need to remove parenthesis around alias name...
SELECT b.blog_id, c.user_name, c.comment_desc
FROM
blog b
INNER JOIN
comment c
ON b.blog_id = c.blog_id
I think you have incorrectly define the blog_id columns. Please check columns' datatype and sizes.
It will fail when joining tables.
If you can paste error it will be more helpful
Thanks.
I think you should go for Left Join because may be you want to have blogs to be displayed whether they have comments or not.
SELECT b.blog_id, c.user_name, c.comment_desc
FROM
blog as b
LEFT JOIN
comment as c
ON b.blog_id = c.blog_id
So I have two tables, article and comments (which has one-to-many relationship (1 article - many comments)). This is how the table is structured:
Articles - id (prikey), title, publicationDate, content
Comments - com_id (prikey), author, comment, id (foreign key)
I used this to query the two tables:
SELECT * FROM articles as a INNER JOIN comments as c ON a.id = c.id
Previously, I was only displaying the articles table using this:
<?php
while($row = mysqli_fetch_array($query)) {
echo "
<div id='article'>
<header>
<hgroup>
<h2>".$row['title']."</h2>
<h4>Posted on ".$row['publicationDate']."</h4>
</hgroup>
</header><p>".$row['content']."</p></div>";
}
?>
This displays all articles (with date, title, content, etc.). Now there are comments. How do I edit the php code (or if my query is incorrect, how to write the query), so that it shows all articles and all comments per article as in:
Article One
-- Comment 1
-- Comment 2, etc.
Article Two
-- Comment 1
-- Comment 2, etc.
An alternative would be to split the query into two.
The first would bring back the articles you want...
SELECT * FROM article;
Once you have those, you can get all the IDs and use something like the following
SELECT * FROM comments WHERE article_id IN (".$list.");
This restricts the MySQL queries to 2 whilst getting all the data you need. After this loop around the article data, and in that loop, loop around the comments data.
This also means that, unlike using GROUP_CONCAT, you will also have author data to use.
It's not a very eloquent solution, but should work.
Query:
SELECT c.author, c.comment,
a.id article_id, a.title, a.publicationDate, a.content
FROM comments c
RIGHT JOIN articles a
ON c.id = a.id
PHP:
<?php
$lastArticleId = 0;
$isNewArticle = false;
while($row = mysqli_fetch_array($query)) {
$isNewArticle = ($lastArticleId != $row['article_id']) ? true : false;
if($isNewArticle) {
$lastArticleId = $row['article_id']; ?>
<div class="article">
<header>
<hgroup>
<h2><?php echo $row['title']; ?></h2>
<h4>Posted on <?php echo $row['publicationDate']; ?></h4>
</hgroup>
</header>
<p><?php echo $row['content']; ?></p>
</div>
<?php
}
if($row['comment'] != '') { ?>
<p><strong><?php echo $row['author']; ?></strong> - <?php echo $row['comment']; ?></p>
<?php
} ?>
<?php
} ?>
Use something like
SELECT a.article
,GROUP_CONCAT(CONCAT('<p>', c.comment, '</p>') SEPARATOR "\n") as comments
FROM
article a
INNER JOIN comment c ON (c.article_id = a.id)
WHERE a.id = '12454';
You may have to fiddle a bit with the separator.
See: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Do note however:
The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:
SET [GLOBAL | SESSION] group_concat_max_len = val;
See here how to change max_allowed_packet
http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_max_allowed_packet
Look into MySQL GROUP_CONCAT which will return a comma delimited list of items. You can then explode that for your comments section.
once a person will comment on a article insert article id with that comment and later get them accordingly something like this
once a person will select an article to read send article id in the $_GET to your article page so you can excess the article id.Once a person will comment on that article insert it as follows
$sql = mysql_query("INSERT INTO comments_table (subject,article_id,comments) VALUES ('$subject','$_GET['id']','$comments')");
and later when you pulling them do it the same way as you have the article id in the $_GET
you can access it run a query like this
$fetch = mysql_query("SELECT * FROM comments WHERE article_id = $_GET['id'] ORDER BY id DESC") or die(mysql_error());
Hope this will work