TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 05-22-2009, 08:35 PM   #1 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Bricks 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:
<?                    
$result 
mysql_query("SELECT * FROM Portfolio ORDER BY id DESC");

while(
$row mysql_fetch_array($result)) {
$id $row['id'];
$thumb $row['thumb'];
$p1 $row['p1'];
$p1d $row['p1d'];
$p2 $row['p2'];
$p2d $row['p2d'];
$p3 $row['p3'];
$p3d $row['p3d'];
$p4 $row['p4'];
$p4d $row['p4d'];
$p5 $row['p5'];
$p5d $row['p5d'];
$p6 $row['p6'];
$p6d $row['p6d'];
$p7 $row['p7'];
$p7d $row['p7d'];
$p8 $row['p8'];
$p8d $row['p8d'];
$p9 $row['p9'];
$p9d $row['p9d'];
$p10 $row['p10'];
$p10d $row['p10d'];
$name $row['name'];
$website $row['website'];

echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p1;
echo 
'" class="option" id="thumb" title="';
echo 
$p1d;
echo 
'" ><img src="';
echo 
$thumb;
echo 
'" / class="thumbs"></a> ';

if(
$p2 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p2;
echo 
'" class="hidden" title="';
echo 
$p2d;
echo 
'" ></a>';
}                        
if(
$p3 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p3;
echo 
'" class="hidden" title="';
echo 
$p3d;
echo 
'" ></a>';
}
if(
$p4 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p4;
echo 
'" class="hidden" title="';
echo 
$p4d;
echo 
'" ></a>';
}
if(
$p5 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p5;
echo 
'" class="hidden" title="';
echo 
$p5d;
echo 
'" ></a>';
}
if(
$p6 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p6;
echo 
'" class="hidden" title="';
echo 
$p6d;
echo 
'" ></a>';
}
if(
$p7 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p7;
echo 
'" class="hidden" title="';
echo 
$p7d;
echo 
'" ></a>';
}
if(
$p8 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p8;
echo 
'" class="hidden" title="';
echo 
$p8d;
echo 
'" ></a>';
}
if(
$p9 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p9;
echo 
'" class="hidden" title="';
echo 
$p9d;
echo 
'" ></a>';
}
if(
$p10 >= "1") {
echo 
'<a rel="shadowbox[';
echo 
$p1;
echo 
'];" href="';
echo 
$p10;
echo 
'" class="hidden" title="';
echo 
$p10d;
echo 
'" ></a>';
}                                                                     
}
                                     
?>
I'm super emberassed with this code, but as I said - also very unsure how I would do this with a while, do, for, foreach function? Can anyone show me? That would lead me a long way!
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tips to Improve Your Coding and Projects Village Idiot Tips & Tricks 45 12-03-2012 07:46 AM
Writing Clean Code Village Idiot Tips & Tricks 10 06-25-2012 12:35 PM
Game: Let's Develop Something Crazy! Wildhoney The Lounge 25 05-23-2009 09:18 PM
I love maintaining legacy code (priceless snippet of ASP code) Village Idiot The Lounge 10 04-03-2009 09:20 PM
Snipply.com - Code snippet website codyodell Show Off 27 04-13-2008 02:09 PM


All times are GMT. The time now is 09:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design