Sorting nested While() Loops - php

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>';
}
}
}

Related

How to check for no result in MySQL query

I want to check for no result in MySQL query and return a no result message to the user. How do I integrate it with my current code?
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
echo '<div class="mydiv">';
if($row['photo']){
echo "<p>" . '<span class="glyphicon" aria-hidden="true"></span>' . '</p>' . '<p>' . "<img src=http://localhost:8888/example/images/" . $row['photo'] . "><hr>";
}else
echo '<p>' . '<span class="glyphicon" aria-hidden="true"></span>' . $row['message'] . '</p>';
echo '<p>' . '~' . $row['username'] . '</p>';
echo '<p>' . $row['datetime'] . ' ' .'(UTC)' . '</p>';
echo '</div>';
}
I want a "no result" message to show up when there are no records found. How do I integrate it in my existing code. Please advise.
if ($row = mysql_num_rows($result) == 0){
echo "<h3>There are no result found.</h3>";
}
if ($result = mysql_query($sql) && mysql_num_rows($result) > 0) {
// there are results in $result
// whatever you want to do with your results
} else {
// no results
echo "<h3>There are no results found.</h3>";
}

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>';
}
}

Returning records from database in to the table with php

I have this script where I am for first time returning records in to the table. It works quite fine but, it is returning only last added record. Can you look at it, because I cant find what is wrong.
$query = 'SELECT character_id, alias, real_name, alignment
FROM comic_character
ORDER BY ' . $order[$o];
$result = mysql_query($query, $db) or die (mysql_error($db));
if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th>Alias</th>';
echo '<th>Real Name</th>';
echo '<th>Alignment</th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';
$odd = true;
while ($row = mysql_fetch_array($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '</td><td>' . $row['alias'] . '</td>';
echo '<td>' . $row['real_name'] . '</td>';
echo '<td>' . $row['alignment'] . '</td>';
$query2 = 'SELECT power FROM comic_power p
JOIN comic_character_power cp
ON p.power_id = cp.power_id
WHERE cp.character_id = ' . $row['character_id'] . '
ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));
if (mysql_num_rows($result2) > 0) {
$powers = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$powers[] = $row2['power'];
}
echo '<td>' . implode(',', $powers) . '</td>';
} else {
echo '<td>none</td>';

Else statement doesn't work in option select

I am trying to implement a dropdown search option. All my search results are working. All the commands that I have assigned to if statements work, but when it does to else it deosn't work.
Here is my code:
if(isset($_REQUEST['submit'])){
$opt = $_POST['opt'];
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else{
echo "Your Searched keyword did not match";
}
}
What to do?
Try this: Take a flag to check if record exists.
$flag = false;
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}
if(!$flag){
echo "Your Searched keyword did not match";
}

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