Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Arrange by highest (Javascript)

Posted on 2013-01-20
7
214 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
suddenly angular cli doesn't work. 5 39
jquery tab header text 1 23
AJAX Wordpress Not Reading Variable 2 28
Why is initialize not a function? 8 21
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

860 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