Php question about news menu plugin that may not be displayed - php

I am not realy a php programmer myself, so any help would be appreciated.
I run a website on cms e107.
Now I have installed a menu plugin called "Recent news menu"
This will display the latest news articles in the selected menu area on the site.
Now my problem is that it also displays news that is set to "Not display" (see picture)
Can anyone help how to get this not displayed?
Here is all the code:
<?php
global $sql2, $tp;
$caption = "Recent news";
$no_news = "No news items";
$eol_separator = "</td></tr>";
$sol_separator = "<tr><td style='width:0%;text-align:left;'>";
$qry = "SELECT news_id, news_title FROM #news WHERE news_render_type = 0 ORDER BY news_id DESC LIMIT 0,5";
if($sql2->db_Select_gen($qry))
{
$n_text = "<table style='width:100%;'>";
while ($row = $sql2->db_Fetch())
{
$title = $tp->toHTML($row['news_title']);
$n_text .=$sol_separator ."<a href='".e_HTTP."news.php?item.".$row['news_id']."'>".$title."</a>".$eol_separator;
}
$n_text .= "</table>";
}
else
{
$n_text = $no_news;
}
$ns->tablerender($caption, $n_text);

Since the code of this plugin doesn't look very good in general, I guess you might not want to use this plugin and look for a better one... but in case you want to use this plugin you could adjust the db query so that the items, which are not ment to be displayed are not selected. Something like this:
$qry = "SELECT news_id, news_title
FROM #news
WHERE news_render_type = 0
AND display = 1
ORDER BY news_id DESC
LIMIT 0,5";
Depending on how the display information is stored, you might need to make it AND display > 0.

Related

Next and previous buttons

I know there are libraries etc that I could use to get this sorted but Im almost there with my code.
A little about the code and what it's trying to do. I have a mysql table where there are various news articles and grouped in categories of news.
I have managed to get a forward button working. So it looks for the next news article that is in the same category. This works and the code is below.
//Gets the next story from the same story type in line.
$query= "SELECT * FROM news WHERE storytype2 = '$storytype2' AND id > '$currentid'";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0){
echo "<td width=\"20%\"align=\"right\"><img title=\"Next News\" src=\"webImg/forwardarrow.png\"/></td></tr>";
}else{
echo "<td width=\"20%\"align=\"right\"></td></tr>";
}
//End of the next button
However, when I try do the same for the previous button. All I ever seem to get back is the first id of that category regardless of where my iteration is. For example, if I am on news article 10 and try to go to previous one which say has an id of 7 it will automatically show the first news article within that category, say id 4.
Below is the code.
//Gets the next story from the same story type in line.
$query= "SELECT * FROM news WHERE storytype2 = '$storytype2' AND id < '$currentid'";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0){
echo "<td width=\"20%\"align=\"left\"><img title=\"Previous News\" src=\"webImg/backarrow.png\"/></td>";
}else{
echo "<td width=\"20%\"align=\"left\"></td>";
}
//End of the next button
What have I done wrong?
Thanks
Neither of your queries is correct. Your "Next" code selects any row whose ID is higher than the current, not necessarily the next one; if you get the next one, it's just by accident.
You should use ORDER BY and LIMIT to control which row is selected:
Next:
SELECT *
FROM news
WHERE storytype2 = '$storytype2' AND id > '$currentid'
ORDER BY id
LIMIT 1
Previous:
SELECT *
FROM news
WHERE storytype2 = '$storytype2' AND id < '$currentid'
ORDER BY id DESC
LIMIT 1
Without any further information, I don't think you can assume that the first row of your queries will be the ID you're looking for. Ordering by ID first will probably solve your problem; you can also limit your query to one row, since it's the only one you're looking at. Something like the following would probably solve your problem (where x is $storytype2 and y is $currentid:
SELECT * FROM news
WHERE storytype2 = x
AND id < y
ORDER BY id DESC /* <-- THIS */
LIMIT 1
Use ORDER BY id ASC for the other case.
Note that the MySQL family of PHP is deprecated and support thereof will disappear, if it hasn't yet. Please look into PDO or MySQLi.
Note also that you are inserting a variable into SQL code directly, which is never a good idea. I hope you have some good input checks on your variables.
Let's look at the PDO way to get the previous article ID:
$dbh = new PDO(..);
// Use ? where dynamic input will come
$sql = $dbh->prepare('SELECT * FROM news
WHERE storytype2 = ?
AND id < ?
ORDER BY id DESC
LIMIT 1');
// Fill the ? safely with PDO's execute function
$sql->execute(array($storytype2, $currentid));
$result = $sql->fetch(PDO::FETCH_ASSOC);
if($result && isset($result['id'])) {
// Process previous ID
}

Displaying links for similar articles

I have a page where I want to display articles like the one you're reading (randomly chosen articles from same subcategory). I want to use a php script but the server says I have an error. Here is my script:
$article = mysqli_query($con,"SELECT * FROM sources WHERE ID = '$ID'");
while($row = mysqli_fetch_array($article))
{
code which works perfectly
$samecat = $row['Subcategory'];
}
$samecats = explode(', ', $samecat);
foreach($samecats as $similar){
$scat[] = "Subcategory LIKE %".$similar."%";
}
echo implode(' OR ',$scat);
$samearticle = mysqli_query($con,
"SELECT *
FROM sources
WHERE (".implode(' OR ',$scat).")
AND NOT ID='$ID'
ORDER BY Rand()
LIMIT 0,3 ");
while($row2 = mysqli_fetch_array($samearticle))
{
echo "<a href='article.php?ID=".$row2['ID']."'>&raquo "
.$row2['Headline']."</a>";
}
The connection works perfectly because it works with other components but I have bug here :(((
Any alternative solutions will be fine, but I think this way is better.
error is:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Here is your problem:
https://eval.in/96729
I simulated your code and viewed the sql you're generating. It's like this:
"SELECT *
FROM sources
WHERE (Subcategory LIKE %test% OR Subcategory LIKE %foo% OR Subcategory LIKE %bar% OR Subcategory LIKE %baz%)
AND NOT ID=''
ORDER BY Rand()
LIMIT 0,3 "
You need singlequotes around your terms like:
Subcategory LIKE '%test%'
So you need to:
$scat[] = "Subcategory LIKE '%".$similar."%'";
Really you should enable error reporting for your querying.

How to show 5 post each page with next & previous button feature?

I have a page named 'job.php', currently this page is showing all posted job. But now I want to show only 5 latest posts. And if anyone want to check the previous posts, they can click next button thus more 5 posts will be seen. There should be a previous button too.
Following is my code:
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC");
$num_row = mysql_num_rows($result1);
while($row1 = mysql_fetch_array($result1)){
$cat=$row1['Category'];
$title=$row1['Title'];
echo "Job field: $cat<br/> Title: $title<br/>";
}
N:B: It's not pagination. I don't want to show page numbers, just want to show next & previous button.
There are 100s of articles available on the Internet
Create Awesome PHP/MYSQL Pagination
PHP / MySQL select data and split on pages
If you want to do on your own:
Use LIMIT keywords in your query.
Pass the page and the multiplier to the LIMIT.
Some code
<?php
$limit = 5;
$start = (int)(($page - 1 ) * $limit);
$page = mysql_real_escape_string($_GET["page"]);
$query = "SELECT * FROM `table` LIMIT $start, {(int)$page + $limit}"
?>
There are two ways to achieve this.
1) In the query itself by using LIMIT
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 1, 5");
2 ) By using loop
$i = 0;
while($row1 = mysql_fetch_array($result1)){
if($i < 5) {
$cat=$row1['Category'];
$title=$row1['Title'];
echo "Job field: $cat<br/> Title: $title<br/>";
$i++;
}
}
You can pass the current value to URL and get it back by using $_GET..
You can use this query:
Select * from table_name ORDER BY ID DESC LIMIT 5;
I feel its better to use pagination. If you want dont want to show page nos, better hide it or just modify the code. If you planning to do it manually its a bit messy
You can use LIMIT in you mysql query:
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 0, 5");

What is wrong with this pagination class?

I was looking for a simple pagination script and found one here, this seems to be working just fine.
However, when i click on "2", as in page 2, it just shows the records of page 2 underneath those that are already there. So basically if I would click on page 214 it still shows all of the records on one page.
I am not very experienced with PHP so i couldn't figure out what was wrong with the paginator.class.php, hopefully someone here can.
This is the code on the page where it should do the pagination:
else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9;
$pages->paginate();
$query1 = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div> ';
};
echo ' ';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}
The paginator.class.php can be found on the website I just mentioned.
The issue lies in your query:
"SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit"
You need to determine what the value of $pages->limit is. It seems to me that instead of calculating how many records should be displayed on each page (let's say 10 for argument's sake) and then determining what page you're on and setting the LIMIT condition.
What it should be set to is something like this:
LIMIT 30, 10
That's for page 4 - it displays records 30-40, rather than what I suspect it's doing, which is
LIMIT 40
That line will simply show up to 40 records and not close the lower bound of the window.
FYI take a look at the MySQL SELECT syntax in the manual.

How do i GROUP BY or DISTINCT inside array?

I have searched high and low and cant find a similar issue to what i have.
I am a beginner so please forgive my clunky query structure.
I am trying to ( have attached screen grab below of output ):
Query the photos table to get the id based on category id and also start,limit because of pagination.
Query the photos tagged table based on the photo id i just got from the first query.
But my problem is that i cant group the tags, some photos have the same tag name. And the output just shows all the tags for each photo. I want restaurant to show only once etc...
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id']." GROUP BY tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($rowTagged = mysql_fetch_array($resultTagged)) {
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php }} ?>
the above query is producing:
bar,cappucino,coffee,coffee machine,restaurant,bar,cappucino,coffee,coffee machine,restaurant,bar,coffee,restaurant,bar,coffee,coffee machine
restaurant,bar,cappucino,coffee,restaurant
what i need to show is:
bar,cappucino,coffee,coffee machine,restaurant
If anyone could help i would greatly appreciate it.
Thank you in advance.
John
My new code is
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged);
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php } ?>
I now get this: ( So i am close arent i? )
----------
cappucino
restaurant
bar
coffee machine
restaurant
coffee
coffee
restaurant
restaurant
restaurant
coffee
coffee
restaurant
restaurant
coffee machine
restaurant
coffee
I wonder if the spaces are something? i got that from copy and paste...
Any further help would be appreciated :-)
You should first perform a join between your photos and tags table, and THEN select the distinct tags.
I believe this query will let the database do all the work for you:
SELECT DISTINCT tag_name
FROM (SELECT file_id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) t1
LEFT JOIN photoTagged ON t1.id = photoTagged.file_id
You can also sort the tags in the database (ORDER BY tag_name).
Haven't tried it myself, so maybe the syntax is a bit off. But the idea should work.
distinct doesnt work if you are only getting one record at a time, so put the data in a PHP array and then use array_unique, which is PHPs way to do distinct
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged)
$tagged[] = $rowTagged['tag_name'];
}
// Let PHP do the work.
$tagged=array_unique($tagged);
while (list(,$val) = each($tagged)) {
echo "<li><a href="#">$val</li>
}
?>
you need to do a sub-query to dodge the pagination problems with the photos. If you wish the selected tags to be a subset of the photos found in your first query, then you will need to do the following.
<?php
$queryTagged = "SELECT TAG.tag_name, count(TAG.tag_name) AS num FROM photoTagged as TAG JOIN (SELECT id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) as PHOTO ON (PHOTO.id = TAG.file_id) GROUP BY TAG.tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($tagged = mysql_fetch_assoc($resultTagged)) {
echo "<li id="'.$tagged['TAG.tag_name'].'"><a href="#">".$tagged['TAG.tag_name']." (".$tagged['TAG.num'].")</li>";
}
?>
This way you will have two queries, on for finding the photos, and one for finding the tags for the photos on that page. This technically takes a little longer as MySQL has to load the query into a temporary table, but it should work fine.
SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'] ?

Categories