Old 30-06-2004, 12:43   #1 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,236
limiting results

Right I've got a foreach loop displaying some results of some crazy ass db queries. All working fine...
Code:
asort($order) foreach ($order as $id => $created) { printf('<a href="%s/%2$s.html">%s</a>', $names[$id]['table'], $names[$id]['name']); }

I need to limit the results though so tried this instead
Code:
asort($order) for ($i = 0; $i < 10; ++$i) { $id = $order[$i]; printf('<a href="%s/%2$s.html">%s</a>', $names[$id]['table'], $names[$id]['name']); }

It doesn't work. Anyone know why?
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 30-06-2004, 13:03   #2 (permalink)
DaiWelsh
Registered User
 
Join Date: Mar 2004
Location: Derby, UK
Posts: 24
What does $order hold at the beginning of this code? I am guessing from the way you use it in the first example that it is an associative array (not a straight numeric array) in which case you cannot just loop through the items from 0-9 as you are trying to do, since this will not work.

You could either use a loop variable in the first example or truncate the $order array to ten items before you start the loop, not sure which will be easier without knowing what your data looks like.

HTH,

Dai
  Reply With Quote
Old 30-06-2004, 13:10   #3 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,236
OK here's some more example code. Like I say, this works but I need to limit the results. Any help appreciated.

Code:
$tables = array('boys', 'girls'); $names = array(); $order = array(); foreach ($tables as $table) { $result = mysql_query('SELECT id, name, created FROM ' . $table); while ($row = mysql_fetch_assoc($result)) { $names[$table . '_' . $row['id']] = array('name' => $row['name'], 'table' => $table); $order[$table . '_' . $row['id']] = $row['created']; } } asort($order) foreach ($order as $id => $created) { printf('<a href="%s/%2$s.html">%s</a>', $names[$id]['table'], $names[$id]['name']); }
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 30-06-2004, 14:18   #4 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,236
Sorted it.
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Reply



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

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8