displaying only relevant posts - php

I have a website that is made in php and mysql.
It is a podcast site which I have created.
The home page has a list of podcasts and once one is clicked it then brings up the episode.php?id= followed by the ID that is listed in mysql for that podcast.
at the bottom of the episodes page I have added a comment box.
and I have it to display the comments saved in mysql using:
<?php class feedback {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM comments");
$query->execute();
return $query->fetchAll();
} }
$feedback = new feedback;
$articles = $feedback->fetch_all();
?>
<html>
<body>
<?php foreach ($articles as $feedback) { ?>
<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } ?>
</html>
</body>
This displays all the comments that are listed in the comments field in mysql.
each comment has a "cast" tab which displays the id of the podcast.
How can I get this to reflect the page being viewed?
for example.
if I'm viewing episode.php?id=1 then I want the comments with the "cast" tab of "1" to be displayed and not the "cast" tab of "2". Also the same goes for episode.php?id=2. and so on!
Please can someone guide me on how to do this?
thank you.
Kev

so I had a little play around with this after trying Timo Dörsching's suggestion which did not work.
I hit undo to get it back to my original post and changed this:
<?php foreach ($articles as $feedback) { ?>
<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } ?>
to this:
<?php foreach ($articles as $feedback) {
if ($feedback['cast'] === $_GET['id']) { ?>
<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } } ?>
this does the job perfectly.

perhaps you have to filter the $_GET['id'] & $cast.... but this easy to google how to do this...
Have a look at: How can I prevent SQL injection in PHP?
$cast = $_GET['id'];
public function fetch_all($cast){
global $pdo;
$query = $pdo->prepare("SELECT * FROM comments WHERE cast/id/... = ".$cast."");
$query->execute();
return $query->fetchAll();
} }
$feedback = new feedback;
$articles = $feedback->fetch_all();

Related

Displaying content but not the latest one

I have these codes to display content on my website.
Index.php
$rest = new rest;
$list = $rest->fetch_all();
<?php foreach ($rest->fetch_all() as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
And include.php
class rest {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM podcast ORDER BY cast_id DESC");
$query->execute();
return $query->fetchAll();
} }
Please can someone tell me how I can get this to show results but not the latest result?
All fields are numbered using an ID tag in mysql.
On my site I have the latest entry listed above this list so I do not require the latest one appearing here.
Please help.
Thank you.
The easiest way to do this is to discard the row in the application. You can do it in SQL by using a calculated limit. Calculate it as (SELECT COUNT(*) FROM podcast) - 1.
Or:
SELECT *
FROM podcast
EXCEPT
SELECT TOP 1 *
FROM podcast
ORDER BY cast_id ASC
Excluding the last row.
You can either use array_shift or array_pop depending on your query sorting as shown bellow:
Assuming the latest result is the first raw on your query result.
$rest = new rest;
$list = $rest->fetch_all();
$latest = array_shift($list);
<?php foreach ($list as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
If it's the last raw that you need to hide, then use array_pop

Display wrong information with the good row value

I have a problem with the code below. In fact, it's supposed to show the information of a table, where the MomentEvent value match with the row requested. The only problem, is that sometimes, it show the information of a complete diffrent row! And I don't have any idea why! Does the problem come from my code?
And in my table, the information are placed in order, so they are always added one after an other.
<?php
include('base.php');
?>
<?php
if(isset($_GET['MomentEvent']))
{
$MomentEvent = intval($_GET['MomentEvent']);
$dn = mysql_query("select Ordre, Confidentialite, ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent=$MomentEvent");
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['TitreEvent']); ?>" :
<table style="width:500px;">
<tr><td>
<?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
echo 'This user dont have an avatar.';
}
?>
</td>
<td class="left"><h1><?php echo ($dnn['TitreEvent']); ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</tr>
</table>
<?php
}
else
{
echo 'Sorry, any event found';
}
}
else
{
echo 'The user ID is not defined.';
}
?>
</body>
</html>
you have missed </td> in your code.
and use echo $dnn['TitreEvent']; without parenthesis
try this
<td class="left"><h1><?php echo $dnn['TitreEvent']; ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</td>
^^----you missed this
EDIT:
Ithink you have many result returned from your sql and you need to specify more in your query .
like that
where MomentEvent=$MomentEvent AND user = $theUser
Now all works fine with these changes:
$MomentEvent = $_GET['MomentEvent'];
$sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent='$MomentEvent'";
$dn = mysql_query($sql);

displaying accurate results

I'm creating a web site directory for my mobile site (FOUND HERE)
I have figured out how to display listings from my mysql table to my home page from my tables promo_cat list.
The thing im having trouble with is this:
once clicking on one of the catagories it leads me to my list.php page.
How do I get this page to display results related to the category clicked and not others?
For example:when clicking on "FREE" brings up this page: http://www.xclo.mobi/xclo2/list.php?id=FREE. Which displays all results. it should only display results that have a promo_cat field as "FREE" and should not display any other results as it does currently.
My list.php code:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>xclo mobi</title>
<link rel="stylesheet" href="other.css" />
</head>
<body>
<?php include_once('header.html'); ?>
<div class="container">
Category = ???
<ol>
<?php foreach ($articles as $article) { ?>
<div class="border">
<a href="single.php?id=<?php echo $article['promo_title']; ?>" style="text-decoration: none">
<img src="<?php echo $article['promo_image']; ?>" border="0" class="img" align="left"><br />
<img alt="" title="" src="GO.png" height="50" width="50" align="right" />
<font class="title"><em><center><?php echo $article['promo_title']; ?></center></em></font>
<br /><br />
<font class="content"><em><center><?php echo $article['promo_content']; ?></center></em></font>
</div><br/><br />
</a>
<?php } ?>
</ol>
</div>
</body>
</html>
/include/article.php
<?php
class article {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_title) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi WHERE promo_title = ?");
$query->bindValue(1, $promo_title);
$query->execute();
return $query->fetch();
}
}
?>
You need to make changes to the code for list.php based on the input it gets through GET parameter. something like:
if ($_GET['id'] == 'FREE'){
// do something like display FREE items
}
elseif($_GET['id'] == 'GIFT') {
// display GIFT items
}
else {
// perform some default action
}
This is to make it even more database driven (helpful when there are many categories):
$sql = "select * from categories where id = '".$_GET['id']."'";
if (mysql_results($sql)){
// do something
}
else {
// show error
}
Note that this is for demo only and in your code you should use PDO/MySQLI and prepared statements and not mysql_results function.
In light of more information provided by OP:
Change this
$articles = $article->fetch_all();
to
$articles = $article->fetch_data($_GET['id']);
in list.php and see if you get correct results.
Based on the code you provided, try this:
<?php foreach ($articles as $article) {
if ($article['promo_cat'] === 'FREE') { ?>
// Keep the rest of the code
//instead of <?php } ?> - put...
<?php } } ?>
Keep in mind, this is messy. But the foreach statement (I imagine) is being used to print out all posts. So, before printing out a post, you just check to see if the promo_title is FREE, GIFT, etc. If it's not, then it doesn't print that item.
You can make this more dynamic by passing in a $_GET variable (which you apparently are doing, but the code is never using this variable) with the current promo title and altering the conditional line to say
if ($article['promo_cat'] === $_GET['id'])
Hope that helps!

Few examples with HTML SIMPLE DOM

i have some problems with html simple dom and dont know how to get some specific data, i read manual and try by my self, but it looks i miss something so hope somebody can help me.
1th problem:
HTML:
<div>
<h4>Režie:</h4>
<span data-truncate="60">
Ridley Scott
</span>
</div>
<div>
<h4>Scénář:</h4>
<span data-truncate="60">
William Monahan
</span>
</div>
<div>
<h4>Kamera:</h4>
<span data-truncate="60">
John Mathieson
</span>
</div>
<div>
<h4>Hudba:</h4>
<span data-truncate="60">
Harry Gregson-Williams
</span>
</div>
My PHP code:
$ret = $html->find('span[data-truncate*="60"]'); //rezia
foreach ($ret as $rezia) {
echo "rezia <br/>";
}
But this code print just name and a href from all of this name, and what i need is just name which is under "REŽIE"(Ridley Scott) and "Scénář" (William Monahan)
2th Problem
HTML:
<div id="rating">
<h2 class="average">71%</h2>
<p class="charts">
PHP code:
$percenta = $html->find('h2[class*="average"]'); //pocet ˇ%
foreach ($percenta as $hodnotenie) {
echo "$hodnotenie";
}
What i get from this is 71% and i want just number, not that HTML around, is it possible?
3th problem (the last one:P):
HTML:
<table>
<tr>
<th>
V kinech ČR
od:
</th>
<td class="date">
06.05.2005
</td>
</tr>
<tr>
<th>
V kinech SR
od:
</th>
<td class="date">
05.05.2005
</td>
</tr>
<tr class="separator">
<th>
Na DVD
od:
</th>
<td class="date">
01.10.2005 Bonton
</td>
</tr>
PHP code:
$ret = $html->find('td[class="date"]');
$kino = array();
foreach ($ret as $kino) {
$datum[] = $datum->innertext;
}
echo "$datum[0]";
I get not output from this and i have no idea whats wrong on my code. I just want to get that DATEs (so should be 06.05.2005, 05.05.2005, 01.10.2005)
You didn't load the html, look at this
$html = str_get_html('Some text bla bla bla bla<br /><b>Date</b>: 2012-12-13<br /><br /><b>Name</b>: Peter Novak<br /><b>Hobby</b>: books,cinema,facebook');
foreach($html->find('text') as $t){
if(substr($t, 0, 1)==':')
{
// do whatever you want
echo substr($t, 1).'<br />';
}
}
Output will be
2012-12-13
Peter Novak
books,cinema,facebook
Also, check this one to load a remote site's content
$html = file_get_html('http://heera.it');
// Find all article blocks
foreach($html->find('div.post-entry') as $article) {
echo $article->find('div.post-entry-content h2 a', 0) . '<br />';
echo $article->find('div.post-entry-content p', 0)->plaintext. '<br />';
echo "<hr />";
}
The result will be

Echoing forum Id to new page to display content?

can someone please help me i am having problems creating my forum.
At the moment users can create posts, the post title is listed down the page and then the user is suppose to be able to click the title link and be taken to read_post.php and then this should take the user to another page where the post content can be viewed, i am trying to do this by echoing the forum post id but it doesnt seem to want to work, instead i get this error:
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
can someonee please show me where im going wrong.
here is my sql function:
function read_forum() {
global $connection;
global $forum_id;
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = $forum_id ";
$forum_set = mysql_query($query, $connection);
confirm_query($forum_set);
return $forum_set;
}
here is the link code that takes the user to read_post.php which suppose to echo the forum id and display the content for each individual post.
<?
$forum_set = get_forum();
while ($forum = mysql_fetch_array($forum_set)) {
?>
<div class="forumcase" id="forumcase">
<div class="pend-forum-content">
<?php echo "<strong>{$forum['title']}</strong> - Posted by {$user['first_name']}"; ?>
</div>
here's my code for read_post.php:
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['frm'])) {
$forum_id = $_GET['frm'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<div class="modtitle">
<div class="modtitle-text">Messages Between <?php echo "{$forum['display_name']}"; ?> & You</div>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $inbox['date_sent']; ?>
<?php
$forum_set = read_forum();
while ($forum = mysql_fetch_array($forum_set)) {
$prof_photo = "data/photos/{$message['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum_id'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($message['date_sent'])).""; ?></div>
<div class="img-with-border-msg-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<div class="conversation-text">
<?php echo "<i>Conversations between you and </i>{$forum['display_name']}.<br /> "; ?></div>
<div class="message-content">
<?php echo "<strong>Message Subject: </strong><i>{$forum['subject']}</i>"; ?>
<br/>
<br/>
<br/>
<br/>
<?php echo "<strong>Message:<br/></strong></br ><i>{$forum['content']}</i>"; ?>
</div>
<div class="reply-box">
<? include ('message_reply.php'); ?>
</div>
<?php
}
?>
<br/>
<br/>
<br/>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>
You have an error in your query... Your parameter is not quoted...
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = '$forum_id'";
However... I suggest that you refrain from using the mysql_ family of functions. They are deprecated and due to be removed from PHP in a future release. You should be using parameterized queries using MySQLi or PDO.
Also, global is evil. I've never had a need to use it in 10 years of PHP programming. Neither should you.

Categories