Solved

Arrange by highest (Javascript)

Posted on 2013-01-20
7
211 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
Comment Utility
just add
ORDER BY your_filed DESC

Open in new window

to your database query...

HTH

Ivo Stoykov
0
 

Author Comment

by:TLN_CANADA
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:TLN_CANADA
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
Thank you, works perfectly!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now