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
Locust
Forum Commoner
Posts: 31 Joined: Sat Jul 22, 2006 10:26 am
Post
by Locust » Thu Jul 27, 2006 12:19 pm
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?
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Thu Jul 27, 2006 12:23 pm
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 » Thu Jul 27, 2006 12:25 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Jul 27, 2006 12:30 pm
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 » Thu Jul 27, 2006 12:34 pm
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
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Thu Jul 27, 2006 12:37 pm
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 » Thu Jul 27, 2006 7:42 pm
If you have another suggestion to my code I'm all ears :D
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 27, 2006 7:49 pm
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 27, 2006 7:54 pm
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
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Fri Jul 28, 2006 1:12 am
i thought that mysql_fetch_row was the fastest way to gather data from a resource but i could be wrong.