Solved

Arrange by highest (Javascript)

Posted on 2013-01-20
7
213 Views
Last Modified: 2013-01-21
Hi all,

I have a short little php script done that takes the values from the datasbase and displays them on a page. I wonder could someone help me to create an addition to this so that the result count is arranged in descending order, like this:

For example:

<!--<p>New Members: 86
<p>Meditations Today: 45
<p>Journal Entries Today: 22

Here is the print out part of the script:

<!--<p>New Members: <?php echo $new_members_today;  ?> </p>--!>
<p>M Today: <?php echo $total_meditations_today;  ?>  </p>	
<p>J Today: <?php echo $total_journals_today; ?> </p>
<p>ELogged: <?php echo $total_exercises_today; ?></p>
<p>DT oday: <?php echo $total_dyads_today; ?>  </p>
<p>Ebooks Downloaded: <?php echo $total_ebooks_downloaded_today; ?> </p>
<p>Ebooks On Shelf: <?php echo $total_ebooks_shelf_today; ?></p>

Open in new window


Thanks so much for your help,

Derek
0
Comment
Question by:TLN_CANADA
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 38799996
just add
ORDER BY your_filed DESC

Open in new window

to your database query...

HTH

Ivo Stoykov
0
 

Author Comment

by:TLN_CANADA
ID: 38800001
Thanks Ivo but I don't think this will work as they are in different tables. Here is the db part of the code so you'll get a better idea of what I mean.

$query1 = mysql_query("SELECT * FROM exercising_table WHERE exercise_timestamp  > timestampadd(hour, -24, now())");
	$total_exercises_today = mysql_num_rows($query1);
	
	$query2 = mysql_query("SELECT * FROM meditation_table WHERE session_timestamp  > timestampadd(hour, -24, now())");
	$total_meditations_today = mysql_num_rows($query2);
	
	$query3 = mysql_query("SELECT * FROM journal_table WHERE entry_timestamp  > timestampadd(hour, -24, now())");
	$total_journals_today = mysql_num_rows($query3);
	
	$query4 = mysql_query("SELECT * FROM dyad_counter_table WHERE dyad_counter_time   > timestampadd(hour, -24, now())");
	$total_dyads_today = mysql_num_rows($query4);
	
	$query5 = mysql_query("SELECT * FROM phpfox_ebooks_user_downloads WHERE download_timestamp   > timestampadd(hour, -24, now())");
	$total_ebooks_downloaded_today = mysql_num_rows($query5);
	
	$query6 = mysql_query("SELECT * FROM phpfox_ebooks_user_shelf WHERE download_timestamp   > timestampadd(hour, -24, now())");
	$total_ebooks_shelf_today = mysql_num_rows($query6);
	
	//$query7 = mysql_query("SELECT * FROM phpfox_user WHERE ('joined > time() - 86400'); ") ;
	//$new_members_today = mysql_num_rows($query7);




?>
<p><strong>Today</strong></p>
<!--<p>New Members: <?php echo $new_members_today;  ?> </p>--!>
<p>Med Today: <?php echo $total_meditations_today;  ?>  </p>	
<p>Journal Entries Today: <?php echo $total_journals_today; ?> </p>
<p>Exercises Logged: <?php echo $total_exercises_today; ?></p>
<p>D Completed Today: <?php echo $total_dyads_today; ?>  </p>
<p>Ebooks Downloaded: <?php echo $total_ebooks_downloaded_today; ?> </p>
<p>Ebooks On Shelf: <?php echo $total_ebooks_shelf_today; ?></p>

Open in new window

0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 38800020
and these have nothing in common so you could join them?

if they really haven't you could use array like:
$res = array('total_meditations_today' => $total_meditations_today, 'total_journals_today' => $total_journals_today, as so on ...)
array_reverse(sort($res, SORT_NUMERIC))

Open in new window


HTH

Ivo Stoykov
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:TLN_CANADA
ID: 38800025
Yes Ivo, they have nothing in common. So how would I print this array in order then?

Thanks for your help, D
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38800039
Try like this, I have hardcoded values for variables

<?php
$array=array();
/*$query1 = mysql_query("SELECT * FROM exercising_table WHERE exercise_timestamp  > timestampadd(hour, -24, now())");
	$array['total_exercises_today'] = mysql_num_rows($query1);
	
	$query2 = mysql_query("SELECT * FROM meditation_table WHERE session_timestamp  > timestampadd(hour, -24, now())");
	$array['total_meditations_today'] = mysql_num_rows($query2);
	
	$query3 = mysql_query("SELECT * FROM journal_table WHERE entry_timestamp  > timestampadd(hour, -24, now())");
	$array['total_journals_today'] = mysql_num_rows($query3);
	*/
	$array['total_exercises_today'] = '5';
	$array['total_meditations_today'] = '8';
	$array['total_journals_today'] = '2';
	asort($array);
	echo "<pre>";
	print_r(($array));
	echo "</pr>";
	

Open in new window

0
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 500 total points
ID: 38800061
$html = array('total_meditations_today' => '<p>M Today: __  </p>',
              'total_journals_today' => '<p>J Today: __ </p>', and so on ...);

$res = array('total_meditations_today' => $total_meditations_today,
             'total_journals_today' => $total_journals_today, as so on ...);
arsort($res, SORT_NUMERIC); //Sort an array in reverse order
foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
}

Open in new window


HTH

Ivo Stoykov
0
 

Author Closing Comment

by:TLN_CANADA
ID: 38803596
Thank you, works perfectly!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question