5 Replies - 552 Views - Last Post: 20 February 2015 - 12:06 PM Rate Topic: -----

#1 BNOCSK   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-February 15

Slow loading times where there's no results

Posted 20 February 2015 - 11:31 AM

Hi all,

So I posted a question the other day and you guys were great! I decided I better start with an easier project than the one I was trying to get a better hold on this.

I've basically made a site that displays Music Albums - you click the Album and it brings up viewAlbum.php?vp=$album_id which displays the tracklist and cover art.
Now, I had it working before but I had set up my tracklist database a terrible way - I redone that today and it's much better in terms of how I store my tracks for the Album's tracklist.
My page load times were super quick (Not sure exactly how fast) and still are when I run the old version - NOTE: The pages without any tracks in the db currently were loading just as quick.

The problem I'm having is that since I made the change the viewAlbum.php page is loading at the regular speed for Albums that have tracks to list in the database but is taking approximately 1minute to load viewAlbum.php for an album that I've not entered tracks into the db for (Does this make sense)?

It's kind of backwards to what you would expect:
If there was no results = less data = it should load quicker.

Anyway because my code is working everywhere else I'll just post the new code that is causing the problem.
I know a way around it is to just make sure every Album has tracks loaded into the db but I'd like to know just to know.

// Tracklist Count Variable
 	$i = '1';
    
    // FETCH TRACKLIST
    // This works
    $getTracklist = $conn->query("SELECT * FROM tracks WHERE album_id = $track_id"); 
	$getTracklist->execute();
    $tracklist = $getTracklist->fetchAll()
;
    echo "<h5>";
        echo "<ul>";

    // While $i is under the variable declared in the album db track_count column. 
    // Echo the track number and details 
    while($i <= $album['track_count']) {
	    foreach ($tracklist as $track) {
            echo "<li class='list-tracklist clearfix'>";
                // Echo track number 1,2,3...18 etc.
    		echo "<div class='list-number'>" . $i . ".</div>";
                // Echo track name 
                echo "<div class='list-track-name'>" . $track['track_name'] . "</div>";
                // There's a column in the tracks Db that is called track_extras 
                // It's just Varchar with values like 'feat Pink Floyd'
                // If the track has a extra, print it.
                if ($track['track_extras'] != '') {
                    echo "<div class='track-extras'>( " . $track['track_extras'] . " )</div>";
                }
                // Echo track length
                echo "<div class='track-length'>" . $track['track_length'] . "</div>";
    	    echo "</li>";
            
            $i++;
    	}
    }

        echo "</ul>";
    echo "</h5></div>";
}



Hopefully I've commented that in a way that'll make it easy to see what's going on.
That's the only section of code I've changed since it was working 100%.

Like I said, now it's loading pages with tracks to list very quickly and taking around a minute to load a page without any tracks to list.

If anyone can see my error, I'd appreciate it.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Slow loading times where there's no results

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2766
  • View blog
  • Posts: 8,072
  • Joined: 15-January 14

Re: Slow loading times where there's no results

Posted 20 February 2015 - 11:37 AM

Where do you set $album['track_count'], and why do you have 2 loops there? It looks like you only need the foreach loop, what's the point of the while loop?

PHP has a function that you can use to keep track of time, you could use that to time various things and see how long they take:

http://php.net/manua...n.microtime.php

e.g.:

$start = microtime(true);

$sum = 0;
for ($i = 0; $i < 1000000; $i++) {
  $sum += $i;
}

$end = microtime(true);
echo 'Elapsed time is ' . ($end - $start) . ' seconds';

Was This Post Helpful? 1
  • +
  • -

#3 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 602
  • Joined: 19-May 14

Re: Slow loading times where there's no results

Posted 20 February 2015 - 11:44 AM

Well i think its not the other way around because when you have a list of tracks lets say

name:
1
2
3
4
5
6
7
8
9
etc
etc
etc

and you want to get track number 2 then the query only has to check 2 collumns to get the result BUT
if the query is searching for a number 123 then it would need to search how many collumns you have in your database to get the final result wich is an error message with: no track found.
but most of the time this should be done very very fast unless you have a very very old computer/laptop running the appach server.

i could be wrong but i think querys work this way

Also maby you want to check if the result of the query contains any data and if it does not contain any data then you would not need to run the While loops because theres nothing to display.

This post has been edited by RamonRobben: 20 February 2015 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 BNOCSK   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-February 15

Re: Slow loading times where there's no results

Posted 20 February 2015 - 11:55 AM

View PostArtificialSoldier, on 20 February 2015 - 11:37 AM, said:

Where do you set $album['track_count'], and why do you have 2 loops there? It looks like you only need the foreach loop, what's the point of the while loop?

PHP has a function that you can use to keep track of time, you could use that to time various things and see how long they take:

http://php.net/manua...n.microtime.php

e.g.:

$start = microtime(true);

$sum = 0;
for ($i = 0; $i < 1000000; $i++) {
  $sum += $i;
}

$end = microtime(true);
echo 'Elapsed time is ' . ($end - $start) . ' seconds';



Hi, thanks.

I've used a while loop because the working code before I changed my databases was this

// Tracklist Count Variable
 	$i = '1';
    
    // FETCH TRACKLIST
    $getTracklist = $conn->query("SELECT * FROM tracklist WHERE tracklist_id = $track_id");
	$getTracklist->execute();
    $tracklist = $getTracklist->fetchAll();
    echo "<h5>";
        echo "<ul>";
    // While $i is under the variable declared in tracklist_count.php
    // Echo the track number and track title 
    while($i <=  ${$track_artist . '_' . $album_name}) {
	    foreach ($tracklist as $track) {
            echo "<li class='list-tracklist clearfix'>";
    		echo "<div class='list-number'>" . $i . ".</div> <div class='list-track-name'>" . $track["track-" . $i . ""] . "</div>";
            echo "<div class='track-length'>04:42</div>";
    		echo "</li>";
    	}
    	$i++;
    }


The $album['track_count'] is set further up in a pull from the 'Album' db and looks like this

$getAlbum = $conn->prepare("SELECT * FROM album WHERE id = '$id'");
$getAlbum->execute();
$albums = $getAlbum->fetchAll();
foreach ($albums as $album) {
	///////////////////////
        // Now I can use $album['track_count'] to get how
        // many tracks the album fetched has in the database.
        ///////////////////////



Basically when I put the tracks in I just add in how many tracks there were into a separate field

That's why my while statement is 'while $i is <= track_count do this'
That way it only posts a number where a track exists

I'm actually just thinking that because I changed my database I don't need a count because it can only display the data that has the same id across both tables.
I think I know how to solve this on my own - I'll take a look at adding microtime() if the delay persists after I change some stuff.

Thanks for the help and getting me thinking.

Sean
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2766
  • View blog
  • Posts: 8,072
  • Joined: 15-January 14

Re: Slow loading times where there's no results

Posted 20 February 2015 - 12:02 PM

The while loop isn't necessary in that code either, because the foreach loop never changes. There's no reason to run that foreach loop more than once, you only need to run it once to have it loop over the entire track list. You don't need the extra while loop around the foreach loop which may run the foreach multiple times, you only need to display the track list once. So, you can just remove the while loop.

As for the track count, if you're storing the tracks separately in the database then you don't need another column to list how many there are, you can figure out how many there are with a SQL query. That's more flexible than needing to update the track count any time you add or remove a track.
Was This Post Helpful? 1
  • +
  • -

#6 BNOCSK   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-February 15

Re: Slow loading times where there's no results

Posted 20 February 2015 - 12:06 PM

View PostArtificialSoldier, on 20 February 2015 - 12:02 PM, said:

The while loop isn't necessary in that code either, because the foreach loop never changes. There's no reason to run that foreach loop more than once, you only need to run it once to have it loop over the entire track list. You don't need the extra while loop around the foreach loop which may run the foreach multiple times, you only need to display the track list once. So, you can just remove the while loop.

As for the track count, if you're storing the tracks separately in the database then you don't need another column to list how many there are, you can figure out how many there are with a SQL query. That's more flexible than needing to update the track count any time you add or remove a track.



Cool man, I'll give all the changes a go and get it working again with normal load times!

Thanks for the help again!

Sean
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1