![]() |
Optimization of some code!
Hellow dear fellow PHP developers.
I have made a simple portfolio, but also "too simple". It's basically only if() statements, and then it does something - which results in high MySQL traffic - and lot's of useless code (I think at least!) I've now looked around for a couple of hours in search for a function which I could use, to make this script a lot smaller. But, beeing unsure what to do - and everything I tried went very very wrong, I decided to search for help from more experinced people. My code looks like this..: PHP Code:
|
I've read somewhere that using SELECT * FROM causes too much CPU usage, so even if it is a pain, I suppose you can make a SELECT call on each column, this way mysql doesn't have to think.
You can probably get away with using mysql_fetch_assoc($result) instead of array, I don't know if it's true, but I heard it was a little faster too... Also, to replace the if's, you can use switch, it is faster. |
Any chance you can show me a small bunch of code with what you exactly mean? :)
|
Quote:
PHP Code:
I'll take a look at your IF statements, a lot is redundant... |
Quote:
You need to ask yourself: Do I need all of the columns? more than likely not. The reason you need to be wary is that getting MySQL to retrieve data it doesn't need is a waste of resources better used elswhere. This waste adds disk I/O, memory and CPU overheads where they are simply not needed, not to mention network overhead and also potentially b0rking up covering indexes (if there are any). Quote:
Quote:
N.B read the comments, its quite intriguing, I'll have to run my own benchmarks. This brings me nicely to my next point, if a script needs so many if/else's there must be an easier way to do it. |
Quote:
It kinda looks like a pagination script.... |
I'm also curious what all the p1, p2,p3 is all about...pages?
|
Cool.
To clear up, this is for a mini-portfolio - so it adds more pictures (the $p1 = picture one url, $p1d = picture one description) - if I choose so from my Admin panel. Which is why I have all the if()'s, so that if those rows are empty, it should not add an empty picture, but just skip that line of code and do NOTHING. :D - but if the row is filled with an url, it should print out the picture to the gallery. It's in action here. Sorry if the explanation is messy, just ask if I need to clear something up. :) Cheers |
I could not find where you were using $id, $name and $website.
You should change the Selet * to Select col1, col2... however for most smaller sites it really does not matter unless you have a ton of extra columns. There is no need for the extra variables, and a switch would probably be better but it really does not matter on that either. You only need 1 echo to print all the junk instead of the multiples. so something like this would be a bit better. PHP Code:
|
Quote:
You are basically creating a unique column for each unique type of picture. You should only have 2 columns. I mean, if you have 1000 pictures, you can't seriously think about having 1000 columns? There's a limit for columns for mysql tables that in it itself! |
How would I do that then Allwork? I suppose their gonna be seperated by a space or something, but how am I gonna sort those out for good? :)
That really sounds like the stuff I'm looking for! CoryMathews, thanks for your clearing up! Let's see how this ends up :)) |
Quote:
$id = $row['id']; $name = $row['name']; $website = $row['website']; So your table now has just 3 columns. Each portfolio/picture gets a new row. Not new column. That way as you loop through the rows, you can base your conditionals on the values you get from the rows, not going through each column and doing IF conditionals.. Back to what Sketch said, "if you're using so many if conditionals, there's gotta be a better way to do it" or something like that... In this case, it's redesigning your DB structure...... |
Alright, I get the idea of this!
I suppose I'm gonna do somehting like 5 fields: 1. IDIn the PHP I will only have 2 IF's Quote:
|
Quote:
Yup, you can make the thumb column of bool type since it's just going to be either 0 or 1. |
Quote:
Quote:
Easiest solution (bearing in mind, i quickly hack this together): Have 2 pictures (I'm assuming you have a full and a thumb of each), name them <picturename>.gif and thumb_<picturename>.gif (or whatever). Now all we do is prefix 'thumb' to the image to get the thumb: PHP Code:
PHP Code:
With a DB structure like this: sql Code:
Test data: sql Code:
We could crudely output it thus: PHP Code:
|
Sketch, cool idea! I'm gonna work on it in a few minutes. :) I'm very happy for everyones help!
|
Alright, here is how it all ended up..
I felt like I needed to create a Thumb BOOL, as I have > 1 pictures in one gallery, which are invisible on the page before the thumb is opened, and you can browse them in the Thumbail. ![]() After that, I began the coding part. I realised the hard part was to tell the image which "hidden gallery" it was in, so I decided to use functions, to check that - my first use of functions effectively! Here's how it all ended up: PHP Code:
How do I just make the echo's one long line, with the functions in? I couldn't manage to do that! Like this: PHP Code:
Edit2: It seems like that the HTML just stops after this PHP, even though theres around 100 lines more HTML. Anyone got an idea of what's going on?! :O Edit3: It gotta be in the else{ ], since it only runs one of those, and is supposed to do 3 with my current database. Edit4: It looks like this when I view the HTML source on my page: PHP Code:
![]() Edit5: It's fixed! I just cleaned up the code on the couch watching a movie! Macbook ftw. PHP Code:
I'm just gonna clean up the rest of the code first. Edit6: Worked great. |
You should really try and use
sprintf when you have such complex lines of echoes! Otherwise you do often find you create a pretty mess.php Code:
|
As I'm currently learning about classes, it'd be super cool if someone had somne time on their hands to make my code to a class, and how to use that class so I get the same output!
I'm referring to this code..: PHP Code:
|
| All times are GMT. The time now is 09:47 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0