Jump to content

[SOLVED] Sort after highest avg number in php


JJohnsenDK

Recommended Posts

Hey

 

Im trying to sort this after the highest rated blog. I get the avg number from functions2.php with get_rating_avg() and then i want to sort the while loop after which user_blogs_id is highest. Here is the code:

 

<?php
require_once "maincore.php";
include('functions2.php');

				$blogsQuery = mysql_query("SELECT * FROM user_blogs ORDER BY date DESC") or die(mysql_error());
				while($blogsRow = mysql_fetch_array($blogsQuery)){

					echo "<p>".get_rating_avg($blogsRow['user_blogs_id'], 1)."</p>";

					//Bruger billede
					$image = get_useravatar($blogsRow['user_id']);
					if(empty($image)){
						$image =  "<img src='images/avatars/none.jpg' alt='".get_useravatar($blogsRow['user_id'])."' width='50' />";
					}else{
						$image = "<img src='images/avatars/".get_useravatar($blogsRow['user_id'])."' alt='".get_useravatar($blogsRow['user_id'])."' width='50' />";
					}
					//Tekst
					echo "
					<table>
						<tr>
							<td style='width: 500px; border: 1px solid #000000;'>
								<table>
									<tr>
										<td>						
											<table>
												<tr>
													<td><div class='user_blogs_image'>".$image."</div></td>
												</tr>
											</table>
										</td>
										<td>
											<table>
												<tr>
													<td><p>".$blogsRow['headline']."</p></td>
												</tr>
												<tr>
													<td><div><p>".get_username($blogsRow['user_id'])." | ".showdate("longdate",$blogsRow['date'])." | Kommentarer (".get_comments($blogsRow['user_blogs_id']).") | <a href='user_view_blog.php?blog=".$blogsRow['user_blogs_id']."'>Læs mere</a></p></div>
													<div><p>".substr($blogsRow['text'], 0, 100)."</p></div></td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
							<td>
								<table>
									<tr>
										<td>

										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					";
				}
				?>

Link to comment
Share on other sites

try

<?php
require_once "maincore.php";
include('functions2.php');
$a = array();

				$blogsQuery = mysql_query("SELECT * FROM user_blogs ORDER BY date DESC") or die(mysql_error());
				while($blogsRow = mysql_fetch_array($blogsQuery)){
				        $av = get_rating_avg($blogsRow['user_blogs_id'], 1);
					$avg = "<p>".$av."</p>";

					//Bruger billede
					$image = get_useravatar($blogsRow['user_id']);
					if(empty($image)){
						$image =  "<img src='images/avatars/none.jpg' alt='".get_useravatar($blogsRow['user_id'])."' width='50' />";
					}else{
						$image = "<img src='images/avatars/".get_useravatar($blogsRow['user_id'])."' alt='".get_useravatar($blogsRow['user_id'])."' width='50' />";
					}
					//Tekst
					$text = "
					<table>
						<tr>
							<td style='width: 500px; border: 1px solid #000000;'>
								<table>
									<tr>
										<td>						
											<table>
												<tr>
													<td><div class='user_blogs_image'>".$image."</div></td>
												</tr>
											</table>
										</td>
										<td>
											<table>
												<tr>
													<td><p>".$blogsRow['headline']."</p></td>
												</tr>
												<tr>
													<td><div><p>".get_username($blogsRow['user_id'])." | ".showdate("longdate",$blogsRow['date'])." | Kommentarer (".get_comments($blogsRow['user_blogs_id']).") | <a href='user_view_blog.php?blog=".$blogsRow['user_blogs_id']."'>Læs mere</a></p></div>
													<div><p>".substr($blogsRow['text'], 0, 100)."</p></div></td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
							<td>
								<table>
									<tr>
										<td>

										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					";
                                                $a[] = array($av, $avg, $text);
				}
array_multisort($a, SORT_DESC);
foreach($a as $b){ echo $b[1],$b[2];}
				?>

not tested

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.