[PHP & MySQL]My code isn't efficient

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

[PHP & MySQL]My code isn't efficient

Post by Locust »

Code: Select all

$cols = 0;

while($row = mysql_fetch_array($result)){
$cols++;
}

while($row = mysql_fetch_array($result)){
echo ("<a href=\"".$row['men_url']."\">".$row['men_value']."</a>");

if ($cols > 1) {echo " | ";}
$cols--;
}
This is my code so that when it's done I have something like

URL 1 | URL 2 | URL 3 | URL 4 | URL 5

As you can see it seperates each URL with a '|' but I have 2 "while" statements in my code which I believe could potentially slow things down. I'm a bit of a perfectionist and I think this code could be better placed. Anyone have any ideas to cut this down?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

what is the first while doing?

and i think this would work better:

Code: Select all

$cols = 0; 

while($row = mysql_fetch_array($result)){ 
$cols = $cols+1; 
} 

while($row = mysql_fetch_array($result)){ 
echo ("<a href=\"".$row['men_url']."\">".$row['men_value']."</a>"); 

if ($cols > 1) {echo " | ";} 
$cols = $cols-1; 
}
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

The first 'while' is simply counting the number of results from the database query. Then every time a fetch is made, it returns the result and subtracts '1' from $cols.

I want it to sepperate each URL with a | and this is the only way I can think of doing it but I don't think it's optimised.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_num_rows()

alternately, you could simply add each record to an array the use a slightly clever implode()
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

I like mysql_num_rows()

For anyone else that's interested, my end code:

Code: Select all

$result = mysql_query($query);

$num_rows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
echo ("<a href=\"".$row['men_url']."\">".$row['men_value']."</a>");

if ($num_rows > 1) {echo " | ";}
$num_rows--;
}
Thanks as always Feyd
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

There are faster fetchers than _array, aren't there?
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

If you have another suggestion to my code I'm all ears :D
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Here ya go..

Code: Select all

$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)) {
    $records[] = '<a href="' . $row['men_url'] . '">' . $row['men_value'] . '</a>';
}

echo implode(' | ', $records);
Last edited by Benjamin on Thu Jul 27, 2006 7:55 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

feyd wrote:alternately, you could simply add each record to an array the use a slightly clever implode()
I didn't even see that when I wrote my code :roll:
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

i thought that mysql_fetch_row was the fastest way to gather data from a resource but i could be wrong.
Post Reply