Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-06-2009, 12:49 PM Simplify PHP code
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Can someone make this a little smaller and simpler?

PHP Code:
<?php
require('inc/config2345.php'); 

// get adds from the database
$sql mysql_query("SELECT * FROM addvert") or die (mysql_error());

// add results to an array so can be displayed
$rows mysql_fetch_array($sql);

// echo the codes for the adds and stripslashes or it wont display properly.
echo stripslashes($rows['code'])."<br />".stripslashes($rows['code1'])."<br />".stripslashes($rows['code2']);
?>
sith717 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-06-2009, 02:19 PM Re: Simplify PHP code
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Is there a reason why you need it to be simplified? It is already pretty simple.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-06-2009, 05:53 PM Re: Simplify PHP code
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
As Nullpointer said it seem quite simple already.
It may get a tiny bit simpler by correcting data (using stripslashes for example) before putting it in your database, instead of when you're retrieveing it. That way you can do all the necessary checks on all data at the same place and you know that all data in the database is correct and won't need any extra formatting when being output.

And, correct me if I'm wrong, but by using mysql_fetch_array() the keys in $rows will be 0, 1 and 2, not 'code', 'code1' and 'code2'. You shoule be using mysql_fetch_assoc().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-06-2009, 06:15 PM Re: Simplify PHP code
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by lizciz View Post
And, correct me if I'm wrong, but by using mysql_fetch_array() the keys in $rows will be 0, 1 and 2, not 'code', 'code1' and 'code2'. You shoule be using mysql_fetch_assoc().
mysql_fetch_array has three different fetch modes: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. The default is both, meaning that you can index the array using keys or numeric indicies.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-06-2009, 07:45 PM Re: Simplify PHP code
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
So the code is simplified and the shortest it can be? If it is, then thank you.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 08-06-2009, 08:32 PM Re: Simplify PHP code
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Short answer: Yes

Long answer:

I'm not sure what your goal is here. If you're trying to somehow optimize that small chunk of code you're probably not spending your time wisely. Code that appears simple, in terms of the number of lines or the amount of code, can often be misleading. The number of lines in a program is probably the worst way to estimate the performance of that program. There are plenty of cases where you can replace a function call with several lines of code and improve the performance of your program. You should try to focus on the efficiency of your code rather than the size of it.

This advice isn't really helpful in this particular case, but if there were some function that allowed you to do what you are doing in 1 or 2 lines of code it wouldn't necessarily be a better solution.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Simplify PHP code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22768 seconds with 12 queries