Displaying the last 3 news messages - php

I want to display the last 3 news messages onto my PHP page. For that I am using the following code:
function news($number) {
$number = (int)$number
$query = mysql_query("SELECT `id`, `title`, `author`, `message`, `date`
FROM `news`
WHERE `hidden` = 0
ORDER BY `date`
DESC LIMIT $number");
while ($row = mysql_fetch_array($query))
{
return '<p class="p_sub">' . $row['title'] . '~' . $row['author'] .
'</p><p>' . $row['message'] . '</p>';
var_dump($row);
}
}
echo news(3);
However, this only displays one message, not three. Anyone who can figure out why?

Using return will exit your while loop. What you could do is concatenate a string containing your HTML like so
$html = '';
while ($row = mysql_fetch_array($query)) {
$html .= '<p class="p_sub">' . $row['title'] . '~' . $row['author'] . '</p><p>' . $row['message'] . '</p>';
}
return $html;

Related

Display comments for each post in PHP/mySQL site

I'm trying to display multiple posts, each with its own multiple comments. This, below, works for the first post (or, at least shows 2 of the 3 comments), but not for the rest. I searched but couldn't find (or didn't recognize) an answer to this exact problem on this site, or elsewhere. Thanks.
$result1 = #mysql_query('select * from posts,comments where comment_post_ID = ID and post_status="publish" and comment_approved="1" ORDER BY post_date,category,subcat1,subcat2 ASC');
$result2 = #mysql_query('select * from comments where comment_approved="1" ORDER BY comment_date');
$row = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);
while ($row = mysql_fetch_array($result1)) {
$post = $row['post_content'];
$id = $row['ID'];
$title = $row['post_title'];
$content = "<br /><h3 align=\"left\">" . $title . "</h3>\n";
$content .= "<h4 align=\"left\">by " . $row['post_author'] . " - " . $row['post_date'] . "</h4>\n";
$content .= "<p align=\"left\">" . var_export($post, true) . "</p>\n<p>\n";
echo $content;
while ($row2 = mysql_fetch_array($result2)) {
$comment = "<blockquote>";
$comment .= "<h4 align=\"left\">" . $row2['comment_author'] . " commented on " . $row2['comment_date'] . "</h4>\n";
$comment .= "<p align=\"left\">" . $row2['comment_content'] . "</p>\n<p>\n";
$comment .= "</blockquote>";
if($row2['comment_post_ID'] == $id) {
echo $comment;
}
}
}
This, below, finally worked as hoped:
$postData = mysqli_query($conn,"SELECT * FROM wp_posts ORDER BY post_date ASC) or die(mysqli_error());
$commentData = mysqli_query($conn,"SELECT * FROM wp_comments WHERE comment_approved = '1' ORDER BY comment_date ASC") or die(mysqli_error());
$posts = array();
while($row = mysqli_fetch_assoc($postData)) {
$posts[] = $row;
$commentrow = mysqli_fetch_assoc($commentData);
$comments[] = $commentrow;
echo '<h3>' . $row['post_title'] . '</h3>';
echo '<h5>' . $row['post_author'] . ', ' . $row['post_date'] . '</h5>';
echo '<p>' . $row['post_excerpt'] . '... read more</p>';
if($row['comment_count'] > 0) {
echo '<blockquote>';
echo '<b>Comments</b><br />';
foreach($comments as $comment) {
if($row['ID']==$comment['comment_post_ID']) {
$comment_excerpt = substr($comment['comment_content'],0,100);
echo '<br>' . $comment_excerpt . ' - <b>' . $comment['comment_author'] . '</b>, ' . $comment['comment_date'] . '<br>';
}
}
echo '</blockquote>';
}
}

Sorting nested While() Loops

I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}

MySQL LIKE keyword Multiple Columns but Which Column Queried?

I have two scripts below. The first script only searches one Column for a user-typed keyword, then it will display the results as a list and make BOLD the characters the user typed. That script works great.
The second script is something I modified to search multiple columns. It searches just fine. The problem is that I cannot get the BOLD (or make STRONG) if the searched value came from the other columns. How do I determine if the searched value came for Column 1, Column 2, Column 3, etc...? If the searched value came from column "DESCRIP" then I want to make the letter bold in the listed value.
First Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE `MUNTERS_PN` LIKE (:keyword) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Second Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE (`MUNTERS_PN` LIKE (:keyword) OR `DESCRIP` LIKE (:keyword) OR `MANUF` LIKE (:keyword) OR `MANUF_PN` LIKE (:keyword) ) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
/******* INSERT CODE TO DETERMINE WHICH COLUMN WAS QUERIED ******/
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
$manuf = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF']);
$manuf_pn = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF_PN']);
$descrip = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['DESCRIP']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Be careful, in the echo in the second script, you use $rs['MANUF'], $rs['MANUF_PN'] and $rs['DESCRIP'] instead of $manuf, $manuf_pn and $descrip.
That is why the replacements don't appear in the output.
So you should use :
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $manuf_pn . '] <br/>' . $descrip . '</span></li>';

Order by rand(). Put first row in the end

Is this possible somehow?:
Select all rows ( order by rand() )
Make a while loop that outputs all rows except the first one
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
while ($row = $stmt->fetch_assoc()) {
// IF NOT FIRST ROW, DO THIS
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
}
And then include the excluded row at end
$text .= '<p>' . $FIRSTROW_id . '<br />' . $FIRSTROW_name . '</p>';
Create a count and if it's first value, save it in variable.
Then, after loop, you use your variable with data from first row.
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
$i = 0;
while ($row = $stmt->fetch_assoc())
{
if ( $i == 0 )
$firstrow = $row;
else
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
$i++;
}
if ( $firstrow )
$text .= '<p>' . $firstrow['id'] . '<br />' . $firstrow['name'] . '</p>';
EDIT : From what you said in comments, you can just pass first row as param in AJAX and exclude it in your query :
$sql = "SELECT id, name FROM tablename WHERE id != '".intval($_GET['id'])."' ORDER BY rand ()";
$stmt = $conn->query($sql);
$_GET['id'] will be param you send by AJAX.

Extraction of the data from MySQL using PHP

$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}

Categories